next up previous contents
Next: 7.4 User Space Implementation: Up: 7 Source Code Presentation Previous: 7.2 Data Structures

7.3 Application Programmers Interface: XUDP Sockets

 

7.3.1 xudpSocket function

struct XUDPHost *xudpSocket(void);

 1 struct XUDPHost *xudpSocket(void)
 2 {
 3     struct XUDPHost     *xudphost;
 4     int                 un_len, temp;
 5     struct sockaddr_un  xudp_addr;
 6     
 7     if ( !(xudphost=malloc(sizeof(struct XUDPHost))))
 8         xudpError("xudpSocket: Allocating RAM");
 9 
10     bzero((char *) &xudp_addr, sizeof(xudp_addr));
11     if ( (xudphost->fdControl=socket(AF_UNIX, SOCK_STREAM, 0)) <0)
12         xudpError("xudp: socket(UNIX)");   /* Opens a UNIX Stream socket */
13     if ( (xudphost->fdAudio=socket(AF_UNIX, SOCK_STREAM, 0)) <0)
14         xudpError("xudp: socket(UNIX)");   /* Opens a UNIX Stream socket */
15     if ( (xudphost->fdVideo=socket(AF_UNIX, SOCK_STREAM, 0)) <0)
16         xudpError("xudp: socket(UNIX)");   /* Opens a UNIX Stream socket */
17     
18                       /* Fill out address structure for server */
19     
20     xudp_addr.sun_family=AF_UNIX;         /* AF_UNIX is the UNIX Domain
21                                              Protocol Family.*/  
22     
23     /*** Control Port **********/
24     strcpy(xudp_addr.sun_path, CONTROL_UNIX); /* Server stream path */
25     
26     un_len=strlen(xudp_addr.sun_path) + sizeof(xudp_addr.sun_family);
27     
28     if (connect(xudphost->fdControl, (struct sockaddr *) &xudp_addr,
29                 un_len) < 0) 
30         xudpError("xudpSocket: Could not connect");
31     
32     /*** Audio Port **********/
33     strcpy(xudp_addr.sun_path, AUDIO_UNIX); /* Server stream path */
34     
35     un_len=strlen(xudp_addr.sun_path) + sizeof(xudp_addr.sun_family);
36     
37     if (connect(xudphost->fdAudio, (struct sockaddr *) &xudp_addr,
38                 un_len) < 0) 
39         xudpError("xudpSocket: Could not connect");
40 
41     /*** Video Port **********/
42     strcpy(xudp_addr.sun_path, VIDEO_UNIX); /* Server stream path */
43     
44     un_len=strlen(xudp_addr.sun_path) + sizeof(xudp_addr.sun_family);
45     
46     if (connect(xudphost->fdVideo, (struct sockaddr *) &xudp_addr,
47                 un_len) < 0) 
48         xudpError("xudpSocket: Could not connect");
49 
50     /*** Socket buffer sizes *************/
51 
52     temp=SOCKBUFSIZE;  /* Set up maximum socket-level buffers */
53     if (setsockopt(xudphost->fdControl, SOL_SOCKET, SO_SNDBUF,
54                    &temp, sizeof(int))<0) xudpError("xudpSocket");  
55     if (setsockopt(xudphost->fdControl, SOL_SOCKET, SO_RCVBUF,
56                    &temp, sizeof(int))<0) xudpError("xudpSocket");  
57     if (setsockopt(xudphost->fdAudio, SOL_SOCKET, SO_SNDBUF,
58                    &temp, sizeof(int))<0) xudpError("xudpSocket");  
59     if (setsockopt(xudphost->fdAudio, SOL_SOCKET, SO_RCVBUF,
60                    &temp, sizeof(int))<0) xudpError("xudpSocket");  
61     if (setsockopt(xudphost->fdVideo, SOL_SOCKET, SO_SNDBUF,
62                    &temp, sizeof(int))<0) xudpError("xudpSocket");  
63     if (setsockopt(xudphost->fdVideo, SOL_SOCKET, SO_RCVBUF,
64                    &temp, sizeof(int))<0) xudpError("xudpSocket");
65 
66     return xudphost;
67 }

7.3.2 xudpBind function

int xudpBind(struct XUDPHost *xudphost, u_short port);

 1 int xudpBind(struct XUDPHost *xudphost, u_short port)
 2 {
 3     xudpSendFunction(xudphost, F_BIND, (char *) &port);
 4 
 5     return 0;
 6 }

7.3.3 xudpListen function

int xudpListen(struct XUDPHost *xudphost);

 1 int xudpListen(struct XUDPHost *xudphost)
 2 {
 3     xudpSendFunction(xudphost, F_LISTEN, NULL);
 4     return 0;
 5 }

7.3.4 xudpAccept function

