2017-01-08 16:36:28 +01:00
|
|
|
#include "extension.h"
|
2015-05-21 19:29:14 +02:00
|
|
|
#include "context.h"
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncSocketContext::CAsyncSocketContext(IPluginContext *pContext)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
m_pContext = pContext;
|
2015-05-21 23:23:53 +02:00
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
m_pHost = NULL;
|
|
|
|
m_Port = -1;
|
|
|
|
|
|
|
|
m_Deleted = false;
|
|
|
|
m_PendingCallback = false;
|
|
|
|
m_Pending = false;
|
|
|
|
m_Server = false;
|
|
|
|
|
|
|
|
m_pSocket = NULL;
|
|
|
|
m_pStream = NULL;
|
2015-05-24 04:57:44 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
m_pConnectCallback = NULL;
|
|
|
|
m_pErrorCallback = NULL;
|
|
|
|
m_pDataCallback = NULL;
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
CAsyncSocketContext::~CAsyncSocketContext()
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
if(m_pHost)
|
|
|
|
free(m_pHost);
|
2015-05-21 22:14:29 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(m_pConnectCallback)
|
|
|
|
forwards->ReleaseForward(m_pConnectCallback);
|
2015-05-21 22:14:29 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(m_pErrorCallback)
|
|
|
|
forwards->ReleaseForward(m_pErrorCallback);
|
2015-05-21 19:29:14 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
if(m_pDataCallback)
|
|
|
|
forwards->ReleaseForward(m_pDataCallback);
|
2017-01-08 16:36:28 +01:00
|
|
|
|
|
|
|
m_Deleted = true;
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 16:36:28 +01:00
|
|
|
// Client
|
2016-12-24 12:12:27 +01:00
|
|
|
void CAsyncSocketContext::Connected()
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
m_PendingCallback = false;
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!m_pConnectCallback)
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
m_pConnectCallback->PushCell(m_Handle);
|
2017-01-08 16:36:28 +01:00
|
|
|
m_pConnectCallback->Execute(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server
|
|
|
|
void CAsyncSocketContext::OnConnect(CAsyncSocketContext *pSocketContext)
|
|
|
|
{
|
|
|
|
m_PendingCallback = false;
|
|
|
|
if(!m_pConnectCallback)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_pConnectCallback->PushCell(pSocketContext->m_Handle);
|
|
|
|
m_pConnectCallback->Execute(NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void CAsyncSocketContext::OnError(int error)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
m_PendingCallback = false;
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!m_pErrorCallback)
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
m_pErrorCallback->PushCell(m_Handle);
|
|
|
|
m_pErrorCallback->PushCell(error);
|
|
|
|
m_pErrorCallback->PushString(uv_err_name(error));
|
|
|
|
m_pErrorCallback->Execute(NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
void CAsyncSocketContext::OnData(char* data, ssize_t size)
|
|
|
|
{
|
2017-01-08 16:36:28 +01:00
|
|
|
m_PendingCallback = false;
|
2016-12-24 12:12:27 +01:00
|
|
|
if(!m_pDataCallback)
|
2015-05-21 19:29:14 +02:00
|
|
|
return;
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
m_pDataCallback->PushCell(m_Handle);
|
|
|
|
m_pDataCallback->PushString(data);
|
|
|
|
m_pDataCallback->PushCell(size);
|
2017-01-08 16:36:28 +01:00
|
|
|
m_pDataCallback->Execute(NULL);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
bool CAsyncSocketContext::SetConnectCallback(funcid_t function)
|
|
|
|
{
|
|
|
|
if(m_pConnectCallback)
|
|
|
|
forwards->ReleaseForward(m_pConnectCallback);
|
|
|
|
|
|
|
|
m_pConnectCallback = forwards->CreateForwardEx(NULL, ET_Single, 1, NULL, Param_Cell);
|
|
|
|
return m_pConnectCallback->AddFunction(m_pContext, function);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
bool CAsyncSocketContext::SetErrorCallback(funcid_t function)
|
|
|
|
{
|
|
|
|
if(m_pConnectCallback)
|
|
|
|
forwards->ReleaseForward(m_pErrorCallback);
|
2015-05-21 23:23:53 +02:00
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
m_pErrorCallback = forwards->CreateForwardEx(NULL, ET_Single, 3, NULL, Param_Cell, Param_Cell, Param_String);
|
|
|
|
return m_pErrorCallback->AddFunction(m_pContext, function);
|
2015-05-21 19:29:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-24 12:12:27 +01:00
|
|
|
bool CAsyncSocketContext::SetDataCallback(funcid_t function)
|
|
|
|
{
|
|
|
|
if(m_pDataCallback)
|
|
|
|
forwards->ReleaseForward(m_pDataCallback);
|
|
|
|
|
|
|
|
m_pDataCallback = forwards->CreateForwardEx(NULL, ET_Single, 3, NULL, Param_Cell, Param_String, Param_Cell);
|
|
|
|
return m_pDataCallback->AddFunction(m_pContext, function);
|
|
|
|
}
|