Line data Source code
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 0 : int nr_socket_create_int(void *obj, nr_socket_vtbl *vtbl, nr_socket **sockp)
44 : {
45 : int _status;
46 0 : nr_socket *sock=0;
47 :
48 0 : if(!(sock=RCALLOC(sizeof(nr_socket))))
49 0 : ABORT(R_NO_MEMORY);
50 :
51 0 : assert(vtbl->version >= 1 && vtbl->version <= 2);
52 0 : if (vtbl->version < 1 || vtbl->version > 2)
53 0 : ABORT(R_INTERNAL);
54 :
55 0 : sock->obj=obj;
56 0 : sock->vtbl=vtbl;
57 :
58 0 : *sockp=sock;
59 :
60 0 : _status=0;
61 : abort:
62 0 : return(_status);
63 : }
64 :
65 0 : int nr_socket_destroy(nr_socket **sockp)
66 : {
67 : nr_socket *sock;
68 :
69 0 : if(!sockp || !*sockp)
70 0 : return(0);
71 :
72 :
73 0 : sock=*sockp;
74 0 : *sockp=0;
75 :
76 0 : CHECK_DEFINED(destroy);
77 :
78 0 : assert(sock->vtbl);
79 0 : if (sock->vtbl)
80 0 : sock->vtbl->destroy(&sock->obj);
81 :
82 0 : RFREE(sock);
83 :
84 0 : return(0);
85 : }
86 :
87 0 : int nr_socket_sendto(nr_socket *sock,const void *msg, size_t len, int flags,
88 : nr_transport_addr *addr)
89 : {
90 0 : CHECK_DEFINED(ssendto);
91 0 : return sock->vtbl->ssendto(sock->obj,msg,len,flags,addr);
92 : }
93 :
94 0 : 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 0 : CHECK_DEFINED(srecvfrom);
98 0 : return sock->vtbl->srecvfrom(sock->obj, buf, maxlen, len, flags, addr);
99 : }
100 :
101 0 : int nr_socket_getfd(nr_socket *sock, NR_SOCKET *fd)
102 : {
103 0 : CHECK_DEFINED(getfd);
104 0 : return sock->vtbl->getfd(sock->obj, fd);
105 : }
106 :
107 0 : int nr_socket_getaddr(nr_socket *sock, nr_transport_addr *addrp)
108 : {
109 0 : CHECK_DEFINED(getaddr);
110 0 : return sock->vtbl->getaddr(sock->obj, addrp);
111 : }
112 :
113 0 : int nr_socket_close(nr_socket *sock)
114 : {
115 0 : CHECK_DEFINED(close);
116 0 : return sock->vtbl->close(sock->obj);
117 : }
118 :
119 0 : int nr_socket_connect(nr_socket *sock, nr_transport_addr *addr)
120 : {
121 0 : CHECK_DEFINED(connect);
122 0 : return sock->vtbl->connect(sock->obj, addr);
123 : }
124 :
125 0 : int nr_socket_write(nr_socket *sock,const void *msg, size_t len, size_t *written, int flags)
126 : {
127 0 : CHECK_DEFINED(swrite);
128 0 : return sock->vtbl->swrite(sock->obj, msg, len, written);
129 : }
130 :
131 :
132 0 : int nr_socket_read(nr_socket *sock,void * restrict buf, size_t maxlen,
133 : size_t *len, int flags)
134 : {
135 0 : CHECK_DEFINED(sread);
136 0 : return sock->vtbl->sread(sock->obj, buf, maxlen, len);
137 : }
138 :
139 0 : int nr_socket_listen(nr_socket *sock, int backlog)
140 : {
141 0 : assert(sock->vtbl->version >=2 );
142 0 : CHECK_DEFINED(listen);
143 0 : return sock->vtbl->listen(sock->obj, backlog);
144 : }
145 :
146 0 : int nr_socket_accept(nr_socket *sock, nr_transport_addr *addrp, nr_socket **sockp)
147 : {
148 0 : assert(sock->vtbl->version >= 2);
149 0 : CHECK_DEFINED(accept);
150 0 : return sock->vtbl->accept(sock->obj, addrp, sockp);
151 : }
152 :
153 :
154 0 : int nr_socket_factory_create_int(void *obj,
155 : nr_socket_factory_vtbl *vtbl, nr_socket_factory **factorypp)
156 : {
157 : int _status;
158 0 : nr_socket_factory *factoryp=0;
159 :
160 0 : if(!(factoryp=RCALLOC(sizeof(nr_socket_factory))))
161 0 : ABORT(R_NO_MEMORY);
162 :
163 0 : factoryp->obj = obj;
164 0 : factoryp->vtbl = vtbl;
165 :
166 0 : *factorypp = factoryp;
167 :
168 0 : _status=0;
169 : abort:
170 0 : return(_status);
171 : }
172 :
173 0 : int nr_socket_factory_destroy(nr_socket_factory **factorypp)
174 : {
175 : nr_socket_factory *factoryp;
176 :
177 0 : if (!factorypp || !*factorypp)
178 0 : return (0);
179 :
180 0 : factoryp = *factorypp;
181 0 : *factorypp = NULL;
182 0 : factoryp->vtbl->destroy(&factoryp->obj);
183 0 : RFREE(factoryp);
184 0 : return (0);
185 : }
186 :
187 0 : int nr_socket_factory_create_socket(nr_socket_factory *factory, nr_transport_addr *addr, nr_socket **sockp)
188 : {
189 0 : return factory->vtbl->create_socket(factory->obj, addr, sockp);
190 : }
191 :
|