13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DESOCKET_H
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DESOCKET_H
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Utility Library
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Socket abstraction.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Socket API is thread-safe except to:
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *  - listen()
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *  - connect()
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *  - destroy()
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.h"
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_BEGIN_EXTERN_C
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* Socket types. */
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef struct deSocket_s			deSocket;
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef struct deSocketAddress_s	deSocketAddress;
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketFamily_e
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETFAMILY_INET4 = 0,
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETFAMILY_INET6,
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETFAMILY_LAST
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketFamily;
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketType_e
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETTYPE_STREAM	= 0,
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETTYPE_DATAGRAM,
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETTYPE_LAST
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketType;
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketProtocol_e
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETPROTOCOL_TCP = 0,
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETPROTOCOL_UDP,
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETPROTOCOL_LAST
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketProtocol;
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketFlag_e
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKET_KEEPALIVE		= (1<<0),
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKET_NODELAY		= (1<<1),
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKET_NONBLOCKING	= (1<<2),
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKET_CLOSE_ON_EXEC	= (1<<3)
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketFlag;
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* \todo [2012-07-09 pyry] Separate closed bits for send and receive channels. */
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketState_e
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETSTATE_CLOSED					= 0,
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETSTATE_CONNECTED				= 1,
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETSTATE_LISTENING				= 2,
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETSTATE_DISCONNECTED				= 3,
793fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETSTATE_LAST
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketState;
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketResult_e
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_SUCCESS					= 0,
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_WOULD_BLOCK				= 1,
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_CONNECTION_CLOSED		= 2,
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_CONNECTION_TERMINATED	= 3,
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_ERROR					= 4,
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETRESULT_LAST
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketResult;
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum deSocketChannel_e
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETCHANNEL_RECEIVE	= (1<<0),
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETCHANNEL_SEND		= (1<<1),
983fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_SOCKETCHANNEL_BOTH		= DE_SOCKETCHANNEL_RECEIVE|DE_SOCKETCHANNEL_SEND
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} deSocketChannel;
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* Socket API, similar to Berkeley sockets. */
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocketAddress*	deSocketAddress_create		(void);
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid				deSocketAddress_destroy		(deSocketAddress* address);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1073c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocketAddress_setFamily	(deSocketAddress* address, deSocketFamily family);
1083c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocketFamily		deSocketAddress_getFamily	(const deSocketAddress* address);
1093c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocketAddress_setType		(deSocketAddress* address, deSocketType type);
1103c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocketType		deSocketAddress_getType		(const deSocketAddress* address);
1113c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocketAddress_setProtocol	(deSocketAddress* address, deSocketProtocol protocol);
1123c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocketProtocol	deSocketAddress_getProtocol	(const deSocketAddress* address);
1133c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocketAddress_setPort		(deSocketAddress* address, int port);
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint					deSocketAddress_getPort		(const deSocketAddress* address);
1153c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocketAddress_setHost		(deSocketAddress* address, const char* host);
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst char*			deSocketAddress_getHost		(const deSocketAddress* address);
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1183c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocket*			deSocket_create				(void);
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid				deSocket_destroy			(deSocket* socket);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1213c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocketState		deSocket_getState			(const deSocket* socket);
1223c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeUint32			deSocket_getOpenChannels	(const deSocket* socket);
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1243c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocket_setFlags			(deSocket* socket, deUint32 flags);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1263c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocket_listen				(deSocket* socket, const deSocketAddress* address);
1273c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeSocket*			deSocket_accept				(deSocket* socket, deSocketAddress* clientAddress);
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocket_connect			(deSocket* socket, const deSocketAddress* address);
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1313c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocket_shutdown			(deSocket* socket, deUint32 channels);
1323c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool				deSocket_close				(deSocket* socket);
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
134745d7c616351405cfb6d2a7133d261eb4989d626Jarkko PöyrydeSocketResult		deSocket_send				(deSocket* socket, const void* buf, size_t bufSize, size_t* numSent);
135745d7c616351405cfb6d2a7133d261eb4989d626Jarkko PöyrydeSocketResult		deSocket_receive			(deSocket* socket, void* buf, size_t bufSize, size_t* numReceived);
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* Utilities. */
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst char*			deGetSocketFamilyName		(deSocketFamily family);
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst char*			deGetSocketResultName		(deSocketResult result);
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1423c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_END_EXTERN_C
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry#endif /* _DESOCKET_H */
145