int xudpAccept(struct XUDPHost *xudphost);

 1 int xudpAccept(struct XUDPHost *xudphost)
 2 {
 3     xudpSendFunction(xudphost, F_ACCEPT, NULL);
 4     
 5     if (xudpGetFunction(xudphost)==F_ERROR)
 6         return xudpGetFunction(xudphost);
 7 
 8     streamRead(xudphost->fdControl, (char *) &xudphost->hostaddr,
 9                sizeof(struct sockaddr_in));
10 
11     xudphost->pHostent=gethostbyaddr((char *)&xudphost->hostaddr.sin_addr,
12                                      sizeof(struct in_addr), AF_INET); 
13 
14     return 0;
15 }

7.3.5 xudpConnect function

int xudpConnect(struct XUDPHost *xudphost, char *fqdn, u_short port);

 1 int xudpConnect(struct XUDPHost *xudphost, char *fqdn, u_short port)
 2 {
 3     xudphost->pHostent=gethostbyname(fqdn);
 4                                       /* Given any DNS or /etc/hosts
 5                                          registered hostname, gethostbyname()
 6                                          will return useful information,
 7                                          including the 32-bit IP that we need
 8                                          */ 
 9     if (xudphost->pHostent==NULL)
10         xudpError("Trying to resolve hostname");
11                                  /* Make sure our hostname is valid */
12 
13     memcpy(&xudphost->hostaddr.sin_addr, xudphost->pHostent->h_addr_list[0],
14            sizeof(struct in_addr)); 
15     xudphost->hostaddr.sin_family=AF_INET;
16     if (port==0) xudphost->hostaddr.sin_port=htons(SERVER_PORT);
17     else xudphost->hostaddr.sin_port=htons(port);
18 
19     xudpSendFunction(xudphost, F_CONNECT, NULL);
20 
21     if (xudpGetFunction(xudphost)==F_ERROR)
22         return xudpGetFunction(xudphost);
23     streamRead(xudphost->fdControl, (char *) &xudphost->hostaddr,
24                sizeof(struct sockaddr_in));
25 
26     return 0;
27 }

7.3.6 xudpSend function

int xudpSend(struct XUDPHost *xudphost, char *buffer, u_short bytes, u_char type, u_long timeout);

 1 int xudpSend(struct XUDPHost *xudphost, char *buffer, u_short bytes,
 2              u_char type, u_long timeout)
 3 {
 4     Timestamp tto;
 5     int       result;
 6 
 7     if ((timeout/GRANULARITY)>RELIABLE) tto=RELIABLE;
 8     else tto=timeout/GRANULARITY;
 9 
10     /* CAUTION: Improve Error Handling! */
11     switch (type)
12     {
13 /*    case AUDIO:
14         xudpSendFunction(xudphost, F_SENDAUDIO, (char *)&bytes);
15         if (streamWrite(xudphost->fdControl, (char *)&tto,
16                         sizeof(tto))!=sizeof(tto))
17             xudpError("UNIX Stream Failed");
18         if (streamWrite(xudphost->fdAudio, buffer, bytes)!=bytes)
19             xudpError("UNIX Stream Failed");
20         break;*/
21     case VIDEO:
22         if ((result=send(xudphost->fdVideo, buffer, bytes, 0))>0)
23         {
24             if (result!=bytes)
25                 xudpError("UNIX Stream Failed");
26             xudpSendFunction(xudphost, F_SENDVIDEO, (char *)&bytes);
27             if (streamWrite(xudphost->fdControl, (char *)&tto,
28                             sizeof(tto))!=sizeof(tto))
29                 xudpError("UNIX Stream Failed");
30         }
31         else return -1;
32 
33         break;
34     default: return 0; break;
35     }
36     
37 
38     return bytes;
39 }

7.3.7 xudpRecv function

int xudpRecv(struct XUDPHost *xudphost, char *buffer, u_short *bytes, u_char *type);

 1 int xudpRecv(struct XUDPHost *xudphost, char *buffer, u_short *bytes,
 2              u_char *type) 
 3 {
 4     char    cmd;
 5     u_short length;
 6     
 7     cmd=xudpGetFunction(xudphost);
 8     switch (cmd)
 9     {
10     case F_RECVAUDIO: break; 
11     case F_RECVVIDEO: 
12         streamRead(xudphost->fdControl, (char *)&length, sizeof(length));
13         streamRead(xudphost->fdVideo, buffer, length);
14         memcpy(bytes, &length, sizeof(u_short));
15         *type=VIDEO;
16         break;
17     default:    return 0; break;
18     }
19     return length;
20 }

7.3.8 xudpClose function

int xudpClose(struct XUDPHost *xudphost);

 1 int xudpClose(struct XUDPHost *xudphost)
 2 {
 3     xudpSendFunction(xudphost, F_CLOSE, NULL);
 4     xudpGetFunction(xudphost);
 5     close(xudphost->fdControl);
 6     close(xudphost->fdAudio);
 7     close(xudphost->fdVideo);
 8     free(xudphost);
 9     return 0;
10 }



Mike Andrews
Wed Mar 19 16:07:58 EST 1997