|
1 /* |
|
2 Copyright (c) 2007, Adobe Systems, Incorporated |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions are |
|
7 met: |
|
8 |
|
9 * Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 |
|
12 * Redistributions in binary form must reproduce the above copyright |
|
13 notice, this list of conditions and the following disclaimer in the |
|
14 documentation and/or other materials provided with the distribution. |
|
15 |
|
16 * Neither the name of Adobe Systems, Network Resonance nor the names of its |
|
17 contributors may be used to endorse or promote products derived from |
|
18 this software without specific prior written permission. |
|
19 |
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 |
|
33 |
|
34 |
|
35 static char *RCSSTRING __UNUSED__="$Id: nr_socket.c,v 1.2 2008/04/28 17:59:02 ekr Exp $"; |
|
36 |
|
37 #include <assert.h> |
|
38 #include <nr_api.h> |
|
39 #include "nr_socket.h" |
|
40 #include "local_addr.h" |
|
41 |
|
42 #define CHECK_DEFINED(f) assert(sock->vtbl->f); if (!sock->vtbl->f) ERETURN(R_INTERNAL); |
|
43 int nr_socket_create_int(void *obj, nr_socket_vtbl *vtbl, nr_socket **sockp) |
|
44 { |
|
45 int _status; |
|
46 nr_socket *sock=0; |
|
47 |
|
48 if(!(sock=RCALLOC(sizeof(nr_socket)))) |
|
49 ABORT(R_NO_MEMORY); |
|
50 |
|
51 assert(vtbl->version == 1); |
|
52 if (vtbl->version != 1) |
|
53 ABORT(R_INTERNAL); |
|
54 |
|
55 sock->obj=obj; |
|
56 sock->vtbl=vtbl; |
|
57 |
|
58 *sockp=sock; |
|
59 |
|
60 _status=0; |
|
61 abort: |
|
62 return(_status); |
|
63 } |
|
64 |
|
65 int nr_socket_destroy(nr_socket **sockp) |
|
66 { |
|
67 nr_socket *sock; |
|
68 |
|
69 if(!sockp || !*sockp) |
|
70 return(0); |
|
71 |
|
72 |
|
73 sock=*sockp; |
|
74 *sockp=0; |
|
75 |
|
76 CHECK_DEFINED(destroy); |
|
77 |
|
78 assert(sock->vtbl); |
|
79 if (sock->vtbl) |
|
80 sock->vtbl->destroy(&sock->obj); |
|
81 |
|
82 RFREE(sock); |
|
83 |
|
84 return(0); |
|
85 } |
|
86 |
|
87 int nr_socket_sendto(nr_socket *sock,const void *msg, size_t len, int flags, |
|
88 nr_transport_addr *addr) |
|
89 { |
|
90 CHECK_DEFINED(ssendto); |
|
91 return sock->vtbl->ssendto(sock->obj,msg,len,flags,addr); |
|
92 } |
|
93 |
|
94 int nr_socket_recvfrom(nr_socket *sock,void * restrict buf, size_t maxlen, |
|
95 size_t *len, int flags, nr_transport_addr *addr) |
|
96 { |
|
97 CHECK_DEFINED(srecvfrom); |
|
98 return sock->vtbl->srecvfrom(sock->obj, buf, maxlen, len, flags, addr); |
|
99 } |
|
100 |
|
101 int nr_socket_getfd(nr_socket *sock, NR_SOCKET *fd) |
|
102 { |
|
103 CHECK_DEFINED(getfd); |
|
104 return sock->vtbl->getfd(sock->obj, fd); |
|
105 } |
|
106 |
|
107 int nr_socket_getaddr(nr_socket *sock, nr_transport_addr *addrp) |
|
108 { |
|
109 CHECK_DEFINED(getaddr); |
|
110 return sock->vtbl->getaddr(sock->obj, addrp); |
|
111 } |
|
112 |
|
113 int nr_socket_close(nr_socket *sock) |
|
114 { |
|
115 CHECK_DEFINED(close); |
|
116 return sock->vtbl->close(sock->obj); |
|
117 } |
|
118 |
|
119 int nr_socket_connect(nr_socket *sock, nr_transport_addr *addr) |
|
120 { |
|
121 CHECK_DEFINED(connect); |
|
122 return sock->vtbl->connect(sock->obj, addr); |
|
123 } |
|
124 |
|
125 int nr_socket_write(nr_socket *sock,const void *msg, size_t len, size_t *written, int flags) |
|
126 { |
|
127 CHECK_DEFINED(swrite); |
|
128 return sock->vtbl->swrite(sock->obj, msg, len, written); |
|
129 } |
|
130 |
|
131 |
|
132 int nr_socket_read(nr_socket *sock,void * restrict buf, size_t maxlen, |
|
133 size_t *len, int flags) |
|
134 { |
|
135 CHECK_DEFINED(sread); |
|
136 return sock->vtbl->sread(sock->obj, buf, maxlen, len); |
|
137 } |