//  Copyright (c) 2007, Monju System Architects
//
//  Released under the Monju-Public copyright license:
//
//  Permission is hereby granted, free of charge, to any person or entity
//  obtaining a copy of this software and associated documentation files
//  (the "Software"), to deal in the Software without restriction,
//  including without limitation the rights to use, copy, modify, merge,
//  publish, distribute, sublicense, and/or sell copies and modifications
//  of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of published, sold, or
//  ownership-transferred versions of the Software. The notices are not
//  required for binary-only releases or binary-only derivatives of the
//  Software, or documentation provided with binary-only releases and
//  binary-only derivatives.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
//  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
//  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
//  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
//  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



#ifndef _PUBLIC_SOCKET_H
#define _PUBLIC_SOCKET_H



//  INCLUDES.
#ifdef _WIN32
	#include <Windows.h>
#else
	#include <arpa/inet.h>
	#include <netinet/in.h>
	#include <sys/socket.h>
#endif



//  TYPE DEFINITIONS.
#ifdef _WIN32

	typedef  int                    socklen_t;
	typedef  SOCKET                 SOCKET_TYPE;
	typedef  struct sockaddr_in     CLIENT_ADDRESS_TYPE;
	typedef  u_long                 PORT_TYPE;

#else //  #ifdef _WIN32

	typedef  int                    SOCKET_TYPE;
	typedef  struct sockaddr_in     CLIENT_ADDRESS_TYPE;
	typedef  int                    PORT_TYPE;

#endif //  #ifdef _WIN32




//  FUNCTION DECLARATIONS.
bool        acceptSocket(SOCKET_TYPE& listener, SOCKET_TYPE* pNewSocket, CLIENT_ADDRESS_TYPE* clientAddress);

void        bindSocket(SOCKET_TYPE& socket, PORT_TYPE port);
void        bindSocket(SOCKET_TYPE& socket, const char* pAddress, PORT_TYPE port);

SOCKET_TYPE createTcpSocket();

void        closeSocket(SOCKET_TYPE& socket);

void        listenSocket(SOCKET_TYPE& socket);

bool        readSocket(SOCKET_TYPE& socket, char* pBuffer, int* pInOutAmount);
bool        readSocket(SOCKET_TYPE& socket, char* pBuffer, int* pInOutAmount, double timeout);
bool        readSocket(SOCKET_TYPE& socket, char* pBuffer, int* pInOutAmount, int timeout);

void        setBlockingSocket(SOCKET_TYPE& theSocket);
void        setNonBlockingSocket(SOCKET_TYPE& socket);

void        setReadTimeoutSocket(SOCKET_TYPE& theSocket, double seconds);

void        writeSocket(SOCKET_TYPE& socket, const char* pString);
void        writeSocket(SOCKET_TYPE& socket, const char* pData, int dataLength);



#endif //  #ifndef _PUBLIC_SOCKET_H



