#ifndef DEBUGSOCK_INCLUDE_H
#define DEBUGSOCK_INCLUDE_H
#ifndef RELEASE_VERSION

#include <winsock2.h>
#define BUFFERSIZE 8192
#define SECTIMEOUTLISTEN 1


#include "PFrogConsole.h"
#include <PocketFrog/PocketFrog.h>

using namespace Frog;

class DebugSock
{
private:

	timeval _listenTimeout;
	SOCKET ListenSocket;
	SOCKET AcceptSocket;
	SOCKADDR_IN InternetAddr;
	FD_SET Writer;
	FD_SET Reader;
	DWORD Flags;
	DWORD SendBytes;
	DWORD RecvBytes;
	ULONG NonBlock;
	BOOL CreateSocketInformation(SOCKET s);
	void FreeSocketInformation(DWORD Index);

	typedef struct _MYSOCKET_INFORMATION {
		CHAR Buffer[BUFFERSIZE];
		WSABUF DataBuf;
		SOCKET Socket;
		OVERLAPPED Overlapped;
		DWORD SendBytes;
		DWORD RecvBytes;
	} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;

	DWORD TotalSockets;
	LPSOCKET_INFORMATION SocketList[1]; //[FD_SETSIZE];

	int _PORT;


	void _OnDataReceive(int sockIndex, char *data, long buflen);
	bool _isConnected;
	HANDLE thread;

	// call back dati ricevuti sul socket
	void (*onsocketreceive)(char *text);
	int bufferInCount;
	char bufferIn[BUFFERSIZE];

	// socket avviato in modalità debug (logga tuuutto)
	bool _debugMode;

	void _socksend(unsigned int sockIndex ,char *data, int len);

	// buffer temporaneo per la sprintf nel logfile
	char buffer_temp[BUFFERSIZE+1];

public:
	DebugSock(int PORT, bool debugMode);
	~DebugSock(void);
	void socksend(unsigned int sockIndex ,char *data);

	bool isConnected();
	bool isInDebugMode();

	// setta callback per ricezione dati su socket
	void SetSocketReceiveCallBack(void (*onsocketreceive)(char *text));
	DWORD RunningThread();
	void logfile(bool truncate, char *pattern, ...);
};
#endif
#endif
