2022-09-26 02:31:54 +00:00
|
|
|
#include "../ScriptWorker.h"
|
|
|
|
#include "ScriptAPI.h"
|
|
|
|
#include <windows.h>
|
2021-09-03 05:11:04 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2022-09-26 02:31:54 +00:00
|
|
|
#pragma comment(lib, "Ws2_32.lib")
|
2021-09-03 05:11:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CJSSocketWorker : public CScriptWorker
|
|
|
|
{
|
|
|
|
private:
|
2022-09-26 02:31:54 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TIMEOUT_MS = 1
|
|
|
|
};
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
struct JSSocketAddrInfo
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
JSSocketAddrInfo() :
|
|
|
|
address(""),
|
|
|
|
family(""),
|
|
|
|
port(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BufferedWrite
|
|
|
|
{
|
|
|
|
size_t offset;
|
|
|
|
std::vector<char> data;
|
|
|
|
duk_int_t callbackId;
|
|
|
|
};
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
struct JSEmitDataEnv
|
|
|
|
{
|
|
|
|
char * data;
|
2021-09-03 05:11:04 +00:00
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JSSocketQueue
|
|
|
|
{
|
|
|
|
CriticalSection cs;
|
|
|
|
|
|
|
|
bool bConnectPending;
|
|
|
|
bool bFullClosePending;
|
|
|
|
bool bSendClosePending;
|
|
|
|
bool bSendClosed;
|
|
|
|
bool bRecvClosed;
|
|
|
|
|
|
|
|
std::string connectHost;
|
|
|
|
unsigned short connectPort;
|
|
|
|
std::vector<BufferedWrite> writes;
|
|
|
|
|
|
|
|
JSSocketQueue() :
|
|
|
|
bConnectPending(false),
|
|
|
|
bFullClosePending(false),
|
|
|
|
bSendClosePending(false),
|
|
|
|
bSendClosed(false),
|
|
|
|
bRecvClosed(false),
|
|
|
|
connectHost(""),
|
|
|
|
connectPort(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
SOCKET m_Socket;
|
|
|
|
bool m_bWinsockOK;
|
|
|
|
bool m_bAllowHalfOpen;
|
2021-09-03 05:11:04 +00:00
|
|
|
JSSocketAddrInfo m_LocalAddress;
|
|
|
|
JSSocketAddrInfo m_RemoteAddress;
|
2022-09-26 02:31:54 +00:00
|
|
|
JSSocketQueue m_Queue;
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
public:
|
2022-09-26 02:31:54 +00:00
|
|
|
CJSSocketWorker(CScriptInstance * inst, void * objectHeapPtr, bool bAllowHalfOpen);
|
2021-09-03 05:11:04 +00:00
|
|
|
virtual ~CJSSocketWorker();
|
|
|
|
|
|
|
|
bool Init(SOCKET sock);
|
2022-09-26 02:31:54 +00:00
|
|
|
bool Init(const char * host, unsigned short port);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
void WorkerProc();
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
bool Write(const char * data, size_t length, duk_int_t callbackId, bool bEnd = false);
|
2021-09-03 05:11:04 +00:00
|
|
|
//bool GetAddress(JSSocketAddrInfo& address);
|
|
|
|
std::string GetLocalAddress();
|
|
|
|
unsigned short GetLocalPort();
|
|
|
|
std::string GetRemoteAddress();
|
|
|
|
unsigned short GetRemotePort();
|
2022-09-26 02:31:54 +00:00
|
|
|
const char * GetFamily();
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool ProcConnect();
|
|
|
|
void ProcSendData();
|
|
|
|
void ProcRecvData();
|
|
|
|
|
|
|
|
void UpdateAddresses();
|
|
|
|
void ClearAddress();
|
|
|
|
void ClearQueue();
|
|
|
|
|
|
|
|
void JSEmitConnect();
|
2022-09-26 02:31:54 +00:00
|
|
|
void JSEmitData(const char * data, size_t size);
|
2021-09-03 05:11:04 +00:00
|
|
|
void JSEmitEnd();
|
|
|
|
void JSEmitClose();
|
|
|
|
void JSEmitDrain();
|
2022-09-26 02:31:54 +00:00
|
|
|
void JSEmitLookup(JSSocketAddrInfo & addr);
|
|
|
|
void JSEmitError(const char * errMessage);
|
|
|
|
|
|
|
|
static duk_idx_t CbArgs_EmitConnect(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitData(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitEnd(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitClose(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitDrain(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitLookup(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_EmitError(duk_context * ctx, void * _env);
|
|
|
|
static duk_idx_t CbArgs_Write(duk_context * ctx, void * _env);
|
2021-09-03 05:11:04 +00:00
|
|
|
};
|