Fix bugs: Stable release.

This commit is contained in:
BotoX 2017-08-02 22:03:07 +02:00
parent 7b10708a20
commit b38d2077b1
6 changed files with 28 additions and 55 deletions

View File

@ -49,7 +49,6 @@ uv_thread_t g_UV_LoopThread;
uv_async_t g_UV_AsyncAdded; uv_async_t g_UV_AsyncAdded;
moodycamel::ReaderWriterQueue<CAsyncAddJob> g_AsyncAddQueue; moodycamel::ReaderWriterQueue<CAsyncAddJob> g_AsyncAddQueue;
int g_MallocHandles = 0;
bool g_Running; bool g_Running;
AsyncSocket g_AsyncSocket; /**< Global singleton for extension's main interface */ AsyncSocket g_AsyncSocket; /**< Global singleton for extension's main interface */
@ -78,7 +77,6 @@ void AsyncSocket::OnHandleDestroy(HandleType_t type, void *object)
if(g_Running && (pSocketContext->m_pSocket || pSocketContext->m_pStream || pSocketContext->m_PendingCallback)) if(g_Running && (pSocketContext->m_pSocket || pSocketContext->m_pStream || pSocketContext->m_PendingCallback))
{ {
printf("Delete: Async\n");
CAsyncAddJob Job; CAsyncAddJob Job;
Job.CallbackFn = UV_DeleteAsyncContext; Job.CallbackFn = UV_DeleteAsyncContext;
Job.pData = pSocketContext; Job.pData = pSocketContext;
@ -88,7 +86,6 @@ void AsyncSocket::OnHandleDestroy(HandleType_t type, void *object)
} }
else else
{ {
printf("Delete: Now\n");
delete pSocketContext; delete pSocketContext;
} }
} }
@ -99,10 +96,8 @@ void OnGameFrame(bool simulating)
CSocketConnect *pConnect; CSocketConnect *pConnect;
while(g_ConnectQueue.try_dequeue(pConnect)) while(g_ConnectQueue.try_dequeue(pConnect))
{ {
printf("g_ConnectQueue got: ");
if(pConnect->pSocketContext->m_Server) if(pConnect->pSocketContext->m_Server)
{ {
printf("server\n");
CAsyncSocketContext *pSocketContext = new CAsyncSocketContext(pConnect->pSocketContext->m_pContext); CAsyncSocketContext *pSocketContext = new CAsyncSocketContext(pConnect->pSocketContext->m_pContext);
pSocketContext->m_Handle = handlesys->CreateHandle(g_AsyncSocket.socketHandleType, pSocketContext, pSocketContext->m_Handle = handlesys->CreateHandle(g_AsyncSocket.socketHandleType, pSocketContext,
pConnect->pSocketContext->m_pContext->GetIdentity(), myself->GetIdentity(), NULL); pConnect->pSocketContext->m_pContext->GetIdentity(), myself->GetIdentity(), NULL);
@ -114,7 +109,6 @@ void OnGameFrame(bool simulating)
if(!pSocketContext->m_Deleted) if(!pSocketContext->m_Deleted)
{ {
printf("add UV_StartRead\n");
CAsyncAddJob Job; CAsyncAddJob Job;
Job.CallbackFn = UV_StartRead; Job.CallbackFn = UV_StartRead;
Job.pData = pSocketContext; Job.pData = pSocketContext;
@ -125,7 +119,6 @@ void OnGameFrame(bool simulating)
} }
else else
{ {
printf("client\n");
pConnect->pSocketContext->Connected(); pConnect->pSocketContext->Connected();
} }
@ -161,9 +154,7 @@ void UV_OnAsyncAdded(uv_async_t *pHandle)
CAsyncAddJob Job; CAsyncAddJob Job;
while(g_AsyncAddQueue.try_dequeue(Job)) while(g_AsyncAddQueue.try_dequeue(Job))
{ {
printf("dequeing job\n");
uv_async_t *pAsync = (uv_async_t *)malloc(sizeof(uv_async_t)); uv_async_t *pAsync = (uv_async_t *)malloc(sizeof(uv_async_t));
g_MallocHandles++;
uv_async_init(g_UV_Loop, pAsync, Job.CallbackFn); uv_async_init(g_UV_Loop, pAsync, Job.CallbackFn);
pAsync->data = Job.pData; pAsync->data = Job.pData;
pAsync->close_cb = UV_FreeHandle; pAsync->close_cb = UV_FreeHandle;
@ -173,9 +164,7 @@ void UV_OnAsyncAdded(uv_async_t *pHandle)
void UV_FreeHandle(uv_handle_t *handle) void UV_FreeHandle(uv_handle_t *handle)
{ {
printf("freeing %p\n", handle);
free(handle); free(handle);
g_MallocHandles--;
} }
void UV_AllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) void UV_AllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
@ -186,7 +175,6 @@ void UV_AllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
void UV_Quit(uv_async_t *pHandle) void UV_Quit(uv_async_t *pHandle)
{ {
printf("quithandle: %p\n", pHandle);
uv_close((uv_handle_t *)pHandle, pHandle->close_cb); uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
uv_close((uv_handle_t *)&g_UV_AsyncAdded, NULL); uv_close((uv_handle_t *)&g_UV_AsyncAdded, NULL);
@ -198,12 +186,14 @@ void UV_DeleteAsyncContext(uv_async_t *pHandle)
{ {
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data; CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data;
uv_close((uv_handle_t *)pHandle, pHandle->close_cb); uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
if(pSocketContext->m_pStream) if(pSocketContext->m_pStream)
{ {
uv_close((uv_handle_t *)pSocketContext->m_pStream, pSocketContext->m_pStream->close_cb); uv_close((uv_handle_t *)pSocketContext->m_pStream, pSocketContext->m_pStream->close_cb);
pSocketContext->m_pStream = NULL; pSocketContext->m_pStream = NULL;
pSocketContext->m_pSocket = NULL; pSocketContext->m_pSocket = NULL;
} }
if(pSocketContext->m_pSocket) if(pSocketContext->m_pSocket)
{ {
uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb); uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb);
@ -226,11 +216,9 @@ void UV_PushError(CAsyncSocketContext *pSocketContext, int error)
void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
{ {
printf("UV_OnRead()\n");
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)client->data; CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)client->data;
if(pSocketContext->m_Deleted) if(pSocketContext->m_Deleted)
{ {
printf("m_Deleted\n");
free(buf->base); free(buf->base);
uv_close((uv_handle_t *)client, client->close_cb); uv_close((uv_handle_t *)client, client->close_cb);
pSocketContext->m_pStream = NULL; pSocketContext->m_pStream = NULL;
@ -240,7 +228,6 @@ void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
if(nread < 0) if(nread < 0)
{ {
printf("nread < 0\n");
// Connection closed // Connection closed
free(buf->base); free(buf->base);
// But let the client disconnect. // But let the client disconnect.
@ -251,9 +238,8 @@ void UV_OnRead(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
UV_PushError(pSocketContext, nread); UV_PushError(pSocketContext, nread);
return; return;
} }
pSocketContext->m_PendingCallback = true;
printf("nread = %d\n", nread); pSocketContext->m_PendingCallback = true;
char *data = (char *)malloc(sizeof(char) * (nread + 1)); char *data = (char *)malloc(sizeof(char) * (nread + 1));
data[nread] = 0; data[nread] = 0;
@ -284,6 +270,7 @@ void UV_OnConnect(uv_connect_t *req, int status)
UV_PushError(pSocketContext, status); UV_PushError(pSocketContext, status);
return; return;
} }
pSocketContext->m_PendingCallback = true; pSocketContext->m_PendingCallback = true;
pSocketContext->m_pStream = req->handle; pSocketContext->m_pStream = req->handle;
@ -300,35 +287,27 @@ void UV_OnConnect(uv_connect_t *req, int status)
void UV_StartRead(uv_async_t *pHandle) void UV_StartRead(uv_async_t *pHandle)
{ {
printf("UV_StartRead()\n");
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data; CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)pHandle->data;
uv_close((uv_handle_t *)pHandle, pHandle->close_cb); uv_close((uv_handle_t *)pHandle, pHandle->close_cb);
if(pSocketContext->m_Deleted || !pSocketContext->m_pStream) if(pSocketContext->m_Deleted || !pSocketContext->m_pStream)
{
printf("UV_StartRead() -> error\n");
return; return;
}
int err = uv_read_start(pSocketContext->m_pStream, UV_AllocBuffer, UV_OnRead); uv_read_start(pSocketContext->m_pStream, UV_AllocBuffer, UV_OnRead);
printf("~UV_StartRead() = %d %s\n", err, uv_err_name(err));
} }
void UV_OnNewConnection(uv_stream_t *server, int status) void UV_OnNewConnection(uv_stream_t *server, int status)
{ {
printf("UV_OnNewConnection()\n");
// server context // server context
CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)server->data; CAsyncSocketContext *pSocketContext = (CAsyncSocketContext *)server->data;
if(pSocketContext->m_Deleted) if(pSocketContext->m_Deleted)
{ {
printf("m_Deleted()\n");
uv_close((uv_handle_t *)server, server->close_cb); uv_close((uv_handle_t *)server, server->close_cb);
return; return;
} }
if(status < 0) if(status < 0)
{ {
printf("status < 0\n");
uv_close((uv_handle_t *)server, server->close_cb); uv_close((uv_handle_t *)server, server->close_cb);
//uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb); //uv_close((uv_handle_t *)pSocketContext->m_pSocket, pSocketContext->m_pSocket->close_cb);
UV_PushError(pSocketContext, status); UV_PushError(pSocketContext, status);
@ -336,13 +315,11 @@ void UV_OnNewConnection(uv_stream_t *server, int status)
} }
uv_tcp_t *pClientSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t)); uv_tcp_t *pClientSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
g_MallocHandles++;
uv_tcp_init(g_UV_Loop, pClientSocket); uv_tcp_init(g_UV_Loop, pClientSocket);
pClientSocket->close_cb = UV_FreeHandle; pClientSocket->close_cb = UV_FreeHandle;
if(uv_accept((uv_stream_t *)pSocketContext->m_pSocket, (uv_stream_t *)pClientSocket) == 0) if(uv_accept((uv_stream_t *)pSocketContext->m_pSocket, (uv_stream_t *)pClientSocket) == 0)
{ {
printf("accepted\n");
pSocketContext->m_PendingCallback = true; pSocketContext->m_PendingCallback = true;
CSocketConnect *pConnect = (CSocketConnect *)malloc(sizeof(CSocketConnect)); CSocketConnect *pConnect = (CSocketConnect *)malloc(sizeof(CSocketConnect));
pConnect->pSocketContext = pSocketContext; pConnect->pSocketContext = pSocketContext;
@ -351,7 +328,6 @@ void UV_OnNewConnection(uv_stream_t *server, int status)
} }
else else
{ {
printf("accept failed\n");
uv_close((uv_handle_t *)pClientSocket, pClientSocket->close_cb); uv_close((uv_handle_t *)pClientSocket, pClientSocket->close_cb);
} }
} }
@ -376,11 +352,7 @@ void UV_OnAsyncResolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo
return; return;
} }
uv_connect_t *pConnectReq = (uv_connect_t *)malloc(sizeof(uv_connect_t));
pConnectReq->data = pSocketContext;
uv_tcp_t *pSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t)); uv_tcp_t *pSocket = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));
g_MallocHandles++;
uv_tcp_init(g_UV_Loop, pSocket); uv_tcp_init(g_UV_Loop, pSocket);
pSocket->close_cb = UV_FreeHandle; pSocket->close_cb = UV_FreeHandle;
pSocket->data = pSocketContext; pSocket->data = pSocketContext;
@ -390,22 +362,21 @@ void UV_OnAsyncResolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo
if(pSocketContext->m_Server) if(pSocketContext->m_Server)
{ {
printf("new server\n");
uv_tcp_bind(pSocket, (const struct sockaddr *)res->ai_addr, 0); uv_tcp_bind(pSocket, (const struct sockaddr *)res->ai_addr, 0);
int err = uv_listen((uv_stream_t *)pSocket, 32, UV_OnNewConnection); int err = uv_listen((uv_stream_t *)pSocket, 32, UV_OnNewConnection);
if(err) if(err)
{ {
printf("yes, new server\n");
uv_freeaddrinfo(res);
uv_close((uv_handle_t *)pSocket, pSocket->close_cb); uv_close((uv_handle_t *)pSocket, pSocket->close_cb);
pSocketContext->m_pSocket = NULL;
UV_PushError(pSocketContext, err); UV_PushError(pSocketContext, err);
return;
} }
} }
else else
{ {
printf("connect client\n"); uv_connect_t *pConnectReq = (uv_connect_t *)malloc(sizeof(uv_connect_t));
pConnectReq->data = pSocketContext;
uv_tcp_connect(pConnectReq, pSocket, (const struct sockaddr *)res->ai_addr, UV_OnConnect); uv_tcp_connect(pConnectReq, pSocket, (const struct sockaddr *)res->ai_addr, UV_OnConnect);
} }
@ -452,7 +423,6 @@ void UV_OnAsyncWriteCleanup(uv_write_t *req, int status)
void UV_OnAsyncWrite(uv_async_t *handle) void UV_OnAsyncWrite(uv_async_t *handle)
{ {
printf("Write 1\n");
CAsyncWrite *pWrite = (CAsyncWrite *)handle->data; CAsyncWrite *pWrite = (CAsyncWrite *)handle->data;
uv_close((uv_handle_t *)handle, handle->close_cb); uv_close((uv_handle_t *)handle, handle->close_cb);
@ -470,7 +440,6 @@ void UV_OnAsyncWrite(uv_async_t *handle)
uv_write_t *req = (uv_write_t *)malloc(sizeof(uv_write_t)); uv_write_t *req = (uv_write_t *)malloc(sizeof(uv_write_t));
req->data = pWrite; req->data = pWrite;
printf("Write 2\n");
uv_write(req, pWrite->pSocketContext->m_pStream, pWrite->pBuffer, 1, UV_OnAsyncWriteCleanup); uv_write(req, pWrite->pSocketContext->m_pStream, pWrite->pBuffer, 1, UV_OnAsyncWriteCleanup);
} }
@ -563,13 +532,19 @@ cell_t Native_AsyncSocket_Write(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Socket is not connected"); return pContext->ThrowNativeError("Socket is not connected");
char *data = NULL; char *data = NULL;
pContext->LocalToString(params[2], &data); pContext->LocalToPhysAddr(params[2], (cell_t **)&data);
uv_buf_t *buffer = (uv_buf_t *)malloc(sizeof(uv_buf_t)); uv_buf_t *buffer = (uv_buf_t *)malloc(sizeof(uv_buf_t));
buffer->base = strdup(data); if(params[3] >= 0)
buffer->len = params[3];
else
buffer->len = strlen(data); buffer->len = strlen(data);
buffer->base = (char *)malloc(buffer->len + 1);
memcpy(buffer->base, data, buffer->len + 1);
buffer->base[buffer->len] = 0;
CAsyncWrite *pWrite = (CAsyncWrite *)malloc(sizeof(CAsyncWrite)); CAsyncWrite *pWrite = (CAsyncWrite *)malloc(sizeof(CAsyncWrite));
pWrite->pSocketContext = pSocketContext; pWrite->pSocketContext = pSocketContext;
@ -628,7 +603,6 @@ cell_t Native_AsyncSocket_SetDataCallback(IPluginContext *pContext, const cell_t
bool AsyncSocket::SDK_OnLoad(char *error, size_t maxlength, bool late) bool AsyncSocket::SDK_OnLoad(char *error, size_t maxlength, bool late)
{ {
g_Running = true; g_Running = true;
g_MallocHandles = 0;
sharesys->AddNatives(myself, AsyncSocketNatives); sharesys->AddNatives(myself, AsyncSocketNatives);
sharesys->RegisterLibrary(myself, "AsyncSocket"); sharesys->RegisterLibrary(myself, "AsyncSocket");
@ -672,7 +646,6 @@ void AsyncSocket::SDK_OnUnload()
uv_loop_close(g_UV_Loop); uv_loop_close(g_UV_Loop);
smutils->RemoveGameFrameHook(OnGameFrame); smutils->RemoveGameFrameHook(OnGameFrame);
printf("g_MallocHandles = %d\n", g_MallocHandles);
} }
const sp_nativeinfo_t AsyncSocketNatives[] = { const sp_nativeinfo_t AsyncSocketNatives[] = {

View File

@ -40,7 +40,7 @@
/* Basic information exposed publicly */ /* Basic information exposed publicly */
#define SMEXT_CONF_NAME "Async Socket Extension" #define SMEXT_CONF_NAME "Async Socket Extension"
#define SMEXT_CONF_DESCRIPTION "Async TCP server and client" #define SMEXT_CONF_DESCRIPTION "Async TCP server and client"
#define SMEXT_CONF_VERSION "0.1" #define SMEXT_CONF_VERSION "1.0"
#define SMEXT_CONF_AUTHOR "Nikki + BotoX" #define SMEXT_CONF_AUTHOR "Nikki + BotoX"
#define SMEXT_CONF_URL "https://github.com/CSSZombieEscape/sm-ext-AsyncSocket" #define SMEXT_CONF_URL "https://github.com/CSSZombieEscape/sm-ext-AsyncSocket"
#define SMEXT_CONF_LOGTAG "ASYNCSOCKET" #define SMEXT_CONF_LOGTAG "ASYNCSOCKET"

View File

@ -16,7 +16,7 @@ methodmap AsyncSocket < Handle {
public native bool Listen(const char[] host, const int port); public native bool Listen(const char[] host, const int port);
public native bool Write(const char[] data); public native bool Write(const char[] data, int length = -1);
public native bool SetConnectCallback(AsyncSocketConnectCallback callback); public native bool SetConnectCallback(AsyncSocketConnectCallback callback);

View File