2021-09-03 05:11:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
#include "ScriptAPI.h"
|
|
|
|
#include <windows.h>
|
2021-09-03 05:11:04 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
|
|
|
class CJSServerWorker : public CScriptWorker
|
|
|
|
{
|
|
|
|
private:
|
2022-09-26 02:31:54 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TIMEOUT_MS = 1
|
|
|
|
};
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
struct ServerQueue
|
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
CriticalSection cs;
|
|
|
|
std::string listenAddress;
|
|
|
|
unsigned short listenPort;
|
|
|
|
bool bClosePending;
|
2022-09-26 02:31:54 +00:00
|
|
|
ServerQueue() :
|
|
|
|
listenAddress(""), listenPort(0), bClosePending(false)
|
|
|
|
{
|
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
} m_Queue;
|
|
|
|
|
|
|
|
struct JSServerAddrInfo
|
|
|
|
{
|
|
|
|
char address[INET6_ADDRSTRLEN];
|
2022-09-26 02:31:54 +00:00
|
|
|
const char * family;
|
2021-09-03 05:11:04 +00:00
|
|
|
unsigned short port;
|
|
|
|
|
|
|
|
JSServerAddrInfo() :
|
|
|
|
address(""),
|
|
|
|
family(""),
|
|
|
|
port(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool m_bWinsockOK;
|
|
|
|
SOCKET m_ServerSocket;
|
|
|
|
JSServerAddrInfo m_Address;
|
|
|
|
|
|
|
|
public:
|
2022-09-26 02:31:54 +00:00
|
|
|
CJSServerWorker(CScriptInstance * instance, void * dukObjectHeapPtr);
|
2021-09-03 05:11:04 +00:00
|
|
|
virtual ~CJSServerWorker();
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
void Init(const char * address, unsigned short port);
|
2021-09-03 05:11:04 +00:00
|
|
|
void WorkerProc();
|
|
|
|
|
|
|
|
std::string GetAddress();
|
|
|
|
unsigned short GetPort();
|
2022-09-26 02:31:54 +00:00
|
|
|
const char * GetFamily();
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void JSEmitConnection(SOCKET c);
|
|
|
|
void JSEmitClose();
|
|
|
|
void JSEmitListening();
|
2022-09-26 02:31:54 +00:00
|
|
|
void JSEmitError(const char * errMessage);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
static duk_idx_t CbArgs_EmitConnection(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitClose(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitListening(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitError(duk_context * ctx, void * _env);
|
2021-09-03 05:11:04 +00:00
|
|
|
};
|