#ifndef _COMMON_SOCKET_FUNCTIONS_H_
#define _COMMON_SOCKET_FUNCTIONS_H_

#include "includes.h"
#include "lwip/sockets.h"
#include <stdint.h>
#include <string.h>


/*	Create TCP client socket.
	Parameters: pLocalIP	-- Local IP address, if you don't bind local IP, set it as NULL
	Return: success--socket handle; else -1
*/
int CreateTCPClientSock();

/*	Connect to server with server IP, port
	Parameters: pServerIP	-- Server IP address, can not be NULL
				nServerPort -- Server port
				sd			-- Socket
	Return: success--0; else -1
*/
int ClientConnect(int sd, const char * pServerIP, unsigned short nServerPort);

/*	Create UDP client socket.
	Parameters: pLocalIP	-- Local IP address, if you don't bind local IP, set it as NULL
	Return: success--socket handle; else -1
*/
int CreateUDPClientSock();

/* Receive fixed length data from socket
 * Parameters: timeout -- 0 wait forever, >0 timeout limit( second )
 * return : OK >=1; timeout -- 0; socket error -- <0
 * */

















#if 0
/* 	Create TCP socket of server, call socket(), bind(), listen() 
	Parameters: pIP			-- The IP server want to bind, if pIP==NULL, bind INADDR_ANY
				nPort		-- The port server want to bind
				nMaxConnect -- the Max connect the server can accept, use by listen()
	Return: success--socket handle; else -1, can get last error by errno */
int CreateTCPSrvSock( const char *  pIP, unsigned short nPort, int nMaxConnect );

/* 	Create UDP socket of server, call socket(), bind()
	Parameters: pIP			-- The IP server want to bind, if pIP==NULL, bind INADDR_ANY
				nPort		-- The port server want to bind
	Return: success--socket handle; else -1, can get last error by errno */
int CreateUDPSrvSock( const char *  pIP, unsigned short nPort );

/* Accept connect from client, format client IP to string, call accept()
	Parameters: szClientIP	--	If accept suceess, return client IP in this buffer, format: xxx.xxx.xxx.xxx
				pPort		--	If accept suceess, return client port
	Return: success--new socket handle; else -1 
*/
int SockAccept( int Socket, char szClientIP[20], unsigned short *pPort);

/* 
Parameters: timeout--0 wait forever, >0 timeout limit( second )
Return: Socket handle is ready -- >=1; timeout -- 0; socket error -- <0 */
int SockSelect( int sockfd, int timeout );

/*	Create UDP client socket.
	Parameters: pLocalIP	-- Local IP address, if you don't bind local IP, set it as NULL
	Return: success--socket handle; else -1
*/
int CreateUDPClientSock( const char *  pLocalIP );

/*	Connect to server with server IP, port
	Parameters: pServerIP	-- Server IP address, can not be NULL
				nServerPort -- Server port
				sd			-- Socket
	Return: success--0; else -1
*/
int ClientConnect( const char * pServerIP, unsigned short nServerPort, int sd );

/*	Connect to server with server IP, port in time
	Parameters: pServerIP	-- Server IP address, can not be NULL
				nServerPort -- Server port
				sd			-- Socket handle
				nTimeout	-- Timeout(second)
	Return: success--socket handle; else -1
*/
int ClientConnect_Intime( const char * pServerIP, unsigned short nServerPort, 
						   int sd, int nTimeout );

/*	Connect to server with server IP, port
	Parameters: pServerIP	-- Server IP address, can not be NULL
				nServerPort -- Server port
				pLocalIP	-- Local IP address, if you don't bind local IP, set it as NULL
	Return: success--socket handle; else -1
*/
int ConnectTo( const char * pServerIP, unsigned short nServerPort, const char *  pLocalIP );

/*	Connect to server with server IP, port in time
	Parameters: pServerIP	-- Server IP address, can not be NULL
				nServerPort -- Server port
				pLocalIP	-- Local IP address, if you don't bind local IP, set it as NULL
				nTimeout	-- Timeout(second)
	Return: success--socket handle; else -1
*/
int ConnectTo_Intime( const char * pServerIP, unsigned short nServerPort, 
					const char *  pLocalIP,int nTimeout );

int SockRecv(int sd, void *buff, int len);
/* Receive fixed length data from socket
 * Parameters: timeout -- 0 wait forever, >0 timeout limit( second )
 * return : OK >=1; timeout -- 0; socket error -- <0
 * */
int SockRecvFixLen(int sd, void *buff, int fix_len, int timeout);
int SockSendFixLen(int sock, void *buff, int len);

#endif

#endif /*_COMMON_SOCKET_FUNCTIONS_H_*/

