2015-05-21 19:29:14 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
|
|
|
* SourceMod Sample Extension
|
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
2016-12-24 12:12:27 +01:00
|
|
|
*
|
2015-05-21 19:29:14 +02:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "extension.h"
|
|
|
|
#include "context.h"
|
2016-12-24 12:12:27 +01:00
|
|
|
#include "readerwriterqueue.h"
|
2015-05-21 19:29:14 +02:00
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file extension.cpp
|
|
|
|
* @brief Implement extension code here.
|
|
|
|
*/
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
moodycamel::ReaderWriterQueue<CAsyncSocketContext *> g_ConnectQueue;
|
|
|
|
moodycamel::ReaderWriterQueue<CSocketError *> g_ErrorQueue;
|
|
|
|
moodycamel::ReaderWriterQueue<CSocketData *> g_DataQueue;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_loop_t *g_UV_Loop;
|
|
|
|
uv_thread_t g_UV_LoopThread;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_async_t g_UV_AsyncAdded;
|
|
|
|
moodycamel::ReaderWriterQueue<CAsyncAddJob> g_AsyncAddQueue;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
AsyncSocket g_AsyncSocket; /**< Global singleton for extension's main interface */
|
|
|
|
|
|
|
|
SMEXT_LINK(&g_AsyncSocket);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncSocketContext *AsyncSocket::GetSocketInstanceByHandle(Handle_t handle)
|
|
|
|
{
|
2015-05-21 19:29:14 +02:00
|
|
|
HandleSecurity sec;
|
|
|
|
sec.pOwner = NULL;
|
|
|
|
sec.pIdentity = myself->GetIdentity();
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncSocketContext *pContext;
|
|
|
|
|
|
|
|
if(handlesys->ReadHandle(handle, socketHandleType, &sec, (void **)&pContext) != HandleError_None)
|
2015-05-21 19:29:14 +02:00
|
|
|
return NULL;
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
return pContext;
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void AsyncSocket::OnHandleDestroy(HandleType_t type, void *object)
|
|
|
|
{
|
|
|
|
if(object != NULL)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pContext = (CAsyncSocketContext *)object;
|
|
|
|
delete pContext;
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void OnGameFrame(bool simulating)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pContext;
|
|
|
|
while(g_ConnectQueue.try_dequeue(pContext))
|
|
|
|
{
|
|
|
|
pContext->Connected();
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketError *pError;
|
|
|
|
while(g_ErrorQueue.try_dequeue(pError))
|
|
|
|
{
|
|
|
|
pError->pAsyncContext->OnError(pError->Error);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
free(pError);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketData *pData;
|
|
|
|
while(g_DataQueue.try_dequeue(pData))
|
|
|
|
{
|
|
|
|
pData->pAsyncContext->OnData(pData->pBuffer, pData->BufferSize);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
free(pData->pBuffer);
|
|
|
|
free(pData);
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnAsyncAdded(uv_async_t *pHandle)
|
|
|
|
{
|
|
|
|
CAsyncAddJob Job;
|
|
|
|
while(g_AsyncAddQueue.try_dequeue(Job))
|
|
|
|
{
|
|
|
|
uv_async_t *pAsync = (uv_async_t *)malloc(sizeof(uv_async_t));
|
|
|
|
uv_async_init(g_UV_Loop, pAsync, Job.CallbackFn);
|
|
|
|
pAsync->data = Job.pData;
|
|
|
|
uv_async_send(pAsync);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// main event loop thread
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_EventLoop(void *data)
|
|
|
|
{
|
|
|
|
uv_run(g_UV_Loop, UV_RUN_DEFAULT);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_AllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
|
|
|
|
{
|
|
|
|
buf->base = (char *)malloc(suggested_size);
|
2015-05-21 19:29:14 +02:00
|
|
|
buf->len = suggested_size;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_HandleCleanup(uv_handle_t *handle)
|
|
|
|
{
|
|
|
|
free(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UV_PushError(CAsyncSocketContext *pContext, int error)
|
|
|
|
{
|
|
|
|
CSocketError *pError = (CSocketError *)malloc(sizeof(CSocketError));
|
|
|
|
|
|
|
|
pError->pAsyncContext = pContext;
|
|
|
|
pError->Error = error;
|
|
|
|
|
|
|
|
g_ErrorQueue.enqueue(pError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pContext = (CAsyncSocketContext *)client->data;
|
|
|
|
if(nread < 0)
|
|
|
|
{
|
|
|
|
// Connection closed
|
|
|
|
uv_close((uv_handle_t *)client, NULL);
|
|
|
|
pContext->socket = NULL;
|
|
|
|
|
|
|
|
UV_PushError((CAsyncSocketContext *)client->data, nread);
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
char *data = (char *)malloc(sizeof(char) * (nread + 1));
|
|
|
|
data[nread] = 0;
|
2015-05-21 19:29:14 +02:00
|
|
|
strncpy(data, buf->base, nread);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketData *pData = (CSocketData *)malloc(sizeof(CSocketData));
|
|
|
|
pData->pAsyncContext = pContext;
|
|
|
|
pData->pBuffer = data;
|
|
|
|
pData->BufferSize = nread;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
g_DataQueue.enqueue(pData);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
free(buf->base);
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnConnect(uv_connect_t *req, int status)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pContext = (CAsyncSocketContext *)req->data;
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(status < 0)
|
|
|
|
{
|
|
|
|
UV_PushError(pContext, status);
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
pContext->stream = req->handle;
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
g_ConnectQueue.enqueue(pContext);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
req->handle->data = req->data;
|
2016-12-24 12:12:27 +01:00
|
|
|
free(req);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_read_start(pContext->stream, UV_AllocBuffer, UV_OnRead);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnAsyncResolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
|
|
|
|
{
|
|
|
|
free(resolver->service);
|
|
|
|
CAsyncSocketContext *pContext = (CAsyncSocketContext *) resolver->data;
|
|
|
|
|
|
|
|
if(status < 0)
|
|
|
|
{
|
|
|
|
UV_PushError(pContext, status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_connect_t *connect_req = (uv_connect_t *)malloc(sizeof(uv_connect_t));
|
|
|
|
uv_tcp_t *socket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
|
|
|
|
|
|
|
|
pContext->socket = socket;
|
|
|
|
connect_req->data = pContext;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
char addr[32] = {0};
|
|
|
|
uv_ip4_name((struct sockaddr_in *)res->ai_addr, addr, sizeof(addr));
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_tcp_init(g_UV_Loop, socket);
|
|
|
|
uv_tcp_connect(connect_req, socket, (const struct sockaddr*) res->ai_addr, UV_OnConnect);
|
|
|
|
|
|
|
|
uv_freeaddrinfo(res);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnAsyncResolve(uv_async_t *handle)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = (CAsyncSocketContext *)handle->data;
|
|
|
|
uv_close((uv_handle_t *)handle, UV_HandleCleanup);
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
pAsyncContext->resolver.data = pAsyncContext;
|
|
|
|
|
|
|
|
char *service = (char *)malloc(8);
|
|
|
|
sprintf(service, "%d", pAsyncContext->m_Port);
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
hints.ai_family = PF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = 0;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
int err = uv_getaddrinfo(g_UV_Loop, &pAsyncContext->resolver, UV_OnAsyncResolved, pAsyncContext->m_pHost, service, &hints);
|
|
|
|
if(err)
|
|
|
|
UV_PushError(pAsyncContext, err);
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnAsyncWriteCleanup(uv_write_t *req, int status)
|
|
|
|
{
|
|
|
|
CAsyncWrite *pWrite = (CAsyncWrite *)req->data;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
free(pWrite->pBuffer->base);
|
|
|
|
free(pWrite->pBuffer);
|
|
|
|
free(pWrite);
|
|
|
|
free(req);
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnAsyncWrite(uv_async_t *handle)
|
|
|
|
{
|
|
|
|
CAsyncWrite *pWrite = (CAsyncWrite *)handle->data;
|
|
|
|
uv_close((uv_handle_t *)handle, UV_HandleCleanup);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pWrite == NULL || pWrite->pBuffer == NULL)
|
|
|
|
return;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pWrite->pAsyncContext == NULL || pWrite->pAsyncContext->stream == NULL)
|
|
|
|
{
|
|
|
|
free(pWrite->pBuffer->base);
|
|
|
|
free(pWrite->pBuffer);
|
|
|
|
free(pWrite);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_write_t *req = (uv_write_t *)malloc(sizeof(uv_write_t));
|
|
|
|
req->data = pWrite;
|
|
|
|
|
|
|
|
uv_write(req, pWrite->pAsyncContext->stream, pWrite->pBuffer, 1, UV_OnAsyncWriteCleanup);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_Create(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = new CAsyncSocketContext(pContext);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
pAsyncContext->m_Handle = handlesys->CreateHandle(g_AsyncSocket.socketHandleType, pAsyncContext,
|
|
|
|
pContext->GetIdentity(), myself->GetIdentity(), NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
return pAsyncContext->m_Handle;
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_Connect(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pAsyncContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(params[3] < 0 || params[3] > 65535)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid port specified");
|
2016-12-24 12:12:27 +01:00
|
|
|
|
2015-05-21 19:29:14 +02:00
|
|
|
char *address = NULL;
|
|
|
|
pContext->LocalToString(params[2], &address);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
pAsyncContext->m_pHost = address;
|
|
|
|
pAsyncContext->m_Port = params[3];
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_OnAsyncResolve;
|
|
|
|
Job.pData = pAsyncContext;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
|
|
|
|
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_Write(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pAsyncContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
|
|
|
char *data = NULL;
|
|
|
|
pContext->LocalToString(params[2], &data);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_buf_t* buffer = (uv_buf_t *)malloc(sizeof(uv_buf_t));
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2015-05-24 04:57:44 +02:00
|
|
|
buffer->base = strdup(data);
|
|
|
|
buffer->len = strlen(data);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncWrite *pWrite = (CAsyncWrite *)malloc(sizeof(CAsyncWrite));
|
|
|
|
|
|
|
|
pWrite->pAsyncContext = pAsyncContext;
|
|
|
|
pWrite->pBuffer = buffer;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_OnAsyncWrite;
|
|
|
|
Job.pData = pWrite;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_SetConnectCallback(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pAsyncContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!pAsyncContext->SetConnectCallback(params[2]))
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid callback");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_SetErrorCallback(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pAsyncContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!pAsyncContext->SetErrorCallback(params[2]))
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid callback");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
cell_t Native_AsyncSocket_SetDataCallback(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pAsyncContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(pAsyncContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!pAsyncContext->SetDataCallback(params[2]))
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid callback");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sourcemod Plugin Events
|
2016-12-24 12:12:27 +01:00
|
|
|
bool AsyncSocket::SDK_OnLoad(char *error, size_t maxlength, bool late)
|
|
|
|
{
|
2015-05-21 19:29:14 +02:00
|
|
|
sharesys->AddNatives(myself, AsyncSocketNatives);
|
|
|
|
sharesys->RegisterLibrary(myself, "async_socket");
|
|
|
|
|
|
|
|
socketHandleType = handlesys->CreateType("AsyncSocket", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
|
|
|
|
|
|
|
|
smutils->AddGameFrameHook(OnGameFrame);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
g_UV_Loop = uv_default_loop();
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_async_init(g_UV_Loop, &g_UV_AsyncAdded, UV_OnAsyncAdded);
|
|
|
|
|
|
|
|
uv_thread_create(&g_UV_LoopThread, UV_EventLoop, NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void AsyncSocket::SDK_OnUnload()
|
|
|
|
{
|
2015-05-21 19:29:14 +02:00
|
|
|
handlesys->RemoveType(socketHandleType, NULL);
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_close((uv_handle_t *)&g_UV_AsyncAdded, NULL);
|
|
|
|
|
|
|
|
uv_thread_join(&g_UV_LoopThread);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_loop_close(g_UV_Loop);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
smutils->RemoveGameFrameHook(OnGameFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sp_nativeinfo_t AsyncSocketNatives[] = {
|
2016-12-24 12:12:27 +01:00
|
|
|
{"AsyncSocket.AsyncSocket", Native_AsyncSocket_Create},
|
|
|
|
{"AsyncSocket.Connect", Native_AsyncSocket_Connect},
|
|
|
|
{"AsyncSocket.Write", Native_AsyncSocket_Write},
|
|
|
|
{"AsyncSocket.SetConnectCallback", Native_AsyncSocket_SetConnectCallback},
|
|
|
|
{"AsyncSocket.SetErrorCallback", Native_AsyncSocket_SetErrorCallback},
|
|
|
|
{"AsyncSocket.SetDataCallback", Native_AsyncSocket_SetDataCallback},
|
2015-05-21 19:29:14 +02:00
|
|
|
{NULL, NULL}
|
2016-12-24 12:12:27 +01:00
|
|
|
};
|