update dev300-m57
[ooovba.git] / sal / workben / t_layer.c
blobe286bcfb78e19340be71ec30599741dfc33a3b81
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_layer.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/types.h>
32 #include <rtl/alloc.h>
33 #include <osl/diagnose.h>
34 #include <osl/interlck.h>
35 #ifndef _OSL_ISOCKET_H_
36 #include <osl/isocket.h>
37 #endif
39 /*========================================================================
41 * osl/debug.c
43 *======================================================================*/
45 * __osl_debug_create.
47 static oslSocketResult SAL_CALL __osl_debug_create (
48 oslSocketDescriptor *socket,
49 oslAddrFamily family,
50 oslSocketType type,
51 oslProtocol protocol)
53 oslSocketResult result;
55 if (!(socket->m_lower))
57 oslSocketDescriptor *lower;
58 lower = osl_socket_createSocketLayer (osl_socket_getSocketMethods());
59 osl_socket_pushSocketLayer (socket, lower);
62 result = (socket->m_lower->m_methods->m_create)(
63 socket->m_lower, family, type, protocol);
64 if (!(result == osl_Socket_Ok))
65 return (result);
67 if (!(socket->m_private))
69 /* ... */
71 return (osl_Socket_Ok);
75 * __osl_debug_delete.
77 static void SAL_CALL __osl_debug_delete (oslSocketDescriptor *socket)
79 if (socket->m_private)
81 /* ... */
83 if (socket->m_lower)
85 (socket->m_lower->m_methods->m_delete)(socket->m_lower);
86 osl_socket_deleteSocketLayer (socket->m_lower);
91 * __osl_debug_getOption.
93 static oslSocketResult SAL_CALL __osl_debug_getOption (
94 oslSocketDescriptor *socket,
95 oslSocketOptionLevel level,
96 oslSocketOption option,
97 void *buffer,
98 sal_uInt32 length)
100 if (level == osl_Socket_LevelSocketLayer)
102 /* ... */
103 return (osl_Socket_Ok);
106 return (socket->m_lower->m_methods->m_getoption)(
107 socket->m_lower, level, option, buffer, length);
111 * __osl_debug_setOption.
113 static oslSocketResult SAL_CALL __osl_debug_setOption (
114 oslSocketDescriptor *socket,
115 oslSocketOptionLevel level,
116 oslSocketOption option,
117 const void *buffer,
118 sal_uInt32 length)
120 if (level == osl_Socket_LevelSocketLayer)
122 /* ... */
123 return (osl_Socket_Ok);
126 return (socket->m_lower->m_methods->m_setoption)(
127 socket->m_lower, level, option, buffer, length);
131 * __osl_debug_accept.
133 static oslSocketDescriptor* SAL_CALL __osl_debug_accept (
134 oslSocketDescriptor *socket, oslSocketAddr *fromaddr)
136 oslSocketDescriptor *lower;
137 lower = (socket->m_lower->m_methods->m_accept)(socket->m_lower, fromaddr);
138 if (lower)
140 oslSocketDescriptor *upper;
141 upper = osl_socket_createSocketLayer (socket->m_methods);
142 osl_socket_pushSocketLayer (upper, lower);
143 return (upper);
145 return ((oslSocketDescriptor*)NULL);
149 * __osl_debug_connect.
151 static oslSocketResult SAL_CALL __osl_debug_connect (
152 oslSocketDescriptor *socket, oslSocketAddr addr)
154 return (socket->m_lower->m_methods->m_connect)(socket->m_lower, addr);
158 * __osl_debug_close.
160 static void SAL_CALL __osl_debug_close (oslSocketDescriptor *socket)
162 (socket->m_lower->m_methods->m_close)(socket->m_lower);
166 * __osl_shutdown.
168 static void SAL_CALL __osl_debug_shutdown (
169 oslSocketDescriptor *socket, oslSocketDirection direction)
171 (socket->m_lower->m_methods->m_shutdown)(socket->m_lower, direction);
175 * __osl_debug_methods.
177 static oslSocketMethods __osl_debug_methods =
179 NULL /* initialized on demand */
183 * osl_debug_getSocketMethods.
185 const oslSocketMethods* SAL_CALL osl_debug_getSocketMethods (void)
187 static oslInterlockedCount nonce = 0;
188 if (osl_incrementInterlockedCount (&nonce) == 1)
190 const oslSocketMethods *methods = osl_socket_getDefaultSocketMethods();
191 __osl_debug_methods = *methods;
193 __osl_debug_methods.m_create = __osl_debug_create;
194 __osl_debug_methods.m_delete = __osl_debug_delete;
196 __osl_debug_methods.m_getoption = __osl_debug_getOption;
197 __osl_debug_methods.m_setoption = __osl_debug_setOption;
199 __osl_debug_methods.m_accept = __osl_debug_accept;
200 __osl_debug_methods.m_connect = __osl_debug_connect;
201 __osl_debug_methods.m_close = __osl_debug_close;
203 __osl_debug_methods.m_shutdown = __osl_debug_shutdown;
205 osl_incrementInterlockedCount (&nonce);
208 osl_decrementInterlockedCount (&nonce);
209 return (&__osl_debug_methods);
212 /*========================================================================
214 * osl/socket.c (physical, bottom layer).
216 *======================================================================*/
217 #if 0 /* NYI */
219 static oslSocketResult SAL_CALL __osl_socket_create (
220 oslSocketDescriptor *socket,
221 oslAddrFamily family,
222 oslSocketType type,
223 oslProtocol protocol)
225 #if OSL_DEBUG_LEVEL > 1
226 const oslSocketMethods *debug = osl_debug_getSocketMethods();
227 if (!(socket->m_upper || socket->m_upper->m_methods == debug))
229 oslSocketDescriptor *upper;
231 upper = osl_socket_createSocketLayer (debug);
232 osl_socket_pushSocketLayer (upper, socket);
234 return (upper->m_methods->m_create)(upper, family, type, protocol);
236 #endif /* OSL_DEBUG_LEVEL */
237 if (!(socket->m_private))
239 /* ... */
240 socket->m_private = osl_createSocket (family, type, protocol);
242 return (osl_Socket_Ok);
245 #endif /* NYI */
247 /*========================================================================
249 * main.
251 *======================================================================*/
252 int SAL_CALL main (int argc, char **argv)
254 oslSocketDescriptor *socket;
256 socket = osl_socket_createSocketLayer (osl_debug_getSocketMethods());
257 if (socket)
259 oslSocketDescriptor *connection;
260 oslSocketResult result;
261 oslSocketAddr addr, from;
262 sal_Int32 option = 1;
264 result = osl_socket_create (
265 socket,
266 osl_Socket_FamilyInet,
267 osl_Socket_TypeStream,
268 osl_Socket_ProtocolIp);
269 OSL_ASSERT(result == osl_Socket_Ok);
271 result = osl_socket_setOption (
272 socket,
273 osl_Socket_LevelSocket,
274 osl_Socket_OptionReuseAddr,
275 &option, sizeof(option));
276 OSL_ASSERT(result == osl_Socket_Ok);
278 addr = osl_createEmptySocketAddr (osl_Socket_FamilyInet);
279 osl_setInetPortOfSocketAddr (addr, 7777);
281 result = osl_socket_bind (socket, addr);
282 OSL_ASSERT(result == osl_Socket_Ok);
284 result = osl_socket_listen (socket, 1);
285 OSL_ASSERT(result == osl_Socket_Ok);
287 connection = osl_socket_accept (socket, &from);
288 if (connection)
290 sal_Char buffer[1024];
291 sal_Int32 k, n = sizeof(buffer);
292 oslSocketMsgFlag flag = osl_Socket_MsgNormal;
294 while ((k = osl_socket_recv (connection, buffer, n, flag)) > 0)
296 if (osl_socket_send (connection, buffer, k, flag) < 0)
297 break;
300 osl_socket_close (connection);
301 osl_socket_delete (connection);
303 osl_socket_deleteSocketLayer (connection);
306 osl_socket_close (socket);
307 osl_socket_delete (socket);
309 osl_socket_deleteSocketLayer (socket);
312 return 0;