|
OpenMAXBellagio 0.9.3
|
00001 00026 #include "content_pipe_inet.h" 00027 00028 /* 00029 * Create a socket 00030 */ 00031 00032 static CPresult Create( CPhandle *hContent, CPstring szURI ) 00033 { 00034 inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent; 00035 CPresult err = 0; 00036 int nHostPort; 00037 struct sockaddr_in sAddress; /* Internet socket address stuct */ 00038 int nAddressSize=sizeof(struct sockaddr_in); 00039 00040 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00041 00042 if(1 != sscanf(szURI, "inet://%d", &nHostPort)) 00043 err = KD_EINVAL; 00044 00045 pPipe->sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 00046 if(SOCKET_ERROR == pPipe->sfd) { 00047 printf("\nCould not make a socket\n"); 00048 err = KD_EIO; 00049 } 00050 00051 if(0 == err) { 00052 00053 /* fill address struct */ 00054 sAddress.sin_addr.s_addr = INADDR_ANY; 00055 sAddress.sin_port = htons(nHostPort); 00056 sAddress.sin_family = AF_INET; 00057 00058 /* bind to a port */ 00059 if(SOCKET_ERROR == bind(pPipe->sfd ,(struct sockaddr*) &sAddress, sizeof(sAddress))) { 00060 printf("\nCould not connect to host\n"); 00061 err = KD_EIO; 00062 } 00063 00064 } 00065 00066 if(0 == err) /* establish listen queue */ 00067 if(listen(pPipe->sfd, QUEUE_SIZE) == SOCKET_ERROR) { 00068 printf("\nCould not listen\n"); 00069 err = KD_EIO; 00070 } 00071 00072 if(0 == err) /* get the connected socket */ 00073 pPipe->cfd = accept(pPipe->sfd, (struct sockaddr*) &sAddress, (socklen_t*) &nAddressSize); 00074 00075 return err; 00076 } 00077 00081 static CPresult Open( CPhandle* hContent, CPstring szURI, CP_ACCESSTYPE eAccess ) 00082 { 00083 inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent; 00084 CPresult err = 0; 00085 char strHostName[80]; 00086 int nHostPort = 0; 00087 00088 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00089 00090 { 00091 char *pc = strrchr(szURI, ':'); 00092 if(pc != NULL) { 00093 strncpy(strHostName, szURI+7, (long) pc - (long) szURI - 7); 00094 strHostName[(long) pc - (long) szURI - 7] = '\0'; 00095 nHostPort = atoi(++pc); 00096 } 00097 } 00098 00099 /* make a socket */ 00100 pPipe->cfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 00101 if(SOCKET_ERROR == pPipe->cfd) { 00102 err = KD_EIO; 00103 } 00104 00105 if(0 == err) { 00106 00107 struct hostent* pHostInfo; /* holds info about a machine */ 00108 long nHostAddress; 00109 struct sockaddr_in sAddress; /* Internet socket address stuct */ 00110 00111 /* get IP address from name */ 00112 pHostInfo = gethostbyname(strHostName); 00113 00114 /* copy address into long */ 00115 memcpy(&nHostAddress, pHostInfo->h_addr, pHostInfo->h_length); 00116 00117 /* fill address struct */ 00118 sAddress.sin_addr.s_addr = nHostAddress; 00119 sAddress.sin_port = htons(nHostPort); 00120 sAddress.sin_family = AF_INET; 00121 00122 /* connect to host */ 00123 if(SOCKET_ERROR == connect(pPipe->cfd, (struct sockaddr*) &sAddress, sizeof(sAddress))) { 00124 printf("\nCould not connect to host\n"); 00125 err = KD_EIO; 00126 } 00127 00128 } 00129 00130 return err; 00131 } 00132 00134 static CPresult Close( CPhandle hContent ) 00135 { 00136 inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent; 00137 CPresult err = 0; 00138 00139 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00140 00141 /* close socket */ 00142 if(SOCKET_ERROR == close(pPipe->cfd)) { 00143 printf("\nCould not close client socket\n"); 00144 err = KD_EIO; 00145 } 00146 00147 return err; 00148 } 00149 00151 static CPresult CheckAvailableBytes( CPhandle hContent, CPuint nBytesRequested, CP_CHECKBYTESRESULTTYPE *eResult ) 00152 { 00153 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00154 00155 return KD_EBADF; 00156 } 00157 00159 static CPresult SetPosition( CPhandle hContent, CPint nOffset, CP_ORIGINTYPE eOrigin) 00160 { 00161 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00162 00163 return KD_EBADF; 00164 } 00165 00167 static CPresult GetPosition( CPhandle hContent, CPuint *pPosition) 00168 { 00169 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00170 00171 return KD_EBADF; 00172 } 00173 00176 static CPresult Read( CPhandle hContent, CPbyte *pData, CPuint nSize) 00177 { 00178 inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent; 00179 CPresult err = 0; 00180 ssize_t count; 00181 00182 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00183 00184 count = read(pPipe->cfd, (void*) pData, (size_t) nSize); 00185 00186 if(count < nSize) { 00187 err = KD_EIO; /* ??? */ 00188 } else if(count == -1) { 00189 err = KD_EIO; /* ??? */ 00190 } 00191 00192 return err; 00193 } 00194 00207 static CPresult ReadBuffer( CPhandle hContent, CPbyte **ppBuffer, CPuint *nSize, CPbool bForbidCopy) 00208 { 00209 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00210 00211 return KD_EBADF; 00212 } 00213 00215 static CPresult ReleaseReadBuffer(CPhandle hContent, CPbyte *pBuffer) 00216 { 00217 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00218 00219 return KD_EBADF; 00220 } 00221 00224 static CPresult Write( CPhandle hContent, CPbyte *pData, CPuint nSize) 00225 { 00226 inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent; 00227 CPresult err = 0; 00228 ssize_t count; 00229 00230 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00231 00232 count = write(pPipe->cfd, (void*) pData, (size_t) nSize); 00233 00234 if(count < nSize) { 00235 err = KD_EIO; /* ??? */ 00236 } else if(count == -1) { 00237 err = KD_EIO; /* ??? */ 00238 } 00239 00240 return err; 00241 } 00242 00246 static CPresult GetWriteBuffer( CPhandle hContent, CPbyte **ppBuffer, CPuint nSize) 00247 { 00248 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00249 00250 return KD_EBADF; 00251 } 00252 00255 static CPresult WriteBuffer( CPhandle hContent, CPbyte *pBuffer, CPuint nFilledSize) 00256 { 00257 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00258 00259 return KD_EBADF; 00260 } 00261 00263 static CPresult RegisterCallback( CPhandle hContent, CPresult (*ClientCallback)(CP_EVENTTYPE eEvent, CPuint iParam)) 00264 { 00265 DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__); 00266 00267 return KD_EBADF; 00268 } 00269 00270 CPresult inet_pipe_Constructor(CP_PIPETYPE **ppPipe, CPstring szURI) 00271 { 00272 inet_ContentPipe* pPipe; 00273 00274 pPipe = (inet_ContentPipe*) calloc(1, sizeof(inet_ContentPipe)); 00275 00276 if(NULL != pPipe) { 00277 00278 pPipe->pipe.Open = Open; 00279 pPipe->pipe.Close = Close; 00280 pPipe->pipe.Create = Create; 00281 pPipe->pipe.CheckAvailableBytes = CheckAvailableBytes; 00282 pPipe->pipe.SetPosition = SetPosition; 00283 pPipe->pipe.GetPosition = GetPosition; 00284 pPipe->pipe.Read = Read; 00285 pPipe->pipe.ReadBuffer = ReadBuffer; 00286 pPipe->pipe.ReleaseReadBuffer = ReleaseReadBuffer; 00287 pPipe->pipe.Write = Write; 00288 pPipe->pipe.GetWriteBuffer = GetWriteBuffer; 00289 pPipe->pipe.WriteBuffer = WriteBuffer; 00290 pPipe->pipe.RegisterCallback = RegisterCallback; 00291 00292 pPipe->sfd = SOCKET_ERROR; /* Server file descriptor */ 00293 pPipe->cfd = SOCKET_ERROR; /* Client file descriptor */ 00294 00295 *ppPipe = (CP_PIPETYPE*) pPipe; 00296 } 00297 00298 return 0; 00299 } 00300