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.
|
|
|
|
*/
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
moodycamel::ReaderWriterQueue<CSocketConnect *> g_ConnectQueue;
|
2016-12-24 12:12:27 +01:00
|
|
|
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
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
bool g_Running;
|
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();
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(handlesys->ReadHandle(handle, socketHandleType, &sec, (void **)&pSocketContext) != HandleError_None)
|
2015-05-21 19:29:14 +02:00
|
|
|
return NULL;
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
return pSocketContext;
|
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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)object;
|
|
|
|
pSocketContext->m_Deleted = true;
|
|
|
|
|
|
|
|
if(g_Running && (pSocketContext->m_pSocket || pSocketContext->m_pStream || pSocketContext->m_PendingCallback))
|
|
|
|
{
|
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_DeleteAsyncContext;
|
|
|
|
Job.pData = pSocketContext;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
|
|
|
|
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pSocketContext;
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void OnGameFrame(bool simulating)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CSocketConnect *pConnect;
|
|
|
|
while(g_ConnectQueue.try_dequeue(pConnect))
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pConnect->pSocketContext->m_Server)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pSocketContext = new CAsyncSocketContext(pConnect->pSocketContext->m_pContext);
|
|
|
|
pSocketContext->m_Handle = handlesys->CreateHandle(g_AsyncSocket.socketHandleType, pSocketContext,
|
|
|
|
pConnect->pSocketContext->m_pContext->GetIdentity(), myself->GetIdentity(), NULL);
|
|
|
|
|
|
|
|
pSocketContext->m_pStream = pConnect->pClientSocket;
|
|
|
|
pSocketContext->m_pStream->data = pSocketContext;
|
|
|
|
|
|
|
|
pConnect->pSocketContext->OnConnect(pSocketContext);
|
|
|
|
|
|
|
|
if(!pSocketContext->m_Deleted)
|
|
|
|
{
|
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_StartRead;
|
|
|
|
Job.pData = pSocketContext;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
|
|
|
|
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pConnect->pSocketContext->Connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pConnect);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketData *pData;
|
|
|
|
while(g_DataQueue.try_dequeue(pData))
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
pData->pSocketContext->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);
|
|
|
|
}
|
2017-01-08 16:36:28 +01:00
|
|
|
|
|
|
|
CSocketError *pError;
|
|
|
|
while(g_ErrorQueue.try_dequeue(pError))
|
|
|
|
{
|
|
|
|
pError->pSocketContext->OnError(pError->Error);
|
|
|
|
|
|
|
|
free(pError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// main event loop thread
|
|
|
|
void UV_EventLoop(void *data)
|
|
|
|
{
|
|
|
|
uv_run(g_UV_Loop, UV_RUN_DEFAULT);
|
2016-12-24 12:12:27 +01:00
|
|
|
}
|
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;
|
2017-01-08 16:36:28 +01:00
|
|
|
pAsync->close_cb = UV_FreeHandle;
|
2016-12-24 12:12:27 +01:00
|
|
|
uv_async_send(pAsync);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_FreeHandle(uv_handle_t *handle)
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
free(handle);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_Quit(uv_async_t *pHandle)
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
|
|
|
|
|
|
|
|
uv_close((uv_handle_t *)&g_UV_AsyncAdded, NULL);
|
|
|
|
|
|
|
|
uv_stop(g_UV_Loop);
|
2016-12-24 12:12:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_DeleteAsyncContext(uv_async_t *pHandle)
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data;
|
|
|
|
uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
|
2017-08-02 22:03:07 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext->m_pStream)
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)pSocketContext->m_pStream, pSocketContext->m_pStream->close_cb);
|
|
|
|
pSocketContext->m_pStream = NULL;
|
|
|
|
pSocketContext->m_pSocket = NULL;
|
|
|
|
}
|
2017-08-02 22:03:07 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext->m_pSocket)
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb);
|
|
|
|
pSocketContext->m_pSocket = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete pSocketContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UV_PushError(CAsyncSocketContext *pSocketContext, int error)
|
|
|
|
{
|
|
|
|
pSocketContext->m_PendingCallback = true;
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketError *pError = (CSocketError *)malloc(sizeof(CSocketError));
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pError->pSocketContext = pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
pError->Error = error;
|
|
|
|
|
|
|
|
g_ErrorQueue.enqueue(pError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)client->data;
|
|
|
|
if(pSocketContext->m_Deleted)
|
|
|
|
{
|
|
|
|
free(buf->base);
|
|
|
|
uv_close((uv_handle_t *)client, client->close_cb);
|
|
|
|
pSocketContext->m_pStream = NULL;
|
|
|
|
pSocketContext->m_pSocket = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(nread < 0)
|
|
|
|
{
|
|
|
|
// Connection closed
|
2017-01-08 16:36:28 +01:00
|
|
|
free(buf->base);
|
|
|
|
// But let the client disconnect.
|
|
|
|
//uv_close((uv_handle_t *)client, client->close_cb);
|
|
|
|
//pSocketContext->m_pStream = NULL;
|
|
|
|
//pSocketContext->m_pSocket = NULL;
|
2016-12-24 12:12:27 +01:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
UV_PushError(pSocketContext, nread);
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-01-08 16:36:28 +01:00
|
|
|
|
2017-08-02 22:03:07 +02:00
|
|
|
pSocketContext->m_PendingCallback = true;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
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);
|
2017-01-08 16:36:28 +01:00
|
|
|
free(buf->base);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CSocketData *pData = (CSocketData *)malloc(sizeof(CSocketData));
|
2017-01-08 16:36:28 +01:00
|
|
|
pData->pSocketContext = pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void UV_OnConnect(uv_connect_t *req, int status)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)req->data;
|
|
|
|
if(pSocketContext->m_Deleted)
|
|
|
|
{
|
|
|
|
free(req);
|
|
|
|
uv_close((uv_handle_t *)req->handle, req->handle->close_cb);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(status < 0)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
free(req);
|
|
|
|
UV_PushError(pSocketContext, status);
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-02 22:03:07 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_PendingCallback = true;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_pStream = req->handle;
|
|
|
|
free(req);
|
|
|
|
pSocketContext->m_pStream->data = pSocketContext;
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
CSocketConnect *pConnect = (CSocketConnect *)malloc(sizeof(CSocketConnect));
|
|
|
|
pConnect->pSocketContext = pSocketContext;
|
|
|
|
pConnect->pClientSocket = pSocketContext->m_pStream;
|
|
|
|
g_ConnectQueue.enqueue(pConnect);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_read_start(pSocketContext->m_pStream, UV_AllocBuffer, UV_OnRead);
|
|
|
|
}
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_StartRead(uv_async_t *pHandle)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data;
|
|
|
|
uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
|
|
|
|
|
|
|
|
if(pSocketContext->m_Deleted || !pSocketContext->m_pStream)
|
|
|
|
return;
|
|
|
|
|
2017-08-02 22:03:07 +02:00
|
|
|
uv_read_start(pSocketContext->m_pStream, UV_AllocBuffer, UV_OnRead);
|
2017-01-08 16:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UV_OnNewConnection(uv_stream_t *server, int status)
|
|
|
|
{
|
|
|
|
// server context
|
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)server->data;
|
|
|
|
if(pSocketContext->m_Deleted)
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)server, server->close_cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status < 0)
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)server, server->close_cb);
|
|
|
|
//uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb);
|
|
|
|
UV_PushError(pSocketContext, status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_tcp_t *pClientSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
|
|
|
|
uv_tcp_init(g_UV_Loop, pClientSocket);
|
|
|
|
pClientSocket->close_cb = UV_FreeHandle;
|
|
|
|
|
|
|
|
if(uv_accept((uv_stream_t *)pSocketContext->m_pSocket, (uv_stream_t *)pClientSocket) == 0)
|
|
|
|
{
|
|
|
|
pSocketContext->m_PendingCallback = true;
|
|
|
|
CSocketConnect *pConnect = (CSocketConnect *)malloc(sizeof(CSocketConnect));
|
|
|
|
pConnect->pSocketContext = pSocketContext;
|
|
|
|
pConnect->pClientSocket = (uv_stream_t *)pClientSocket;
|
|
|
|
g_ConnectQueue.enqueue(pConnect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)pClientSocket, pClientSocket->close_cb);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
if(resolver->service != NULL)
|
|
|
|
free(resolver->service);
|
|
|
|
|
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)resolver->data;
|
|
|
|
if(pSocketContext->m_Deleted || pSocketContext->m_pSocket)
|
|
|
|
{
|
|
|
|
uv_freeaddrinfo(res);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
if(status < 0)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_Pending = false;
|
|
|
|
uv_freeaddrinfo(res);
|
|
|
|
UV_PushError(pSocketContext, status);
|
2016-12-24 12:12:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_tcp_t *pSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
|
|
|
|
uv_tcp_init(g_UV_Loop, pSocket);
|
|
|
|
pSocket->close_cb = UV_FreeHandle;
|
|
|
|
pSocket->data = pSocketContext;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_pSocket = pSocket;
|
|
|
|
pSocketContext->m_Pending = false;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext->m_Server)
|
|
|
|
{
|
|
|
|
uv_tcp_bind(pSocket, (const struct sockaddr *)res->ai_addr, 0);
|
|
|
|
|
|
|
|
int err = uv_listen((uv_stream_t *)pSocket, 32, UV_OnNewConnection);
|
|
|
|
if(err)
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t *)pSocket, pSocket->close_cb);
|
2017-08-02 22:03:07 +02:00
|
|
|
pSocketContext->m_pSocket = NULL;
|
2017-01-08 16:36:28 +01:00
|
|
|
UV_PushError(pSocketContext, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-02 22:03:07 +02:00
|
|
|
uv_connect_t *pConnectReq = (uv_connect_t *)malloc(sizeof(uv_connect_t));
|
|
|
|
pConnectReq->data = pSocketContext;
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_tcp_connect(pConnectReq, pSocket, (const struct sockaddr *)res->ai_addr, UV_OnConnect);
|
|
|
|
}
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
uv_freeaddrinfo(res);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_OnAsyncResolve(uv_async_t *pHandle)
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data;
|
|
|
|
uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
|
|
|
|
|
|
|
|
if(pSocketContext->m_Deleted || pSocketContext->m_pSocket)
|
|
|
|
return;
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_Resolver.data = pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
char *service = (char *)malloc(8);
|
2017-01-08 16:36:28 +01:00
|
|
|
sprintf(service, "%d", pSocketContext->m_Port);
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
int err = uv_getaddrinfo(g_UV_Loop, &pSocketContext->m_Resolver, UV_OnAsyncResolved, pSocketContext->m_pHost, service, &hints);
|
2016-12-24 12:12:27 +01:00
|
|
|
if(err)
|
2017-01-08 16:36:28 +01:00
|
|
|
{
|
|
|
|
if(service != NULL)
|
|
|
|
free(service);
|
|
|
|
UV_PushError(pSocketContext, err);
|
|
|
|
}
|
2016-12-24 12:12:27 +01:00
|
|
|
}
|
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;
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_close((uv_handle_t *)handle, handle->close_cb);
|
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
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pWrite->pSocketContext == NULL || pWrite->pSocketContext->m_pStream == NULL)
|
2016-12-24 12:12:27 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_write(req, pWrite->pSocketContext->m_pStream, 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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = new CAsyncSocketContext(pContext);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_Handle = handlesys->CreateHandle(g_AsyncSocket.socketHandleType, pSocketContext,
|
2016-12-24 12:12:27 +01:00
|
|
|
pContext->GetIdentity(), myself->GetIdentity(), NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
return pSocketContext->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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
|
|
|
|
|
|
|
if(pSocketContext == NULL)
|
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
|
|
|
if(params[3] < 0 || params[3] > 65535)
|
|
|
|
return pContext->ThrowNativeError("Invalid port specified");
|
|
|
|
|
|
|
|
if(pSocketContext->m_pSocket)
|
|
|
|
return pContext->ThrowNativeError("Socket is already connected");
|
|
|
|
|
|
|
|
if(pSocketContext->m_Pending)
|
|
|
|
return pContext->ThrowNativeError("Socket is currently pending");
|
|
|
|
|
|
|
|
char *address = NULL;
|
|
|
|
pContext->LocalToString(params[2], &address);
|
|
|
|
|
|
|
|
pSocketContext->m_pHost = strdup(address);
|
|
|
|
pSocketContext->m_Port = params[3];
|
|
|
|
pSocketContext->m_Server = false;
|
|
|
|
pSocketContext->m_Pending = true;
|
|
|
|
|
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_OnAsyncResolve;
|
|
|
|
Job.pData = pSocketContext;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
|
|
|
|
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell_t Native_AsyncSocket_Listen(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext == 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
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext->m_pSocket)
|
|
|
|
return pContext->ThrowNativeError("Socket is already connected");
|
|
|
|
|
|
|
|
if(pSocketContext->m_Pending)
|
|
|
|
return pContext->ThrowNativeError("Socket is currently pending");
|
|
|
|
|
2015-05-21 19:29:14 +02:00
|
|
|
char *address = NULL;
|
|
|
|
pContext->LocalToString(params[2], &address);
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pSocketContext->m_pHost = strdup(address);
|
|
|
|
pSocketContext->m_Port = params[3];
|
|
|
|
pSocketContext->m_Server = true;
|
|
|
|
pSocketContext->m_Pending = true;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_OnAsyncResolve;
|
2017-01-08 16:36:28 +01:00
|
|
|
Job.pData = pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(!pSocketContext->m_pStream)
|
|
|
|
return pContext->ThrowNativeError("Socket is not connected");
|
|
|
|
|
2015-05-21 19:29:14 +02:00
|
|
|
char *data = NULL;
|
2017-08-02 22:03:07 +02:00
|
|
|
pContext->LocalToPhysAddr(params[2], (cell_t **)&data);
|
|
|
|
|
|
|
|
uv_buf_t *buffer = (uv_buf_t *)malloc(sizeof(uv_buf_t));
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-08-02 22:03:07 +02:00
|
|
|
if(params[3] >= 0)
|
|
|
|
buffer->len = params[3];
|
|
|
|
else
|
|
|
|
buffer->len = strlen(data);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-08-02 22:03:07 +02:00
|
|
|
buffer->base = (char *)malloc(buffer->len + 1);
|
|
|
|
memcpy(buffer->base, data, buffer->len + 1);
|
|
|
|
buffer->base[buffer->len] = 0;
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncWrite *pWrite = (CAsyncWrite *)malloc(sizeof(CAsyncWrite));
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
pWrite->pSocketContext = pSocketContext;
|
2016-12-24 12:12:27 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(!pSocketContext->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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(!pSocketContext->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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncSocketContext *pSocketContext = g_AsyncSocket.GetSocketInstanceByHandle(params[1]);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(pSocketContext == NULL)
|
2015-05-21 19:29:14 +02:00
|
|
|
return pContext->ThrowNativeError("Invalid socket handle");
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
if(!pSocketContext->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)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
g_Running = true;
|
2015-05-21 19:29:14 +02:00
|
|
|
sharesys->AddNatives(myself, AsyncSocketNatives);
|
2017-01-08 16:36:28 +01:00
|
|
|
sharesys->RegisterLibrary(myself, "AsyncSocket");
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
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);
|
2017-01-08 16:36:28 +01:00
|
|
|
g_UV_AsyncAdded.close_cb = NULL;
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
uv_thread_create(&g_UV_LoopThread, UV_EventLoop, NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
void UV_OnWalk(uv_handle_t *pHandle, void *pArg)
|
|
|
|
{
|
|
|
|
uv_close(pHandle, pHandle->close_cb);
|
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void AsyncSocket::SDK_OnUnload()
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
g_Running = false;
|
|
|
|
handlesys->RemoveType(socketHandleType, myself->GetIdentity());
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
CAsyncAddJob Job;
|
|
|
|
Job.CallbackFn = UV_Quit;
|
|
|
|
Job.pData = NULL;
|
|
|
|
g_AsyncAddQueue.enqueue(Job);
|
|
|
|
|
|
|
|
uv_async_send(&g_UV_AsyncAdded);
|
2016-12-24 12:12:27 +01:00
|
|
|
|
|
|
|
uv_thread_join(&g_UV_LoopThread);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
uv_walk(g_UV_Loop, UV_OnWalk, NULL);
|
|
|
|
|
|
|
|
uv_run(g_UV_Loop, UV_RUN_DEFAULT);
|
|
|
|
|
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},
|
2017-01-08 16:36:28 +01:00
|
|
|
{"AsyncSocket.Listen", Native_AsyncSocket_Listen},
|
2016-12-24 12:12:27 +01:00
|
|
|
{"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
|
|
|
};
|