改为V5.9.6
This commit is contained in:
@@ -208,9 +208,8 @@ void CBufferPool::PutFreeBuffer(TBuffer* pBuffer)
|
||||
{
|
||||
m_itPool.PutFreeItem(pBuffer->items);
|
||||
|
||||
#ifndef USE_EXTERNAL_GC
|
||||
ReleaseGCBuffer();
|
||||
#endif
|
||||
|
||||
if(!m_lsFreeBuffer.TryPut(pBuffer))
|
||||
m_lsGCBuffer.PushBack(pBuffer);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "STLHelper.h"
|
||||
#include "RingBuffer.h"
|
||||
@@ -800,6 +800,7 @@ public:
|
||||
void Prepare ();
|
||||
void Clear ();
|
||||
|
||||
private:
|
||||
void ReleaseGCBuffer (BOOL bForce = FALSE);
|
||||
|
||||
public:
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <malloc.h>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "FuncHelper.h"
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "FuncHelper.h"
|
||||
#include "PollHelper.h"
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "hpsocket/GlobalErrno.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalErrno.h"
|
||||
#include "SysHelper.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
@@ -102,7 +102,7 @@ using namespace std;
|
||||
#define IS_OK(rs) ((BOOL)(rs))
|
||||
#define IS_NOT_OK(rs) (!IS_OK(rs))
|
||||
|
||||
#define IS_ERROR(code) (::GetLastError() == (code))
|
||||
#define IS_ERROR(code) (::GetLastError() == code)
|
||||
#define CONTINUE_IF_ERROR(code) {if(IS_ERROR(code)) continue;}
|
||||
#define BREAK_IF_ERROR(code) {if(IS_ERROR(code)) break;}
|
||||
|
||||
@@ -133,7 +133,7 @@ inline void PrintError(LPCSTR subject) {perror(subject);}
|
||||
#define EXECUTE_RESTORE_ERROR(expr) {int __le_ = ::GetLastError(); (expr); ::SetLastError(__le_);}
|
||||
#define EXECUTE_RESTORE_ERROR_RT(T, expr)\
|
||||
({int __le_ = ::GetLastError(); T __rs_ = (expr); ::SetLastError(__le_); __rs_;})
|
||||
#define ENSURE_ERROR(def_code) ({int __le_ = ::GetLastError(); if(__le_ == NO_ERROR) __le_ = (def_code); __le_;})
|
||||
#define ENSURE_ERROR(def_code) ({int __le_ = ::GetLastError(); if(__le_ == 0) __le_ = (def_code); __le_;})
|
||||
#define ENSURE_ERROR_CANCELLED ENSURE_ERROR(ERROR_CANCELLED)
|
||||
#define TRIGGER(expr) EXECUTE_RESET_ERROR((expr))
|
||||
|
||||
@@ -171,9 +171,6 @@ inline void PrintError(LPCSTR subject) {perror(subject);}
|
||||
#define TO_PVOID(v) ((PVOID)(UINT_PTR)(v))
|
||||
#define FROM_PVOID(T, pv) ((T)(UINT_PTR)(pv))
|
||||
|
||||
#define IS_NULL(v) ((v) == nullptr)
|
||||
#define IS_NOT_NULL(v) (!IS_NULL(v))
|
||||
|
||||
#define stricmp strcasecmp
|
||||
#define strnicmp strncasecmp
|
||||
#define wcsicmp wcscasecmp
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "hpsocket/GlobalErrno.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalErrno.h"
|
||||
#include "Singleton.h"
|
||||
#include "STLHelper.h"
|
||||
#include "FuncHelper.h"
|
||||
|
||||
@@ -27,10 +27,10 @@
|
||||
#include <signal.h>
|
||||
#include <pthread.h>
|
||||
|
||||
volatile UINT CIODispatcher::sm_uiNum = MAXUINT;
|
||||
volatile UINT CIODispatcher::sm_uiNum = 0;
|
||||
LPCTSTR CIODispatcher::WORKER_THREAD_PREFIX = _T("io-disp-");
|
||||
|
||||
BOOL CIODispatcher::Start(IIOHandler* pHandler, int iWorkerMaxEvents, int iWorkers)
|
||||
BOOL CIODispatcher::Start(IIOHandler* pHandler, int iWorkerMaxEvents, int iWorkers, LLONG llTimerInterval)
|
||||
{
|
||||
ASSERT_CHECK_EINVAL(pHandler && iWorkerMaxEvents >= 0 && iWorkers >= 0);
|
||||
CHECK_ERROR(!HasStarted(), ERROR_INVALID_STATE);
|
||||
@@ -42,42 +42,44 @@ BOOL CIODispatcher::Start(IIOHandler* pHandler, int iWorkerMaxEvents, int iWorke
|
||||
m_iWorkers = iWorkers;
|
||||
m_pHandler = pHandler;
|
||||
|
||||
m_epoll = epoll_create1(EPOLL_CLOEXEC);
|
||||
CHECK_ERROR_FD(m_epoll);
|
||||
|
||||
m_evCmd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
||||
|
||||
if(IS_INVALID_FD(m_evCmd))
|
||||
goto START_ERROR;
|
||||
|
||||
if(!VERIFY(AddFD(m_evCmd, EPOLLIN | EPOLLET, &m_evCmd)))
|
||||
goto START_ERROR;
|
||||
|
||||
m_evExit = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE);
|
||||
|
||||
if(IS_INVALID_FD(m_evExit))
|
||||
goto START_ERROR;
|
||||
|
||||
m_pContexts = make_unique<TDispContext[]>(m_iWorkers);
|
||||
if(!VERIFY(AddFD(m_evExit, EPOLLIN, &m_evExit)))
|
||||
goto START_ERROR;
|
||||
|
||||
if(llTimerInterval > 0)
|
||||
{
|
||||
m_evTimer = AddTimer(llTimerInterval, &m_evTimer);
|
||||
|
||||
if(IS_INVALID_FD(m_evTimer))
|
||||
goto START_ERROR;
|
||||
}
|
||||
|
||||
sigset_t ss;
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, SIGPIPE);
|
||||
|
||||
VERIFY_IS_NO_ERROR(pthread_sigmask(SIG_BLOCK, &ss, nullptr));
|
||||
|
||||
m_pWorkers = make_unique<CWorkerThread[]>(m_iWorkers);
|
||||
|
||||
for(int i = 0; i < m_iWorkers; i++)
|
||||
{
|
||||
TDispContext& ctx = m_pContexts[i];
|
||||
|
||||
ctx.m_iIndex = i;
|
||||
|
||||
ctx.m_epoll = epoll_create1(EPOLL_CLOEXEC);
|
||||
CHECK_ERROR_FD(ctx.m_epoll);
|
||||
|
||||
ctx.m_evCmd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
||||
|
||||
if(IS_INVALID_FD(ctx.m_evCmd))
|
||||
goto START_ERROR;
|
||||
|
||||
if(!VERIFY(AddFD(i, ctx.m_evCmd, EPOLLIN | EPOLLET, &ctx.m_evCmd)))
|
||||
goto START_ERROR;
|
||||
|
||||
if(!VERIFY(AddFD(i, m_evExit, EPOLLIN, &m_evExit)))
|
||||
goto START_ERROR;
|
||||
|
||||
sigset_t ss;
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, SIGPIPE);
|
||||
|
||||
VERIFY_IS_NO_ERROR(pthread_sigmask(SIG_BLOCK, &ss, nullptr));
|
||||
|
||||
ctx.m_pWorker = make_unique<CWorkerThread>();
|
||||
|
||||
if(!VERIFY(ctx.m_pWorker->Start(this, &CIODispatcher::WorkerProc, &ctx)))
|
||||
if(!VERIFY(m_pWorkers[i].Start(this, &CIODispatcher::WorkerProc)))
|
||||
goto START_ERROR;
|
||||
}
|
||||
|
||||
@@ -94,38 +96,36 @@ BOOL CIODispatcher::Stop(BOOL bCheck)
|
||||
|
||||
BOOL isOK = TRUE;
|
||||
|
||||
if(m_pContexts)
|
||||
if(m_pWorkers)
|
||||
{
|
||||
isOK &= IS_NO_ERROR(eventfd_write(m_evExit, m_iWorkers));
|
||||
|
||||
for(int i = 0; i < m_iWorkers; i++)
|
||||
{
|
||||
TDispContext& ctx = m_pContexts[i];
|
||||
isOK &= m_pWorkers[i].Join();
|
||||
}
|
||||
|
||||
if(ctx.m_pWorker)
|
||||
isOK &= ctx.m_pWorker->Join();
|
||||
if(!m_queue.IsEmpty())
|
||||
{
|
||||
TDispCommand* pCmd = nullptr;
|
||||
|
||||
if(!ctx.m_queue.IsEmpty())
|
||||
{
|
||||
TDispCommand* pCmd = nullptr;
|
||||
while(m_queue.PopFront(&pCmd))
|
||||
TDispCommand::Destruct(pCmd);
|
||||
|
||||
while(ctx.m_queue.PopFront(&pCmd))
|
||||
TDispCommand::Destruct(pCmd);
|
||||
|
||||
VERIFY(ctx.m_queue.IsEmpty());
|
||||
}
|
||||
|
||||
if(IS_VALID_FD(ctx.m_evCmd))
|
||||
isOK &= IS_NO_ERROR(close(ctx.m_evCmd));
|
||||
|
||||
if(IS_VALID_FD(ctx.m_epoll))
|
||||
isOK &= IS_NO_ERROR(close(ctx.m_epoll));
|
||||
}
|
||||
VERIFY(m_queue.IsEmpty());
|
||||
}
|
||||
|
||||
if(IS_VALID_FD(m_evExit))
|
||||
isOK &= IS_NO_ERROR(close(m_evExit));
|
||||
|
||||
if(IS_VALID_FD(m_evCmd))
|
||||
isOK &= IS_NO_ERROR(close(m_evCmd));
|
||||
|
||||
if(IS_VALID_FD(m_evTimer))
|
||||
isOK &= IS_NO_ERROR(close(m_evTimer));
|
||||
|
||||
if(IS_VALID_FD(m_epoll))
|
||||
isOK &= IS_NO_ERROR(close(m_epoll));
|
||||
|
||||
Reset();
|
||||
|
||||
return isOK;
|
||||
@@ -133,78 +133,54 @@ BOOL CIODispatcher::Stop(BOOL bCheck)
|
||||
|
||||
VOID CIODispatcher::Reset()
|
||||
{
|
||||
m_uiSeq = MAXUINT;
|
||||
m_uiSeq = 0;
|
||||
m_iWorkers = 0;
|
||||
m_iMaxEvents= 0;
|
||||
m_evExit = INVALID_FD;
|
||||
m_pHandler = nullptr;
|
||||
m_pContexts = nullptr;
|
||||
m_pWorkers = nullptr;
|
||||
m_epoll = INVALID_FD;
|
||||
m_evCmd = INVALID_FD;
|
||||
m_evExit = INVALID_FD;
|
||||
m_evTimer = INVALID_FD;
|
||||
}
|
||||
|
||||
VOID CIODispatcher::MakePrefix()
|
||||
|
||||
void CIODispatcher::MakePrefix()
|
||||
{
|
||||
UINT uiNumber = ::InterlockedIncrement(&sm_uiNum);
|
||||
|
||||
m_strPrefix.Format(_T("%s%u-"), WORKER_THREAD_PREFIX, uiNumber);
|
||||
}
|
||||
|
||||
TDispContext& CIODispatcher::GetContext(int idx, FD fd)
|
||||
BOOL CIODispatcher::SendCommand(USHORT t, UINT_PTR wp, UINT_PTR lp)
|
||||
{
|
||||
if(idx < 0) idx = fd;
|
||||
ASSERT(idx >= 0);
|
||||
if(idx >= m_iWorkers) idx %= m_iWorkers;
|
||||
|
||||
return m_pContexts[idx];
|
||||
return SendCommand(TDispCommand::Construct(t, wp, lp));
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::SendCommandByIndex(int idx, USHORT t, UINT_PTR wp, UINT_PTR lp)
|
||||
BOOL CIODispatcher::SendCommand(TDispCommand* pCmd)
|
||||
{
|
||||
return SendCommandByIndex(idx, TDispCommand::Construct(t, wp, lp));
|
||||
m_queue.PushBack(pCmd);
|
||||
return VERIFY_IS_NO_ERROR(eventfd_write(m_evCmd, 1));
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::SendCommandByIndex(int idx, TDispCommand* pCmd)
|
||||
BOOL CIODispatcher::CtlFD(FD fd, int op, UINT mask, PVOID pv)
|
||||
{
|
||||
TDispContext& ctx = GetContextByIndex(idx);
|
||||
return SendCommand(ctx, pCmd);
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::SendCommandByFD(FD fd, USHORT t, UINT_PTR wp, UINT_PTR lp)
|
||||
{
|
||||
return SendCommandByFD(fd, TDispCommand::Construct(t, wp, lp));
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::SendCommandByFD(FD fd, TDispCommand* pCmd)
|
||||
{
|
||||
TDispContext& ctx = GetContextByFD(fd);
|
||||
return SendCommand(ctx, pCmd);
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::SendCommand(TDispContext& ctx, TDispCommand* pCmd)
|
||||
{
|
||||
ctx.m_queue.PushBack(pCmd);
|
||||
return VERIFY_IS_NO_ERROR(eventfd_write(ctx.m_evCmd, 1));
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::CtlFD(int idx, FD fd, int op, UINT mask, PVOID pv)
|
||||
{
|
||||
const TDispContext& ctx = GetContext(idx, fd);
|
||||
|
||||
epoll_event evt = {mask, pv};
|
||||
return IS_NO_ERROR(epoll_ctl(ctx.m_epoll, op, fd, &evt));
|
||||
return IS_NO_ERROR(epoll_ctl(m_epoll, op, fd, &evt));
|
||||
}
|
||||
|
||||
int CIODispatcher::WorkerProc(TDispContext* pContext)
|
||||
int CIODispatcher::WorkerProc(PVOID pv)
|
||||
{
|
||||
::SetSequenceThreadName(SELF_THREAD_ID, m_strPrefix, m_uiSeq);
|
||||
|
||||
m_pHandler->OnDispatchThreadStart(SELF_THREAD_ID);
|
||||
|
||||
BOOL bRun = TRUE;
|
||||
unique_ptr<epoll_event[]> pEvents = make_unique<epoll_event[]>(m_iMaxEvents);
|
||||
BOOL bRun = TRUE;
|
||||
unique_ptr<epoll_event[]> pEvents = make_unique<epoll_event[]>(m_iMaxEvents);
|
||||
|
||||
while(bRun)
|
||||
{
|
||||
int rs = NO_EINTR_INT(epoll_pwait(pContext->m_epoll, pEvents.get(), m_iMaxEvents, INFINITE, nullptr));
|
||||
int rs = NO_EINTR_INT(epoll_pwait(m_epoll, pEvents.get(), m_iMaxEvents, INFINITE, nullptr));
|
||||
|
||||
if(rs <= TIMEOUT)
|
||||
ERROR_ABORT();
|
||||
@@ -214,12 +190,14 @@ int CIODispatcher::WorkerProc(TDispContext* pContext)
|
||||
UINT events = pEvents[i].events;
|
||||
PVOID ptr = pEvents[i].data.ptr;
|
||||
|
||||
if(ptr == &pContext->m_evCmd)
|
||||
ProcessCommand(pContext, events);
|
||||
if(ptr == &m_evCmd)
|
||||
ProcessCommand(events);
|
||||
else if(ptr == &m_evTimer)
|
||||
ProcessTimer(events);
|
||||
else if(ptr == &m_evExit)
|
||||
bRun = ProcessExit(pContext, events);
|
||||
bRun = ProcessExit(events);
|
||||
else
|
||||
ProcessIo(pContext, ptr, events);
|
||||
ProcessIo(ptr, events);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +206,7 @@ int CIODispatcher::WorkerProc(TDispContext* pContext)
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::ProcessCommand(TDispContext* pContext, UINT events)
|
||||
BOOL CIODispatcher::ProcessCommand(UINT events)
|
||||
{
|
||||
if(events & _EPOLL_ALL_ERROR_EVENTS)
|
||||
ERROR_ABORT();
|
||||
@@ -240,7 +218,7 @@ BOOL CIODispatcher::ProcessCommand(TDispContext* pContext, UINT events)
|
||||
|
||||
eventfd_t v;
|
||||
|
||||
int rs = eventfd_read(pContext->m_evCmd, &v);
|
||||
int rs = eventfd_read(m_evCmd, &v);
|
||||
|
||||
if(IS_NO_ERROR(rs))
|
||||
{
|
||||
@@ -248,9 +226,9 @@ BOOL CIODispatcher::ProcessCommand(TDispContext* pContext, UINT events)
|
||||
|
||||
TDispCommand* pCmd = nullptr;
|
||||
|
||||
while(pContext->m_queue.PopFront(&pCmd))
|
||||
while(m_queue.PopFront(&pCmd))
|
||||
{
|
||||
m_pHandler->OnCommand(pContext, pCmd);
|
||||
m_pHandler->OnCommand(pCmd);
|
||||
TDispCommand::Destruct(pCmd);
|
||||
}
|
||||
}
|
||||
@@ -264,7 +242,26 @@ BOOL CIODispatcher::ProcessCommand(TDispContext* pContext, UINT events)
|
||||
return isOK;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::ProcessExit(const TDispContext* pContext, UINT events)
|
||||
BOOL CIODispatcher::ProcessTimer(UINT events)
|
||||
{
|
||||
if(events & _EPOLL_ALL_ERROR_EVENTS)
|
||||
ERROR_ABORT();
|
||||
|
||||
if(!(events & EPOLLIN))
|
||||
return TRUE;
|
||||
|
||||
BOOL isOK = FALSE;
|
||||
ULLONG ullExpirations;
|
||||
|
||||
if(::ReadTimer(m_evTimer, &ullExpirations, &isOK) && isOK)
|
||||
m_pHandler->OnTimer(ullExpirations);
|
||||
else
|
||||
ASSERT(IS_WOULDBLOCK_ERROR());
|
||||
|
||||
return isOK;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::ProcessExit(UINT events)
|
||||
{
|
||||
if(events & _EPOLL_ALL_ERROR_EVENTS)
|
||||
ERROR_ABORT();
|
||||
@@ -289,40 +286,40 @@ BOOL CIODispatcher::ProcessExit(const TDispContext* pContext, UINT events)
|
||||
return bRun;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::ProcessIo(const TDispContext* pContext, PVOID pv, UINT events)
|
||||
BOOL CIODispatcher::ProcessIo(PVOID pv, UINT events)
|
||||
{
|
||||
if(!m_pHandler->OnBeforeProcessIo(pContext, pv, events))
|
||||
if(!m_pHandler->OnBeforeProcessIo(pv, events))
|
||||
return FALSE;
|
||||
|
||||
BOOL rs = DoProcessIo(pContext, pv, events);
|
||||
m_pHandler->OnAfterProcessIo(pContext, pv, events, rs);
|
||||
BOOL rs = DoProcessIo(pv, events);
|
||||
m_pHandler->OnAfterProcessIo(pv, events, rs);
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::DoProcessIo(const TDispContext* pContext, PVOID pv, UINT events)
|
||||
BOOL CIODispatcher::DoProcessIo(PVOID pv, UINT events)
|
||||
{
|
||||
if(events & EPOLLERR)
|
||||
return m_pHandler->OnError(pContext, pv, events);
|
||||
if((events & EPOLLPRI) && !m_pHandler->OnReadyPrivilege(pContext, pv, events))
|
||||
return m_pHandler->OnError(pv, events);
|
||||
if((events & EPOLLPRI) && !m_pHandler->OnReadyPrivilege(pv, events))
|
||||
return FALSE;
|
||||
if((events & EPOLLIN) && !m_pHandler->OnReadyRead(pContext, pv, events))
|
||||
if((events & EPOLLIN) && !m_pHandler->OnReadyRead(pv, events))
|
||||
return FALSE;
|
||||
if((events & EPOLLOUT) && !m_pHandler->OnReadyWrite(pContext, pv, events))
|
||||
if((events & EPOLLOUT) && !m_pHandler->OnReadyWrite(pv, events))
|
||||
return FALSE;
|
||||
if((events & (_EPOLL_HUNGUP_EVENTS)) && !m_pHandler->OnHungUp(pContext, pv, events))
|
||||
if((events & (_EPOLL_HUNGUP_EVENTS)) && !m_pHandler->OnHungUp(pv, events))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
FD CIODispatcher::AddTimer(int idx, LLONG llInterval, PVOID pv)
|
||||
FD CIODispatcher::AddTimer(LLONG llInterval, PVOID pv)
|
||||
{
|
||||
FD fdTimer = ::CreateTimer(llInterval);
|
||||
|
||||
if(IS_VALID_FD(fdTimer))
|
||||
{
|
||||
if(!AddFD(idx, fdTimer, EPOLLIN | EPOLLET, pv))
|
||||
if(!AddFD(fdTimer, EPOLLIN | EPOLLET, pv))
|
||||
{
|
||||
close(fdTimer);
|
||||
fdTimer = INVALID_FD;
|
||||
@@ -332,13 +329,13 @@ FD CIODispatcher::AddTimer(int idx, LLONG llInterval, PVOID pv)
|
||||
return fdTimer;
|
||||
}
|
||||
|
||||
BOOL CIODispatcher::DelTimer(int idx, FD fdTimer)
|
||||
BOOL CIODispatcher::DelTimer(FD fdTimer)
|
||||
{
|
||||
BOOL isOK = FALSE;
|
||||
|
||||
if(IS_VALID_FD(fdTimer))
|
||||
{
|
||||
if(DelFD(idx, fdTimer))
|
||||
if(DelFD(fdTimer))
|
||||
isOK = TRUE;
|
||||
|
||||
close(fdTimer);
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "RingBuffer.h"
|
||||
#include "Thread.h"
|
||||
@@ -48,61 +48,16 @@ using namespace std;
|
||||
#define _EPOLL_ALL_NORMAL_EVENTS (_EPOLL_NORMAL_RW_EVENTS | _EPOLL_ALL_ERROR_EVENTS)
|
||||
#define _EPOLL_ALL_EVENTS (_EPOLL_ALL_RW_EVENTS | _EPOLL_ALL_ERROR_EVENTS)
|
||||
|
||||
#define DISP_EVENT_FLAG_R 0x1
|
||||
#define DISP_EVENT_FLAG_W 0x2
|
||||
#define DISP_EVENT_FLAG_H 0x4
|
||||
|
||||
#define DISP_EVENT_FLAG_R 1
|
||||
#define DISP_EVENT_FLAG_W 2
|
||||
#define DISP_EVENT_FLAG_H 4
|
||||
#define RETRIVE_EVENT_FLAG_R(evt) ((evt) & (_EPOLL_ALL_READ_EVENTS) ? DISP_EVENT_FLAG_R : 0)
|
||||
#define RETRIVE_EVENT_FLAG_W(evt) ((evt) & (_EPOLL_WRITE_EVENTS) ? DISP_EVENT_FLAG_W : 0)
|
||||
#define RETRIVE_EVENT_FLAG_RW(evt) (RETRIVE_EVENT_FLAG_R(evt) | RETRIVE_EVENT_FLAG_W(evt))
|
||||
#define RETRIVE_EVENT_FLAG_H(evt) ((evt) & (_EPOLL_HUNGUP_EVENTS) ? DISP_EVENT_FLAG_H : 0)
|
||||
|
||||
#ifndef EPOLLEXCLUSIVE
|
||||
#define EPOLLEXCLUSIVE (1u << 28)
|
||||
#endif
|
||||
|
||||
#define MAYBE_EPOLLEXCLUSIVE (::IsKernelVersionAbove(4, 5, 0) ? EPOLLEXCLUSIVE : 0)
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------------------------- //
|
||||
|
||||
struct TDispCommand;
|
||||
class CIODispatcher;
|
||||
|
||||
struct TDispContext
|
||||
{
|
||||
friend class CIODispatcher;
|
||||
|
||||
using CCommandQueue = CCASQueue<TDispCommand>;
|
||||
using CWorkerThread = CThread<CIODispatcher, TDispContext, int>;
|
||||
|
||||
public:
|
||||
int GetIndex() const {return m_iIndex;}
|
||||
THR_ID GetThreadId() const {return m_pWorker != nullptr ? m_pWorker->GetThreadID() : 0;}
|
||||
|
||||
public:
|
||||
TDispContext() {Reset();}
|
||||
~TDispContext() = default;
|
||||
|
||||
DECLARE_NO_COPY_CLASS(TDispContext)
|
||||
|
||||
private:
|
||||
VOID Reset()
|
||||
{
|
||||
m_iIndex = -1;
|
||||
m_epoll = INVALID_FD;
|
||||
m_evCmd = INVALID_FD;
|
||||
m_pWorker = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_iIndex;
|
||||
FD m_epoll;
|
||||
FD m_evCmd;
|
||||
|
||||
CCommandQueue m_queue;
|
||||
unique_ptr<CWorkerThread> m_pWorker;
|
||||
};
|
||||
|
||||
struct TDispCommand
|
||||
{
|
||||
USHORT type;
|
||||
@@ -129,18 +84,20 @@ private:
|
||||
class IIOHandler
|
||||
{
|
||||
public:
|
||||
virtual VOID OnCommand(const TDispContext* pContext, TDispCommand* pCmd) = 0;
|
||||
|
||||
virtual BOOL OnBeforeProcessIo(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual VOID OnAfterProcessIo(const TDispContext* pContext, PVOID pv, UINT events, BOOL rs) = 0;
|
||||
virtual BOOL OnReadyRead(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnReadyWrite(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnHungUp(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnError(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnReadyPrivilege(const TDispContext* pContext, PVOID pv, UINT events) = 0;
|
||||
virtual VOID OnCommand(TDispCommand* pCmd) = 0;
|
||||
virtual VOID OnTimer(ULLONG llExpirations) = 0;
|
||||
|
||||
virtual VOID OnDispatchThreadStart(THR_ID tid) = 0;
|
||||
virtual VOID OnDispatchThreadEnd(THR_ID tid) = 0;
|
||||
virtual BOOL OnBeforeProcessIo(PVOID pv, UINT events) = 0;
|
||||
virtual VOID OnAfterProcessIo(PVOID pv, UINT events, BOOL rs) = 0;
|
||||
virtual BOOL OnReadyRead(PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnReadyWrite(PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnHungUp(PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnError(PVOID pv, UINT events) = 0;
|
||||
virtual BOOL OnReadyPrivilege(PVOID pv, UINT events) = 0;
|
||||
|
||||
virtual VOID OnDispatchThreadStart(THR_ID tid) = 0;
|
||||
virtual VOID OnDispatchThreadEnd(THR_ID tid) = 0;
|
||||
|
||||
public:
|
||||
virtual ~IIOHandler() = default;
|
||||
@@ -149,17 +106,17 @@ public:
|
||||
class CIOHandler : public IIOHandler
|
||||
{
|
||||
public:
|
||||
virtual VOID OnCommand(const TDispContext* pContext, TDispCommand* pCmd) override {}
|
||||
virtual VOID OnCommand(TDispCommand* pCmd) override {}
|
||||
virtual VOID OnTimer(ULLONG llExpirations) override {}
|
||||
|
||||
virtual BOOL OnBeforeProcessIo(const TDispContext* pContext, PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual VOID OnAfterProcessIo(const TDispContext* pContext, PVOID pv, UINT events, BOOL rs) override {}
|
||||
virtual BOOL OnReadyWrite(const TDispContext* pContext, PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnHungUp(const TDispContext* pContext, PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnError(const TDispContext* pContext, PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnReadyPrivilege(const TDispContext* pContext, PVOID pv, UINT events) override {return TRUE;}
|
||||
|
||||
virtual VOID OnDispatchThreadStart(THR_ID tid) override {}
|
||||
virtual VOID OnDispatchThreadEnd(THR_ID tid) override {}
|
||||
virtual BOOL OnBeforeProcessIo(PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual VOID OnAfterProcessIo(PVOID pv, UINT events, BOOL rs) override {}
|
||||
virtual BOOL OnReadyWrite(PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnHungUp(PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnError(PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual BOOL OnReadyPrivilege(PVOID pv, UINT events) override {return TRUE;}
|
||||
virtual VOID OnDispatchThreadStart(THR_ID tid) override {}
|
||||
virtual VOID OnDispatchThreadEnd(THR_ID tid) override {}
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------------------------- //
|
||||
@@ -169,91 +126,55 @@ class CIODispatcher
|
||||
public:
|
||||
static const int DEF_WORKER_MAX_EVENTS = 64;
|
||||
|
||||
using CCommandQueue = TDispContext::CCommandQueue;
|
||||
using CWorkerThread = TDispContext::CWorkerThread;
|
||||
using CCommandQueue = CCASQueue<TDispCommand>;
|
||||
using CWorkerThread = CThread<CIODispatcher, VOID, int>;
|
||||
|
||||
public:
|
||||
BOOL Start(IIOHandler* pHandler, int iWorkerMaxEvents = DEF_WORKER_MAX_EVENTS, int iWorkers = 0);
|
||||
BOOL Start(IIOHandler* pHandler, int iWorkerMaxEvents = DEF_WORKER_MAX_EVENTS, int iWorkers = 0, LLONG llTimerInterval = 0);
|
||||
BOOL Stop(BOOL bCheck = TRUE);
|
||||
|
||||
BOOL SendCommandByIndex(int idx, TDispCommand* pCmd);
|
||||
BOOL SendCommandByIndex(int idx, USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0);
|
||||
BOOL SendCommandByFD(FD fd, TDispCommand* pCmd);
|
||||
BOOL SendCommandByFD(FD fd, USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0);
|
||||
BOOL SendCommand(TDispContext& ctx, TDispCommand* pCmd);
|
||||
BOOL SendCommand(TDispCommand* pCmd);
|
||||
BOOL SendCommand(USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0);
|
||||
|
||||
template<class _List, typename = enable_if_t<is_same<remove_reference_t<typename _List::reference>, TDispCommand*>::value>>
|
||||
BOOL SendCommandsByIndex(int idx, const _List& cmds)
|
||||
{
|
||||
TDispContext& ctx = GetContextByIndex(idx);
|
||||
return SendCommands(ctx, cmds);
|
||||
}
|
||||
|
||||
template<class _List, typename = enable_if_t<is_same<remove_reference_t<typename _List::reference>, TDispCommand*>::value>>
|
||||
BOOL SendCommandsByFD(FD fd, const _List& cmds)
|
||||
{
|
||||
TDispContext& ctx = GetContextByFD(fd);
|
||||
return SendCommands(ctx, cmds);
|
||||
}
|
||||
|
||||
template<class _List, typename = enable_if_t<is_same<remove_reference_t<typename _List::reference>, TDispCommand*>::value>>
|
||||
BOOL SendCommands(TDispContext& ctx, const _List& cmds)
|
||||
BOOL SendCommands(const _List& cmds)
|
||||
{
|
||||
size_t size = cmds.size();
|
||||
if(size == 0) return FALSE;
|
||||
|
||||
for(auto it = cmds.begin(), end = cmds.end(); it != end; ++it)
|
||||
ctx.m_queue.PushBack(*it);
|
||||
m_queue.PushBack(*it);
|
||||
|
||||
return VERIFY_IS_NO_ERROR(eventfd_write(ctx.m_evCmd, size));
|
||||
return VERIFY_IS_NO_ERROR(eventfd_write(m_evCmd, size));
|
||||
}
|
||||
|
||||
BOOL AddFD(int idx, FD fd, UINT mask, PVOID pv) {return CtlFD(idx, fd, EPOLL_CTL_ADD, mask, pv);}
|
||||
BOOL ModFD(int idx, FD fd, UINT mask, PVOID pv) {return CtlFD(idx, fd, EPOLL_CTL_MOD, mask, pv);}
|
||||
BOOL DelFD(int idx, FD fd) {return CtlFD(idx, fd, EPOLL_CTL_DEL, 0, nullptr);}
|
||||
BOOL CtlFD(int idx, FD fd, int op, UINT mask, PVOID pv);
|
||||
BOOL AddFD(FD fd, UINT mask, PVOID pv) {return CtlFD(fd, EPOLL_CTL_ADD, mask, pv);}
|
||||
BOOL ModFD(FD fd, UINT mask, PVOID pv) {return CtlFD(fd, EPOLL_CTL_MOD, mask, pv);}
|
||||
BOOL DelFD(FD fd) {return CtlFD(fd, EPOLL_CTL_DEL, 0, nullptr);}
|
||||
BOOL CtlFD(FD fd, int op, UINT mask, PVOID pv);
|
||||
|
||||
BOOL ProcessIo(PVOID pv, UINT events);
|
||||
|
||||
BOOL AddFD(FD fd, UINT mask, PVOID pv) {return CtlFD(-1, fd, EPOLL_CTL_ADD, mask, pv);}
|
||||
BOOL ModFD(FD fd, UINT mask, PVOID pv) {return CtlFD(-1, fd, EPOLL_CTL_MOD, mask, pv);}
|
||||
BOOL DelFD(FD fd) {return CtlFD(-1, fd, EPOLL_CTL_DEL, 0, nullptr);}
|
||||
BOOL CtlFD(FD fd, int op, UINT mask, PVOID pv) {return CtlFD(-1, fd, op, mask, pv);}
|
||||
|
||||
BOOL ProcessIo(const TDispContext* pContext, PVOID pv, UINT events);
|
||||
|
||||
FD AddTimer (int idx, LLONG llInterval, PVOID pv);
|
||||
BOOL DelTimer (int idx, FD fdTimer);
|
||||
|
||||
FD AddTimer (LLONG llInterval, PVOID pv) {return AddTimer(-1, llInterval, pv);}
|
||||
BOOL DelTimer (FD fdTimer) {return DelTimer(-1, fdTimer);}
|
||||
FD AddTimer (LLONG llInterval, PVOID pv);
|
||||
BOOL DelTimer (FD fdTimer);
|
||||
|
||||
private:
|
||||
int WorkerProc(TDispContext* pContext);
|
||||
BOOL ProcessExit(const TDispContext* pContext, UINT events);
|
||||
BOOL ProcessCommand(TDispContext* pContext, UINT events);
|
||||
BOOL DoProcessIo(const TDispContext* pContext, PVOID pv, UINT events);
|
||||
int WorkerProc(PVOID pv = nullptr);
|
||||
BOOL ProcessExit(UINT events);
|
||||
BOOL ProcessTimer(UINT events);
|
||||
BOOL ProcessCommand(UINT events);
|
||||
BOOL DoProcessIo(PVOID pv, UINT events);
|
||||
|
||||
VOID Reset();
|
||||
VOID MakePrefix();
|
||||
|
||||
TDispContext& GetContextByIndex(int idx) {return GetContext(idx, -1);}
|
||||
TDispContext& GetContextByFD(FD fd) {return GetContext(-1, fd);}
|
||||
TDispContext& GetContext(int idx, FD fd);
|
||||
|
||||
public:
|
||||
const TDispContext& GetContextRefByIndex(int idx) {return GetContextByIndex(idx);}
|
||||
const TDispContext& GetContextRefByFD(FD fd) {return GetContextByFD(fd);}
|
||||
const TDispContext& GetContextRef(int idx, FD fd) {return GetContext(idx, fd);}
|
||||
|
||||
BOOL HasStarted() {return m_pHandler && m_pContexts;}
|
||||
int GetWorkers() {return m_iWorkers;}
|
||||
const TDispContext* GetContexts() {return m_pContexts.get();}
|
||||
BOOL HasStarted() {return m_pHandler && m_pWorkers;}
|
||||
const CWorkerThread* GetWorkerThreads() {return m_pWorkers.get();}
|
||||
|
||||
CIODispatcher() {MakePrefix(); Reset();}
|
||||
~CIODispatcher() {if(HasStarted()) Stop();}
|
||||
|
||||
DECLARE_NO_COPY_CLASS(CIODispatcher)
|
||||
|
||||
private:
|
||||
static LPCTSTR WORKER_THREAD_PREFIX;
|
||||
static volatile UINT sm_uiNum;
|
||||
@@ -262,11 +183,14 @@ private:
|
||||
CString m_strPrefix;
|
||||
|
||||
private:
|
||||
int m_iWorkers;
|
||||
int m_iMaxEvents;
|
||||
IIOHandler* m_pHandler;
|
||||
FD m_epoll;
|
||||
FD m_evCmd;
|
||||
FD m_evExit;
|
||||
FD m_evTimer;
|
||||
int m_iWorkers;
|
||||
int m_iMaxEvents;
|
||||
|
||||
FD m_evExit;
|
||||
|
||||
IIOHandler* m_pHandler;
|
||||
unique_ptr<TDispContext[]> m_pContexts;
|
||||
CCommandQueue m_queue;
|
||||
unique_ptr<CWorkerThread[]> m_pWorkers;
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "CriSec.h"
|
||||
|
||||
#include <shared_mutex>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "STLHelper.h"
|
||||
#include "FuncHelper.h"
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Singleton.h"
|
||||
#include "StringT.h"
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
@@ -1018,51 +1017,3 @@ typedef str_sort_func<TCHAR*, false, true> tchar_ptr_desc_case_sort_func;
|
||||
typedef str_sort_func<CString, false, true> string_desc_case_sort_func;
|
||||
typedef str_sort_func<TCHAR*, false, false> tchar_ptr_desc_ucase_sort_func;
|
||||
typedef str_sort_func<CString, false, false> string_desc_ucase_sort_func;
|
||||
|
||||
template<typename _IntType = int, typename = enable_if_t<is_integral<_IntType>::value>>
|
||||
class CRandomIntegralT
|
||||
{
|
||||
public:
|
||||
_IntType Generate() {return dist(gen);}
|
||||
_IntType operator()() {return Generate();}
|
||||
|
||||
public:
|
||||
CRandomIntegralT(_IntType from, _IntType to) : gen(rd()), dist(from, to)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
random_device rd;
|
||||
mt19937 gen;
|
||||
uniform_int_distribution<_IntType> dist;
|
||||
};
|
||||
|
||||
template<typename _RealType = double, typename = enable_if_t<is_floating_point<_RealType>::value>>
|
||||
class CRandomRealTypeT
|
||||
{
|
||||
public:
|
||||
_RealType Generate() {return dist(gen);}
|
||||
_RealType operator()() {return Generate();}
|
||||
|
||||
public:
|
||||
CRandomRealTypeT(_RealType from, _RealType to) : gen(rd()), dist(from, to)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
random_device rd;
|
||||
mt19937 gen;
|
||||
uniform_real_distribution<_RealType> dist;
|
||||
};
|
||||
|
||||
typedef CRandomIntegralT<int> CRandomInt;
|
||||
typedef CRandomIntegralT<long> CRandomLong;
|
||||
typedef CRandomIntegralT<unsigned int> CRandomUint;
|
||||
typedef CRandomIntegralT<unsigned long> CRandomUlong;
|
||||
typedef CRandomIntegralT<int64_t> CRandomInt64;
|
||||
typedef CRandomIntegralT<uint64_t> CRandomUint64;
|
||||
|
||||
typedef CRandomRealTypeT<float> CRandomFloat;
|
||||
typedef CRandomRealTypeT<double> CRandomDouble;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "CriSec.h"
|
||||
|
||||
#include <condition_variable>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "Thread.h"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
|
||||
#define SINGLETON_THIS(ClassName) ClassName::GetThis()
|
||||
#define SINGLETON_INSTANCE(ClassName) ClassName::GetInstance()
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sched.h>
|
||||
@@ -49,9 +49,6 @@ using namespace std;
|
||||
/* 默认内存块缓存池回收阀值 */
|
||||
#define DEFAULT_BUFFER_CACHE_POOL_HOLD 1024
|
||||
|
||||
/* 使用外部垃圾回收 */
|
||||
#define USE_EXTERNAL_GC 1
|
||||
|
||||
|
||||
#define SysGetSystemConfig sysconf
|
||||
#define SysGetSystemInfo sysinfo
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hpsocket/GlobalDef.h"
|
||||
#include "hpsocket/GlobalErrno.h"
|
||||
#include "../hpsocket/GlobalDef.h"
|
||||
#include "../hpsocket/GlobalErrno.h"
|
||||
#include "RWLock.h"
|
||||
#include "STLHelper.h"
|
||||
|
||||
|
||||
2374
common/crypto/Crypto.cpp
Normal file
2374
common/crypto/Crypto.cpp
Normal file
File diff suppressed because it is too large
Load Diff
258
common/crypto/Crypto.h
Normal file
258
common/crypto/Crypto.h
Normal file
@@ -0,0 +1,258 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../hpsocket/GlobalDef.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
// -------------------------------------------------- BASE64 -------------------------------------------------- //
|
||||
|
||||
// Returns the size of the output. If called with out = NULL, will just return
|
||||
// the size of what the output would have been (without a terminating NULL).
|
||||
size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag);
|
||||
|
||||
// Returns the size of the output. If called with out = NULL, will just return
|
||||
// the size of what the output would have been (without a terminating NULL).
|
||||
size_t base64_decode(const BYTE in[], BYTE out[], size_t len);
|
||||
|
||||
// -------------------------------------------------- URL -------------------------------------------------- //
|
||||
|
||||
int url_encode(const char* src, const int src_size, char* dest, const int dest_size);
|
||||
int url_decode(const char* src, const int src_size, char* dest, const int dest_size);
|
||||
|
||||
// -------------------------------------------------- AES -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
//typedef unsigned char BYTE; // 8-bit byte
|
||||
//typedef unsigned int UINT; // 32-bit word, change to "long" for 16-bit machines
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
///////////////////
|
||||
// AES
|
||||
///////////////////
|
||||
// Key setup must be done before any AES en/de-cryption functions can be used.
|
||||
void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
|
||||
UINT w[], // Output key schedule to be used later
|
||||
int keysize); // Bit length of the key, 128, 192, or 256
|
||||
|
||||
void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
|
||||
BYTE out[], // 16 bytes of ciphertext
|
||||
const UINT key[], // From the key setup
|
||||
int keysize); // Bit length of the key, 128, 192, or 256
|
||||
|
||||
void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
|
||||
BYTE out[], // 16 bytes of plaintext
|
||||
const UINT key[], // From the key setup
|
||||
int keysize); // Bit length of the key, 128, 192, or 256
|
||||
|
||||
///////////////////
|
||||
// AES - CBC
|
||||
///////////////////
|
||||
int aes_encrypt_cbc(const BYTE in[], // Plaintext
|
||||
size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
|
||||
BYTE out[], // Ciphertext, same length as plaintext
|
||||
const UINT key[], // From the key setup
|
||||
int keysize, // Bit length of the key, 128, 192, or 256
|
||||
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
||||
|
||||
// Only output the CBC-MAC of the input.
|
||||
int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
|
||||
size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
|
||||
BYTE out[], // Output MAC
|
||||
const UINT key[], // From the key setup
|
||||
int keysize, // Bit length of the key, 128, 192, or 256
|
||||
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
||||
|
||||
///////////////////
|
||||
// AES - CTR
|
||||
///////////////////
|
||||
void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE
|
||||
int counter_size); // Bytes of the IV used for counting (low end)
|
||||
|
||||
void aes_encrypt_ctr(const BYTE in[], // Plaintext
|
||||
size_t in_len, // Any byte length
|
||||
BYTE out[], // Ciphertext, same length as plaintext
|
||||
const UINT key[], // From the key setup
|
||||
int keysize, // Bit length of the key, 128, 192, or 256
|
||||
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
||||
|
||||
void aes_decrypt_ctr(const BYTE in[], // Ciphertext
|
||||
size_t in_len, // Any byte length
|
||||
BYTE out[], // Plaintext, same length as ciphertext
|
||||
const UINT key[], // From the key setup
|
||||
int keysize, // Bit length of the key, 128, 192, or 256
|
||||
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
||||
|
||||
///////////////////
|
||||
// AES - CCM
|
||||
///////////////////
|
||||
// Returns True if the input parameters do not violate any constraint.
|
||||
int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
|
||||
UINT plaintext_len, // IN - Plaintext length.
|
||||
const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
|
||||
unsigned short associated_data_len, // IN - Associated Data length in bytes.
|
||||
const BYTE nonce[], // IN - The Nonce to be used for encryption.
|
||||
unsigned short nonce_len, // IN - Nonce length in bytes.
|
||||
BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
|
||||
UINT *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
|
||||
UINT mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
|
||||
const BYTE key[], // IN - The AES key for encryption.
|
||||
int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
|
||||
|
||||
// Returns True if the input parameters do not violate any constraint.
|
||||
// Use mac_auth to ensure decryption/validation was preformed correctly.
|
||||
// If authentication does not succeed, the plaintext is zeroed out. To overwride
|
||||
// this, call with mac_auth = NULL. The proper proceedure is to decrypt with
|
||||
// authentication enabled (mac_auth != NULL) and make a second call to that
|
||||
// ignores authentication explicitly if the first call failes.
|
||||
int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
|
||||
UINT ciphertext_len, // IN - Ciphertext length in bytes.
|
||||
const BYTE assoc[], // IN - The Associated Data, required for authentication.
|
||||
unsigned short assoc_len, // IN - Associated Data length in bytes.
|
||||
const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
|
||||
unsigned short nonce_len, // IN - Nonce length in bytes.
|
||||
BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
|
||||
UINT *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
|
||||
UINT mac_len, // IN - The length of the MAC that was calculated.
|
||||
int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
|
||||
const BYTE key[], // IN - The AES key for decryption.
|
||||
int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
|
||||
|
||||
// -------------------------------------------------- DES -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define DES_BLOCK_SIZE 8 // DES operates on 8 bytes at a time
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef enum {
|
||||
DES_ENCRYPT,
|
||||
DES_DECRYPT
|
||||
} DES_MODE;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode);
|
||||
void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6]);
|
||||
|
||||
void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode);
|
||||
void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6]);
|
||||
|
||||
// -------------------------------------------------- MD2 -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define MD2_BLOCK_SIZE 16
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef struct {
|
||||
BYTE data[16];
|
||||
BYTE state[48];
|
||||
BYTE checksum[16];
|
||||
int len;
|
||||
} _MD2_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void md2_init(_MD2_CTX *ctx);
|
||||
void md2_update(_MD2_CTX *ctx, const BYTE data[], size_t len);
|
||||
void md2_final(_MD2_CTX *ctx, BYTE hash[]); // size of hash must be MD2_BLOCK_SIZE
|
||||
|
||||
// -------------------------------------------------- MD5 -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef struct {
|
||||
BYTE data[64];
|
||||
UINT datalen;
|
||||
unsigned long long bitlen;
|
||||
UINT state[4];
|
||||
} _MD5_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void md5_init(_MD5_CTX *ctx);
|
||||
void md5_update(_MD5_CTX *ctx, const BYTE data[], size_t len);
|
||||
void md5_final(_MD5_CTX *ctx, BYTE hash[]);
|
||||
|
||||
// -------------------------------------------------- SHA1 -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef struct {
|
||||
BYTE data[64];
|
||||
UINT datalen;
|
||||
unsigned long long bitlen;
|
||||
UINT state[5];
|
||||
UINT k[4];
|
||||
} _SHA1_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
|
||||
void sha1_init(_SHA1_CTX *ctx);
|
||||
void sha1_update(_SHA1_CTX *ctx, const BYTE data[], size_t len);
|
||||
void sha1_final(_SHA1_CTX *ctx, BYTE hash[]);
|
||||
|
||||
// -------------------------------------------------- SHA256 -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef struct {
|
||||
BYTE data[64];
|
||||
UINT datalen;
|
||||
unsigned long long bitlen;
|
||||
UINT state[8];
|
||||
} _SHA256_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void sha256_init(_SHA256_CTX *ctx);
|
||||
void sha256_update(_SHA256_CTX *ctx, const BYTE data[], size_t len);
|
||||
void sha256_final(_SHA256_CTX *ctx, BYTE hash[]);
|
||||
|
||||
// -------------------------------------------------- ARCFOUR -------------------------------------------------- //
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
// Input: state - the state used to generate the keystream
|
||||
// key - Key to use to initialize the state
|
||||
// len - length of key in bytes (valid lenth is 1 to 256)
|
||||
void arcfour_key_setup(BYTE state[], const BYTE key[], int len);
|
||||
|
||||
// Pseudo-Random Generator Algorithm
|
||||
// Input: state - the state used to generate the keystream
|
||||
// out - Must be allocated to be of at least "len" length
|
||||
// len - number of bytes to generate
|
||||
void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len);
|
||||
|
||||
// -------------------------------------------------- BLOWFISH -------------------------------------------------- //
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define BLOWFISH_BLOCK_SIZE 8 // Blowfish operates on 8 bytes at a time
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
|
||||
typedef struct {
|
||||
WORD p[18];
|
||||
WORD s[4][256];
|
||||
} _BLOWFISH_KEY;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void blowfish_key_setup(const BYTE user_key[], _BLOWFISH_KEY *keystruct, size_t len);
|
||||
void blowfish_encrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct);
|
||||
void blowfish_decrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct);
|
||||
|
||||
// -------------------------------------------------- ROT-13 -------------------------------------------------- //
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
// Performs IN PLACE rotation of the input. Assumes input is NULL terminated.
|
||||
// Preserves each charcter's case. Ignores non alphabetic characters.
|
||||
void rot13(char str[]);
|
||||
@@ -3,8 +3,8 @@
|
||||
#define INCLUDE_LLHTTP_H_
|
||||
|
||||
#define LLHTTP_VERSION_MAJOR 9
|
||||
#define LLHTTP_VERSION_MINOR 2
|
||||
#define LLHTTP_VERSION_PATCH 1
|
||||
#define LLHTTP_VERSION_MINOR 1
|
||||
#define LLHTTP_VERSION_PATCH 3
|
||||
|
||||
#ifndef INCLUDE_LLHTTP_ITSELF_H_
|
||||
#define INCLUDE_LLHTTP_ITSELF_H_
|
||||
@@ -181,8 +181,7 @@ enum llhttp_method {
|
||||
HTTP_SET_PARAMETER = 42,
|
||||
HTTP_REDIRECT = 43,
|
||||
HTTP_RECORD = 44,
|
||||
HTTP_FLUSH = 45,
|
||||
HTTP_QUERY = 46
|
||||
HTTP_FLUSH = 45
|
||||
};
|
||||
typedef enum llhttp_method llhttp_method_t;
|
||||
|
||||
@@ -363,7 +362,6 @@ typedef enum llhttp_status llhttp_status_t;
|
||||
XX(31, LINK, LINK) \
|
||||
XX(32, UNLINK, UNLINK) \
|
||||
XX(33, SOURCE, SOURCE) \
|
||||
XX(46, QUERY, QUERY) \
|
||||
|
||||
|
||||
#define RTSP_METHOD_MAP(XX) \
|
||||
@@ -430,7 +428,6 @@ typedef enum llhttp_status llhttp_status_t;
|
||||
XX(43, REDIRECT, REDIRECT) \
|
||||
XX(44, RECORD, RECORD) \
|
||||
XX(45, FLUSH, FLUSH) \
|
||||
XX(46, QUERY, QUERY) \
|
||||
|
||||
|
||||
#define HTTP_STATUS_MAP(XX) \
|
||||
@@ -550,8 +547,6 @@ extern "C" {
|
||||
|
||||
#if defined(__wasm__)
|
||||
#define LLHTTP_EXPORT __attribute__((visibility("default")))
|
||||
//#elif defined(_WIN32)
|
||||
//#define LLHTTP_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define LLHTTP_EXPORT
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user