OpenMAXBellagio  0.9.3
content_pipe_inet.c
Go to the documentation of this file.
1 
26 #include "content_pipe_inet.h"
27 
28 /*
29  * Create a socket
30 */
31 
32 static CPresult Create( CPhandle *hContent, CPstring szURI )
33 {
34  inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent;
35  CPresult err = 0;
36  int nHostPort;
37  struct sockaddr_in sAddress; /* Internet socket address stuct */
38  int nAddressSize=sizeof(struct sockaddr_in);
39 
40  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
41 
42  if(1 != sscanf(szURI, "inet://%d", &nHostPort))
43  err = KD_EINVAL;
44 
45  pPipe->sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
46  if(SOCKET_ERROR == pPipe->sfd) {
47  printf("\nCould not make a socket\n");
48  err = KD_EIO;
49  }
50 
51  if(0 == err) {
52 
53  /* fill address struct */
54  sAddress.sin_addr.s_addr = INADDR_ANY;
55  sAddress.sin_port = htons(nHostPort);
56  sAddress.sin_family = AF_INET;
57 
58  /* bind to a port */
59  if(SOCKET_ERROR == bind(pPipe->sfd ,(struct sockaddr*) &sAddress, sizeof(sAddress))) {
60  printf("\nCould not connect to host\n");
61  err = KD_EIO;
62  }
63 
64  }
65 
66  if(0 == err) /* establish listen queue */
67  if(listen(pPipe->sfd, QUEUE_SIZE) == SOCKET_ERROR) {
68  printf("\nCould not listen\n");
69  err = KD_EIO;
70  }
71 
72  if(0 == err) /* get the connected socket */
73  pPipe->cfd = accept(pPipe->sfd, (struct sockaddr*) &sAddress, (socklen_t*) &nAddressSize);
74 
75  return err;
76 }
77 
81 static CPresult Open( CPhandle* hContent, CPstring szURI, CP_ACCESSTYPE eAccess )
82 {
83  inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent;
84  CPresult err = 0;
85  char strHostName[80];
86  int nHostPort = 0;
87 
88  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
89 
90  {
91  char *pc = strrchr(szURI, ':');
92  if(pc != NULL) {
93  strncpy(strHostName, szURI+7, (long) pc - (long) szURI - 7);
94  strHostName[(long) pc - (long) szURI - 7] = '\0';
95  nHostPort = atoi(++pc);
96  }
97  }
98 
99  /* make a socket */
100  pPipe->cfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
101  if(SOCKET_ERROR == pPipe->cfd) {
102  err = KD_EIO;
103  }
104 
105  if(0 == err) {
106 
107  struct hostent* pHostInfo; /* holds info about a machine */
108  long nHostAddress;
109  struct sockaddr_in sAddress; /* Internet socket address stuct */
110 
111  /* get IP address from name */
112  pHostInfo = gethostbyname(strHostName);
113 
114  /* copy address into long */
115  memcpy(&nHostAddress, pHostInfo->h_addr, pHostInfo->h_length);
116 
117  /* fill address struct */
118  sAddress.sin_addr.s_addr = nHostAddress;
119  sAddress.sin_port = htons(nHostPort);
120  sAddress.sin_family = AF_INET;
121 
122  /* connect to host */
123  if(SOCKET_ERROR == connect(pPipe->cfd, (struct sockaddr*) &sAddress, sizeof(sAddress))) {
124  printf("\nCould not connect to host\n");
125  err = KD_EIO;
126  }
127 
128  }
129 
130  return err;
131 }
132 
134 static CPresult Close( CPhandle hContent )
135 {
136  inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent;
137  CPresult err = 0;
138 
139  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
140 
141  /* close socket */
142  if(SOCKET_ERROR == close(pPipe->cfd)) {
143  printf("\nCould not close client socket\n");
144  err = KD_EIO;
145  }
146 
147  return err;
148 }
149 
151 static CPresult CheckAvailableBytes( CPhandle hContent, CPuint nBytesRequested, CP_CHECKBYTESRESULTTYPE *eResult )
152 {
153  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
154 
155  return KD_EBADF;
156 }
157 
159 static CPresult SetPosition( CPhandle hContent, CPint nOffset, CP_ORIGINTYPE eOrigin)
160 {
161  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
162 
163  return KD_EBADF;
164 }
165 
167 static CPresult GetPosition( CPhandle hContent, CPuint *pPosition)
168 {
169  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
170 
171  return KD_EBADF;
172 }
173 
176 static CPresult Read( CPhandle hContent, CPbyte *pData, CPuint nSize)
177 {
178  inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent;
179  CPresult err = 0;
180  ssize_t count;
181 
182  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
183 
184  count = read(pPipe->cfd, (void*) pData, (size_t) nSize);
185 
186  if(count < nSize) {
187  err = KD_EIO; /* ??? */
188  } else if(count == -1) {
189  err = KD_EIO; /* ??? */
190  }
191 
192  return err;
193 }
194 
207 static CPresult ReadBuffer( CPhandle hContent, CPbyte **ppBuffer, CPuint *nSize, CPbool bForbidCopy)
208 {
209  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
210 
211  return KD_EBADF;
212 }
213 
215 static CPresult ReleaseReadBuffer(CPhandle hContent, CPbyte *pBuffer)
216 {
217  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
218 
219  return KD_EBADF;
220 }
221 
224 static CPresult Write( CPhandle hContent, CPbyte *pData, CPuint nSize)
225 {
226  inet_ContentPipe* pPipe = (inet_ContentPipe*) hContent;
227  CPresult err = 0;
228  ssize_t count;
229 
230  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
231 
232  count = write(pPipe->cfd, (void*) pData, (size_t) nSize);
233 
234  if(count < nSize) {
235  err = KD_EIO; /* ??? */
236  } else if(count == -1) {
237  err = KD_EIO; /* ??? */
238  }
239 
240  return err;
241 }
242 
246 static CPresult GetWriteBuffer( CPhandle hContent, CPbyte **ppBuffer, CPuint nSize)
247 {
248  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
249 
250  return KD_EBADF;
251 }
252 
255 static CPresult WriteBuffer( CPhandle hContent, CPbyte *pBuffer, CPuint nFilledSize)
256 {
257  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
258 
259  return KD_EBADF;
260 }
261 
263 static CPresult RegisterCallback( CPhandle hContent, CPresult (*ClientCallback)(CP_EVENTTYPE eEvent, CPuint iParam))
264 {
265  DEBUG(DEB_LEV_FUNCTION_NAME, "content_pipe_inet:%s \n", __func__);
266 
267  return KD_EBADF;
268 }
269 
271 {
272  inet_ContentPipe* pPipe;
273 
274  pPipe = (inet_ContentPipe*) calloc(1, sizeof(inet_ContentPipe));
275 
276  if(NULL != pPipe) {
277 
278  pPipe->pipe.Open = Open;
279  pPipe->pipe.Close = Close;
280  pPipe->pipe.Create = Create;
281  pPipe->pipe.CheckAvailableBytes = CheckAvailableBytes;
282  pPipe->pipe.SetPosition = SetPosition;
283  pPipe->pipe.GetPosition = GetPosition;
284  pPipe->pipe.Read = Read;
285  pPipe->pipe.ReadBuffer = ReadBuffer;
286  pPipe->pipe.ReleaseReadBuffer = ReleaseReadBuffer;
287  pPipe->pipe.Write = Write;
288  pPipe->pipe.GetWriteBuffer = GetWriteBuffer;
289  pPipe->pipe.WriteBuffer = WriteBuffer;
290  pPipe->pipe.RegisterCallback = RegisterCallback;
291 
292  pPipe->sfd = SOCKET_ERROR; /* Server file descriptor */
293  pPipe->cfd = SOCKET_ERROR; /* Client file descriptor */
294 
295  *ppPipe = (CP_PIPETYPE*) pPipe;
296  }
297 
298  return 0;
299 }
300 

Generated for OpenMAX Bellagio rel. 0.9.3 by  doxygen 1.5.1
SourceForge.net Logo