1 #ifndef _DESOCKET_H
2 #define _DESOCKET_H
3 /*-------------------------------------------------------------------------
4  * drawElements Utility Library
5  * ----------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Socket abstraction.
24  *
25  * Socket API is thread-safe except to:
26  *  - listen()
27  *  - connect()
28  *  - destroy()
29  *//*--------------------------------------------------------------------*/
30 
31 #include "deDefs.h"
32 
33 DE_BEGIN_EXTERN_C
34 
35 /* Socket types. */
36 typedef struct deSocket_s			deSocket;
37 typedef struct deSocketAddress_s	deSocketAddress;
38 
39 typedef enum deSocketFamily_e
40 {
41 	DE_SOCKETFAMILY_INET4 = 0,
42 	DE_SOCKETFAMILY_INET6,
43 
44 	DE_SOCKETFAMILY_LAST
45 } deSocketFamily;
46 
47 typedef enum deSocketType_e
48 {
49 	DE_SOCKETTYPE_STREAM	= 0,
50 	DE_SOCKETTYPE_DATAGRAM,
51 
52 	DE_SOCKETTYPE_LAST
53 } deSocketType;
54 
55 typedef enum deSocketProtocol_e
56 {
57 	DE_SOCKETPROTOCOL_TCP = 0,
58 	DE_SOCKETPROTOCOL_UDP,
59 
60 	DE_SOCKETPROTOCOL_LAST
61 } deSocketProtocol;
62 
63 typedef enum deSocketFlag_e
64 {
65 	DE_SOCKET_KEEPALIVE		= (1<<0),
66 	DE_SOCKET_NODELAY		= (1<<1),
67 	DE_SOCKET_NONBLOCKING	= (1<<2),
68 	DE_SOCKET_CLOSE_ON_EXEC	= (1<<3)
69 } deSocketFlag;
70 
71 /* \todo [2012-07-09 pyry] Separate closed bits for send and receive channels. */
72 
73 typedef enum deSocketState_e
74 {
75 	DE_SOCKETSTATE_CLOSED					= 0,
76 	DE_SOCKETSTATE_CONNECTED				= 1,
77 	DE_SOCKETSTATE_LISTENING				= 2,
78 	DE_SOCKETSTATE_DISCONNECTED				= 3,
79 
80 	DE_SOCKETSTATE_LAST
81 } deSocketState;
82 
83 typedef enum deSocketResult_e
84 {
85 	DE_SOCKETRESULT_SUCCESS					= 0,
86 	DE_SOCKETRESULT_WOULD_BLOCK				= 1,
87 	DE_SOCKETRESULT_CONNECTION_CLOSED		= 2,
88 	DE_SOCKETRESULT_CONNECTION_TERMINATED	= 3,
89 	DE_SOCKETRESULT_ERROR					= 4,
90 
91 	DE_SOCKETRESULT_LAST
92 } deSocketResult;
93 
94 typedef enum deSocketChannel_e
95 {
96 	DE_SOCKETCHANNEL_RECEIVE	= (1<<0),
97 	DE_SOCKETCHANNEL_SEND		= (1<<1),
98 
99 	DE_SOCKETCHANNEL_BOTH		= DE_SOCKETCHANNEL_RECEIVE|DE_SOCKETCHANNEL_SEND
100 } deSocketChannel;
101 
102 /* Socket API, similar to Berkeley sockets. */
103 
104 deSocketAddress*	deSocketAddress_create		(void);
105 void				deSocketAddress_destroy		(deSocketAddress* address);
106 
107 deBool				deSocketAddress_setFamily	(deSocketAddress* address, deSocketFamily family);
108 deSocketFamily		deSocketAddress_getFamily	(const deSocketAddress* address);
109 deBool				deSocketAddress_setType		(deSocketAddress* address, deSocketType type);
110 deSocketType		deSocketAddress_getType		(const deSocketAddress* address);
111 deBool				deSocketAddress_setProtocol	(deSocketAddress* address, deSocketProtocol protocol);
112 deSocketProtocol	deSocketAddress_getProtocol	(const deSocketAddress* address);
113 deBool				deSocketAddress_setPort		(deSocketAddress* address, int port);
114 int					deSocketAddress_getPort		(const deSocketAddress* address);
115 deBool				deSocketAddress_setHost		(deSocketAddress* address, const char* host);
116 const char*			deSocketAddress_getHost		(const deSocketAddress* address);
117 
118 deSocket*			deSocket_create				(void);
119 void				deSocket_destroy			(deSocket* socket);
120 
121 deSocketState		deSocket_getState			(const deSocket* socket);
122 deUint32			deSocket_getOpenChannels	(const deSocket* socket);
123 
124 deBool				deSocket_setFlags			(deSocket* socket, deUint32 flags);
125 
126 deBool				deSocket_listen				(deSocket* socket, const deSocketAddress* address);
127 deSocket*			deSocket_accept				(deSocket* socket, deSocketAddress* clientAddress);
128 
129 deBool				deSocket_connect			(deSocket* socket, const deSocketAddress* address);
130 
131 deBool				deSocket_shutdown			(deSocket* socket, deUint32 channels);
132 deBool				deSocket_close				(deSocket* socket);
133 
134 deSocketResult		deSocket_send				(deSocket* socket, const void* buf, int bufSize, int* numSent);
135 deSocketResult		deSocket_receive			(deSocket* socket, void* buf, int bufSize, int* numReceived);
136 
137 /* Utilities. */
138 
139 const char*			deGetSocketFamilyName		(deSocketFamily family);
140 const char*			deGetSocketResultName		(deSocketResult result);
141 
142 DE_END_EXTERN_C
143 
144 #endif /* _DESOCKET_H */
145