mirror of
https://github.com/caozhiyi/CppNet.git
synced 2026-01-26 16:30:05 +08:00
Merge remote-tracking branch 'origin/feature_iocp'
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{E98183C7-10C3-4B1A-B8FB-D60BB6217962}</ProjectGuid>
|
||||
<RootNamespace>CppNet</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
<ProjectName>Cppnet</ProjectName>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
@@ -41,13 +41,13 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
@@ -94,7 +94,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_X86_</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)base;$(SolutionDir)include;$(SolutionDir)net;$(SolutionDir)net\win;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalOptions>/D "__win__" %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalOptions>/D "__win__" /D "__use_iocp__" %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
@@ -103,7 +103,7 @@
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_AMD64_</PreprocessorDefinitions>
|
||||
<AdditionalOptions>/D "__win__" %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalOptions>/D "__win__" /D "__use_iocp__" %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<HeapReserveSize>260046848</HeapReserveSize>
|
||||
@@ -118,7 +118,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)base;$(SolutionDir)include;$(SolutionDir)net;$(SolutionDir)net\win;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_X86_</PreprocessorDefinitions>
|
||||
<AdditionalOptions>/D "__win__" %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalOptions>/D "__win__" /D "__use_iocp__" %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
@@ -134,7 +134,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)base;$(SolutionDir)include;$(SolutionDir)net;$(SolutionDir)net\win;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_AMD64_</PreprocessorDefinitions>
|
||||
<AdditionalOptions>/D "__win__" %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalOptions>/D "__win__" /D "__use_iocp__" %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
@@ -187,7 +187,6 @@
|
||||
<ClInclude Include="cppnet\event\win\accept_event.h" />
|
||||
<ClInclude Include="cppnet\event\win\expend_func.h" />
|
||||
<ClInclude Include="cppnet\event\win\iocp_action.h" />
|
||||
<ClInclude Include="cppnet\event\win\rw_event.h" />
|
||||
<ClInclude Include="cppnet\socket\connect_socket.h" />
|
||||
<ClInclude Include="cppnet\socket\rw_socket.h" />
|
||||
<ClInclude Include="cppnet\socket\socket_interface.h" />
|
||||
|
||||
@@ -213,9 +213,6 @@
|
||||
<ClInclude Include="cppnet\event\win\accept_event.h">
|
||||
<Filter>cppnet\event\win</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="cppnet\event\win\rw_event.h">
|
||||
<Filter>cppnet\event\win</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="common\log\log_stream.h">
|
||||
<Filter>common\log</Filter>
|
||||
</ClInclude>
|
||||
|
||||
@@ -34,7 +34,9 @@ void* PoolAlloter::Malloc(uint32_t size) {
|
||||
void* ret = _alloter->Malloc(size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
MemNode** my_free = &(_free_list[FreeListIndex(size)]);
|
||||
MemNode* result = *my_free;
|
||||
if (result == nullptr) {
|
||||
@@ -68,7 +70,10 @@ void PoolAlloter::Free(void* &data, uint32_t len) {
|
||||
data = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
MemNode* node = (MemNode*)data;
|
||||
MemNode** my_free = &(_free_list[FreeListIndex(len)]);
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#ifndef COMMON_ALLOTER_POOL_ALLOTER
|
||||
#define COMMON_ALLOTER_POOL_ALLOTER
|
||||
|
||||
#ifdef __use_iocp__
|
||||
#include <mutex>
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "alloter_interface.h"
|
||||
@@ -40,6 +43,9 @@ private:
|
||||
char _data[1];
|
||||
};
|
||||
|
||||
#ifdef __use_iocp__
|
||||
std::mutex _mutex;
|
||||
#endif
|
||||
char* _pool_start;
|
||||
char* _pool_end;
|
||||
std::vector<MemNode*> _free_list;
|
||||
|
||||
@@ -17,13 +17,20 @@ BlockMemoryPool::BlockMemoryPool(uint32_t large_sz, uint32_t add_num) :
|
||||
}
|
||||
|
||||
BlockMemoryPool::~BlockMemoryPool() {
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
// free all memory
|
||||
for (auto iter = _free_mem_vec.begin(); iter != _free_mem_vec.end(); ++iter) {
|
||||
free(*iter);
|
||||
}
|
||||
_free_mem_vec.clear();
|
||||
}
|
||||
|
||||
void* BlockMemoryPool::PoolLargeMalloc() {
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
if (_free_mem_vec.empty()) {
|
||||
Expansion();
|
||||
}
|
||||
@@ -34,11 +41,17 @@ void* BlockMemoryPool::PoolLargeMalloc() {
|
||||
}
|
||||
|
||||
void BlockMemoryPool::PoolLargeFree(void* &m) {
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
_free_mem_vec.push_back(m);
|
||||
// TODO release some block.
|
||||
}
|
||||
|
||||
uint32_t BlockMemoryPool::GetSize() {
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
return (uint32_t)_free_mem_vec.size();
|
||||
}
|
||||
|
||||
@@ -47,6 +60,9 @@ uint32_t BlockMemoryPool::GetBlockLength() {
|
||||
}
|
||||
|
||||
void BlockMemoryPool::ReleaseHalf() {
|
||||
#ifdef __use_iocp__
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
#endif
|
||||
size_t size = _free_mem_vec.size();
|
||||
size_t hale = size / 2;
|
||||
for (auto iter = _free_mem_vec.begin(); iter != _free_mem_vec.end();) {
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#ifndef COMMON_ALLOTER_POOL_BLOCK
|
||||
#define COMMON_ALLOTER_POOL_BLOCK
|
||||
|
||||
#ifdef __use_iocp__
|
||||
#include <mutex>
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
@@ -18,23 +21,26 @@ public:
|
||||
// bulk memory size.
|
||||
// everytime add nodes num
|
||||
BlockMemoryPool(uint32_t large_sz, uint32_t add_num);
|
||||
~BlockMemoryPool();
|
||||
virtual ~BlockMemoryPool();
|
||||
|
||||
// for bulk memory.
|
||||
// return one bulk memory node
|
||||
void* PoolLargeMalloc();
|
||||
void PoolLargeFree(void* &m);
|
||||
virtual void* PoolLargeMalloc();
|
||||
virtual void PoolLargeFree(void* &m);
|
||||
|
||||
// return bulk memory list size
|
||||
uint32_t GetSize();
|
||||
virtual uint32_t GetSize();
|
||||
// return length of bulk memory
|
||||
uint32_t GetBlockLength();
|
||||
virtual uint32_t GetBlockLength();
|
||||
|
||||
// release half memory
|
||||
void ReleaseHalf();
|
||||
void Expansion(uint32_t num = 0);
|
||||
virtual void ReleaseHalf();
|
||||
virtual void Expansion(uint32_t num = 0);
|
||||
|
||||
private:
|
||||
#ifdef __use_iocp__
|
||||
std::mutex _mutex;
|
||||
#endif
|
||||
uint32_t _number_large_add_nodes; //every time add nodes num
|
||||
uint32_t _large_size; //bulk memory size
|
||||
std::vector<void*> _free_mem_vec; //free bulk memory list
|
||||
|
||||
@@ -131,9 +131,9 @@ void CppNetBase::OnAccept(std::shared_ptr<RWSocket> sock) {
|
||||
}
|
||||
}
|
||||
|
||||
void CppNetBase::OnRead(std::shared_ptr<RWSocket> sock, uint32_t len) {
|
||||
void CppNetBase::OnRead(std::shared_ptr<RWSocket> sock, std::shared_ptr<Buffer> buffer, uint32_t len) {
|
||||
if (_read_cb) {
|
||||
_read_cb(sock, sock->GetReadBuffer(), len);
|
||||
_read_cb(sock, buffer, len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class Buffer;
|
||||
class RWSocket;
|
||||
class Dispatcher;
|
||||
class RangeRandom;
|
||||
@@ -47,7 +48,7 @@ public:
|
||||
// call back
|
||||
void OnTimer(std::shared_ptr<RWSocket> sock);
|
||||
void OnAccept(std::shared_ptr<RWSocket> sock);
|
||||
void OnRead(std::shared_ptr<RWSocket> sock, uint32_t len);
|
||||
void OnRead(std::shared_ptr<RWSocket> sock, std::shared_ptr<Buffer> buffer, uint32_t len);
|
||||
void OnWrite(std::shared_ptr<RWSocket> sock, uint32_t len);
|
||||
void OnConnect(std::shared_ptr<RWSocket> sock, uint16_t err);
|
||||
void OnDisConnect(std::shared_ptr<RWSocket> sock, uint16_t err);
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
#include "common/timer/timer.h"
|
||||
#include "common/alloter/pool_alloter.h"
|
||||
|
||||
#ifdef __win__
|
||||
#include "cppnet/socket/win/win_rw_socket.h"
|
||||
#else
|
||||
#endif
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
thread_local std::unordered_map<uint64_t, std::shared_ptr<TimerEvent>> Dispatcher::__all_timer_event_map;
|
||||
@@ -141,18 +146,14 @@ void Dispatcher::Listen(uint64_t sock, const std::string& ip, uint16_t port) {
|
||||
|
||||
void Dispatcher::Connect(const std::string& ip, uint16_t port) {
|
||||
if (std::this_thread::get_id() == _local_thread_id) {
|
||||
auto alloter = std::make_shared<AlloterWrap>(MakePoolAlloterPtr());
|
||||
auto sock = MakeRWSocket(alloter);
|
||||
|
||||
auto sock = MakeRWSocket();
|
||||
sock->SetEventActions(_event_actions);
|
||||
sock->SetCppNetBase(_cppnet_base.lock());
|
||||
sock->Connect(ip, port);
|
||||
|
||||
} else {
|
||||
auto task = [ip, port, this]() {
|
||||
auto alloter = std::make_shared<AlloterWrap>(MakePoolAlloterPtr());
|
||||
auto sock = MakeRWSocket(alloter);
|
||||
|
||||
auto sock = MakeRWSocket();
|
||||
sock->SetEventActions(_event_actions);
|
||||
sock->SetCppNetBase(_cppnet_base.lock());
|
||||
sock->Connect(ip, port);
|
||||
|
||||
@@ -30,14 +30,13 @@ public:
|
||||
virtual bool Dealloc() = 0;
|
||||
|
||||
// net io event
|
||||
virtual bool AddSendEvent(std::shared_ptr<Event>& event) = 0;
|
||||
virtual bool AddRecvEvent(std::shared_ptr<Event>& event) = 0;
|
||||
virtual bool AddAcceptEvent(std::shared_ptr<Event>& event) = 0;
|
||||
virtual bool AddSendEvent(Event* event) = 0;
|
||||
virtual bool AddRecvEvent(Event* event) = 0;
|
||||
virtual bool AddAcceptEvent(Event* event) = 0;
|
||||
virtual bool AddConnection(Event* event, Address& addr) = 0;
|
||||
virtual bool AddDisconnection(Event* event) = 0;
|
||||
|
||||
virtual bool AddConnection(std::shared_ptr<Event>& event, Address& addr) = 0;
|
||||
virtual bool AddDisconnection(std::shared_ptr<Event>& event) = 0;
|
||||
|
||||
virtual bool DelEvent(std::shared_ptr<Event>& event) = 0;
|
||||
virtual bool DelEvent(Event* event) = 0;
|
||||
// io thread process
|
||||
virtual void ProcessEvent(int32_t wait_ms) = 0;
|
||||
// weak up net io thread
|
||||
|
||||
@@ -26,6 +26,7 @@ enum EventType {
|
||||
const char* TypeString(EventType type);
|
||||
|
||||
class Socket;
|
||||
class BufferQueue;
|
||||
class Event {
|
||||
public:
|
||||
Event(): _data(nullptr), _event_type(0) {}
|
||||
@@ -38,14 +39,24 @@ public:
|
||||
void RemoveType(EventType type) { _event_type &= ~type; }
|
||||
uint16_t GetType() { return _event_type; }
|
||||
void ClearType() { _event_type = 0; }
|
||||
void ForceSetType(EventType type) { _event_type = type; }
|
||||
|
||||
void SetSocket(std::shared_ptr<Socket> socket) { _socket = socket; }
|
||||
std::shared_ptr<Socket> GetSocket() { return _socket.lock(); }
|
||||
|
||||
#ifdef __win__
|
||||
void SetBuffer(std::shared_ptr<BufferQueue>& buffer) { _buffer = buffer; }
|
||||
std::shared_ptr<BufferQueue> GetBuffer() { return _buffer; }
|
||||
private:
|
||||
std::shared_ptr<BufferQueue> _buffer;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
void* _data;
|
||||
uint16_t _event_type;
|
||||
std::weak_ptr<Socket> _socket;
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ bool EpollEventActions::Dealloc() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EpollEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
bool EpollEventActions::AddSendEvent(Event* event) {
|
||||
if (event->GetType() & ET_WRITE) {
|
||||
return false;
|
||||
}
|
||||
@@ -89,7 +89,7 @@ bool EpollEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
memset(ep_event, 0, sizeof(epoll_event));
|
||||
event->SetData(ep_event);
|
||||
}
|
||||
ep_event->data.ptr = (void*)&event;
|
||||
ep_event->data.ptr = (void*)event;
|
||||
if (AddEvent(ep_event, EPOLLOUT, sock->GetSocket(), event->GetType() & ET_INACTIONS)) {
|
||||
event->AddType(ET_INACTIONS);
|
||||
return true;
|
||||
@@ -99,7 +99,7 @@ bool EpollEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EpollEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
bool EpollEventActions::AddRecvEvent(Event* event) {
|
||||
if (event->GetType() & ET_READ) {
|
||||
return false;
|
||||
}
|
||||
@@ -114,7 +114,7 @@ bool EpollEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
memset(ep_event, 0, sizeof(epoll_event));
|
||||
event->SetData(ep_event);
|
||||
}
|
||||
ep_event->data.ptr = (void*)&event;
|
||||
ep_event->data.ptr = (void*)event;
|
||||
if (AddEvent(ep_event, EPOLLIN, sock->GetSocket(), event->GetType() & ET_INACTIONS)) {
|
||||
event->AddType(ET_INACTIONS);
|
||||
return true;
|
||||
@@ -124,7 +124,7 @@ bool EpollEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EpollEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
bool EpollEventActions::AddAcceptEvent(Event* event) {
|
||||
if (event->GetType() & ET_ACCEPT) {
|
||||
return false;
|
||||
}
|
||||
@@ -139,7 +139,7 @@ bool EpollEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
memset(ep_event, 0, sizeof(epoll_event));
|
||||
event->SetData(ep_event);
|
||||
}
|
||||
ep_event->data.ptr = (void*)&event;
|
||||
ep_event->data.ptr = (void*)event;
|
||||
if (AddEvent(ep_event, EPOLLIN, sock->GetSocket(), event->GetType() & ET_INACTIONS)) {
|
||||
event->AddType(ET_INACTIONS);
|
||||
return true;
|
||||
@@ -149,7 +149,7 @@ bool EpollEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EpollEventActions::AddConnection(std::shared_ptr<Event>& event, Address& addr) {
|
||||
bool EpollEventActions::AddConnection(Event* event, Address& addr) {
|
||||
if (event->GetType() & ET_CONNECT) {
|
||||
return false;
|
||||
}
|
||||
@@ -169,16 +169,16 @@ bool EpollEventActions::AddConnection(std::shared_ptr<Event>& event, Address& ad
|
||||
|
||||
auto rw_sock = std::dynamic_pointer_cast<RWSocket>(sock);
|
||||
if (ret._return_value == 0) {
|
||||
rw_sock->OnConnect(CEC_SUCCESS);
|
||||
rw_sock->OnConnect(event, CEC_SUCCESS);
|
||||
return true;
|
||||
|
||||
} else if (ret._errno == EINPROGRESS) {
|
||||
if (CheckConnect(rw_sock->GetSocket())) {
|
||||
rw_sock->OnConnect(CEC_SUCCESS);
|
||||
rw_sock->OnConnect(event, CEC_SUCCESS);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
rw_sock->OnConnect(CEC_CONNECT_REFUSE);
|
||||
rw_sock->OnConnect(event, CEC_CONNECT_REFUSE);
|
||||
LOG_WARN("connect event failed! %d", ret._errno);
|
||||
return false;
|
||||
}
|
||||
@@ -186,7 +186,7 @@ bool EpollEventActions::AddConnection(std::shared_ptr<Event>& event, Address& ad
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EpollEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
bool EpollEventActions::AddDisconnection(Event* event) {
|
||||
if (event->GetType() & ET_DISCONNECT) {
|
||||
return false;
|
||||
}
|
||||
@@ -202,11 +202,11 @@ bool EpollEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
OsHandle::Close(socket->GetSocket());
|
||||
socket->OnDisConnect(CEC_SUCCESS);
|
||||
socket->OnDisConnect(event, CEC_SUCCESS);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EpollEventActions::DelEvent(std::shared_ptr<Event>& event) {
|
||||
bool EpollEventActions::DelEvent(Event* event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
return false;
|
||||
@@ -244,7 +244,7 @@ void EpollEventActions::Wakeup() {
|
||||
|
||||
void EpollEventActions::OnEvent(std::vector<epoll_event>& event_vec, int16_t num) {
|
||||
std::shared_ptr<Socket> sock;
|
||||
std::shared_ptr<Event> event;
|
||||
Event* event = nullptr;
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
if ((uint32_t)event_vec[i].data.fd == _pipe[0]) {
|
||||
@@ -254,7 +254,7 @@ void EpollEventActions::OnEvent(std::vector<epoll_event>& event_vec, int16_t num
|
||||
continue;
|
||||
}
|
||||
|
||||
event = *(std::shared_ptr<Event>*)event_vec[i].data.ptr;
|
||||
event = (Event*)event_vec[i].data.ptr;
|
||||
sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("epoll weak up but socket already destroy, index : %d", i);
|
||||
@@ -264,20 +264,20 @@ void EpollEventActions::OnEvent(std::vector<epoll_event>& event_vec, int16_t num
|
||||
// accept event
|
||||
if (event->GetType() & ET_ACCEPT) {
|
||||
std::shared_ptr<ConnectSocket> connect_sock = std::dynamic_pointer_cast<ConnectSocket>(sock);
|
||||
connect_sock->OnAccept();
|
||||
connect_sock->OnAccept(event);
|
||||
|
||||
} else {
|
||||
std::shared_ptr<RWSocket> rw_sock = std::dynamic_pointer_cast<RWSocket>(sock);
|
||||
if (event_vec[i].events & EPOLLIN) {
|
||||
// close
|
||||
if (event_vec[i].events & EPOLLRDHUP) {
|
||||
rw_sock->OnDisConnect(CEC_CLOSED);
|
||||
rw_sock->OnDisConnect(event, CEC_CLOSED);
|
||||
}
|
||||
rw_sock->OnRead();
|
||||
rw_sock->OnRead(event);
|
||||
}
|
||||
|
||||
if (event_vec[i].events & EPOLLOUT) {
|
||||
rw_sock->OnWrite();
|
||||
rw_sock->OnWrite(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@
|
||||
namespace cppnet {
|
||||
|
||||
// epoll event interface
|
||||
class EpollEventActions: public EventActions {
|
||||
class EpollEventActions:
|
||||
public EventActions {
|
||||
|
||||
public:
|
||||
EpollEventActions();
|
||||
virtual ~EpollEventActions();
|
||||
@@ -21,14 +23,13 @@ public:
|
||||
virtual bool Init(uint32_t thread_num = 0);
|
||||
virtual bool Dealloc();
|
||||
// net io event
|
||||
virtual bool AddSendEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddRecvEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddAcceptEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddSendEvent(Event* event);
|
||||
virtual bool AddRecvEvent(Event* event);
|
||||
virtual bool AddAcceptEvent(Event* event);
|
||||
virtual bool AddConnection(Event* event, Address& address);
|
||||
virtual bool AddDisconnection(Event* event);
|
||||
|
||||
virtual bool AddConnection(std::shared_ptr<Event>& event, Address& address);
|
||||
virtual bool AddDisconnection(std::shared_ptr<Event>& event);
|
||||
|
||||
virtual bool DelEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool DelEvent(Event* event);
|
||||
// io thread process
|
||||
virtual void ProcessEvent(int32_t wait_ms);
|
||||
// weak up net io thread
|
||||
|
||||
@@ -73,7 +73,7 @@ bool KqueueEventActions::Dealloc() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
bool KqueueEventActions::AddSendEvent(Event* event) {
|
||||
if (event->GetType() & ET_WRITE) {
|
||||
return false;
|
||||
}
|
||||
@@ -82,7 +82,7 @@ bool KqueueEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (sock) {
|
||||
struct kevent ev;
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, (void*)&event);
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, (void*)event);
|
||||
|
||||
_change_list.push_back(ev);
|
||||
return true;
|
||||
@@ -91,7 +91,7 @@ bool KqueueEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
bool KqueueEventActions::AddRecvEvent(Event* event) {
|
||||
if (event->GetType() & ET_READ) {
|
||||
return false;
|
||||
}
|
||||
@@ -100,7 +100,7 @@ bool KqueueEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (sock) {
|
||||
struct kevent ev;
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void*)&event);
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void*)event);
|
||||
_change_list.push_back(ev);
|
||||
return true;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ bool KqueueEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
bool KqueueEventActions::AddAcceptEvent(Event* event) {
|
||||
if (event->GetType() & ET_ACCEPT) {
|
||||
return false;
|
||||
}
|
||||
@@ -117,7 +117,7 @@ bool KqueueEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (sock) {
|
||||
struct kevent ev;
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void*)&event);
|
||||
EV_SET(&ev, sock->GetSocket(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void*)event);
|
||||
|
||||
_change_list.push_back(ev);
|
||||
return true;
|
||||
@@ -126,7 +126,7 @@ bool KqueueEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::AddConnection(std::shared_ptr<Event>& event, Address& address) {
|
||||
bool KqueueEventActions::AddConnection(Event* event, Address& address) {
|
||||
if (event->GetType() & ET_CONNECT) {
|
||||
return false;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ bool KqueueEventActions::AddConnection(std::shared_ptr<Event>& event, Address& a
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
bool KqueueEventActions::AddDisconnection(Event* event) {
|
||||
if (event->GetType() & ET_DISCONNECT) {
|
||||
return false;
|
||||
}
|
||||
@@ -176,7 +176,7 @@ bool KqueueEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool KqueueEventActions::DelEvent(std::shared_ptr<Event>& event) {
|
||||
bool KqueueEventActions::DelEvent(Event* event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
return false;
|
||||
@@ -222,7 +222,7 @@ void KqueueEventActions::Wakeup() {
|
||||
|
||||
void KqueueEventActions::OnEvent(std::vector<struct kevent>& event_vec, int16_t num) {
|
||||
std::shared_ptr<Socket> sock;
|
||||
std::shared_ptr<Event> event;
|
||||
Event* event;
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
if (event_vec[i].ident == _pipe[0]) {
|
||||
@@ -232,7 +232,7 @@ void KqueueEventActions::OnEvent(std::vector<struct kevent>& event_vec, int16_t
|
||||
continue;
|
||||
}
|
||||
|
||||
event = (*(std::shared_ptr<Event>*)event_vec[i].udata);
|
||||
event = (Event*)event_vec[i].udata;
|
||||
sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("kqueue weak up but socket already destroy, index : %d", i);
|
||||
|
||||
@@ -23,14 +23,13 @@ public:
|
||||
virtual bool Init(uint32_t thread_num = 0);
|
||||
virtual bool Dealloc();
|
||||
// net io event
|
||||
virtual bool AddSendEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddRecvEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddAcceptEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddSendEvent(Event* event);
|
||||
virtual bool AddRecvEvent(Event* event);
|
||||
virtual bool AddAcceptEvent(Event* event);
|
||||
virtual bool AddConnection(Event* event, Address& address);
|
||||
virtual bool AddDisconnection(Event* event);
|
||||
|
||||
virtual bool AddConnection(std::shared_ptr<Event>& event, Address& address);
|
||||
virtual bool AddDisconnection(std::shared_ptr<Event>& event);
|
||||
|
||||
virtual bool DelEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool DelEvent(Event* event);
|
||||
// io thread process
|
||||
virtual void ProcessEvent(int32_t wait_ms);
|
||||
// weak up net io thread
|
||||
|
||||
@@ -11,24 +11,24 @@
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class AcceptEvent:
|
||||
class WinAcceptEvent:
|
||||
public Event{
|
||||
public:
|
||||
AcceptEvent():
|
||||
WinAcceptEvent():
|
||||
_client_sock(0),
|
||||
_buf_offset(0),
|
||||
_index_in_socket(0) {
|
||||
memset(_buf, 0, __iocp_buff_size);
|
||||
}
|
||||
|
||||
AcceptEvent(uint16_t index) :
|
||||
WinAcceptEvent(uint16_t index) :
|
||||
_client_sock(0),
|
||||
_buf_offset(0),
|
||||
_index_in_socket(index) {
|
||||
memset(_buf, 0, __iocp_buff_size);
|
||||
}
|
||||
|
||||
virtual ~AcceptEvent() {}
|
||||
virtual ~WinAcceptEvent() {}
|
||||
|
||||
char* GetBuf() { return _buf; }
|
||||
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Author: caozhiyi (caozhiyi5@gmail.com)
|
||||
#include <WS2tcpip.h>
|
||||
|
||||
#include "expend_func.h"
|
||||
#include "iocp_action.h"
|
||||
|
||||
#include "cppnet/cppnet_config.h"
|
||||
#include "cppnet/event/win/rw_event.h"
|
||||
#include "cppnet/event/win/accept_event.h"
|
||||
#include "cppnet/socket/win/win_rw_socket.h"
|
||||
#include "cppnet/socket/win/win_connect_socket.h"
|
||||
@@ -17,34 +16,12 @@
|
||||
#include "common/network/socket.h"
|
||||
#include "common/buffer/buffer_queue.h"
|
||||
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
std::shared_ptr<EventActions> MakeEventActions() {
|
||||
return std::make_shared<IOCPEventActions>();
|
||||
}
|
||||
|
||||
enum IOCP_NOTIFY_CODE {
|
||||
INC_WEAK_UP = 0xAAAAFFFF,
|
||||
INC_CONNECTION_BREAK = 0x100,
|
||||
INC_CONNECTION_REFUSE = 0x200,
|
||||
INC_CONNECTION_CLOSE = 0x400,
|
||||
};
|
||||
|
||||
struct EventOverlapped {
|
||||
OVERLAPPED _overlapped;
|
||||
uint32_t _event_type;
|
||||
void* _event;
|
||||
|
||||
EventOverlapped():
|
||||
_event_type(0),
|
||||
_event(nullptr){
|
||||
memset(&_overlapped, 0, sizeof(_overlapped));
|
||||
}
|
||||
|
||||
~EventOverlapped() {}
|
||||
};
|
||||
|
||||
IOCPEventActions::IOCPEventActions():
|
||||
_iocp_handler(nullptr) {
|
||||
|
||||
@@ -56,10 +33,10 @@ IOCPEventActions::~IOCPEventActions() {
|
||||
|
||||
bool IOCPEventActions::Init(uint32_t thread_num) {
|
||||
WinSockInit();
|
||||
//tell iocp thread num
|
||||
//tell IOCP thread num
|
||||
_iocp_handler = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, thread_num);
|
||||
if (_iocp_handler == INVALID_HANDLE_VALUE) {
|
||||
LOG_ERROR("IOCP create io completion port failed!");
|
||||
LOG_ERROR("IOCP create IO completion port failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -70,53 +47,44 @@ bool IOCPEventActions::Dealloc() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
bool IOCPEventActions::AddSendEvent(Event* event) {
|
||||
if (event->GetType() & ET_WRITE || event->GetType() & ET_DISCONNECT) {
|
||||
LOG_WARN_S << "repeat send event";
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddSendEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddSendEvent");
|
||||
return false;
|
||||
}
|
||||
auto rw_sock = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (rw_sock->IsShutdown()) {
|
||||
LOG_WARN_S << "socket is shutdown when send";
|
||||
rw_sock->OnDisConnect(event, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(event->GetType() & ET_INACTIONS)) {
|
||||
if (!AddToIOCP(sock->GetSocket())) {
|
||||
LOG_WARN_S << "add to iocp failed when send";
|
||||
return false;
|
||||
}
|
||||
event->AddType(ET_INACTIONS);
|
||||
}
|
||||
auto rw_event = std::dynamic_pointer_cast<RWEvent>(event);
|
||||
EventOverlapped* context = (EventOverlapped*)rw_event->GetExData();
|
||||
|
||||
EventOverlapped* context = (EventOverlapped*)event->GetData();
|
||||
if (!context) {
|
||||
context = rw_sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)&event;
|
||||
rw_event->SetExData(context);
|
||||
context->_event = (void*)event;
|
||||
event->SetData(context);
|
||||
}
|
||||
|
||||
|
||||
context->_event_type = ET_WRITE;
|
||||
|
||||
auto buffer = rw_sock->GetWriteBuffer();
|
||||
auto buffer = event->GetBuffer();
|
||||
std::vector<Iovec> bufs;
|
||||
buffer->GetUseMemoryBlock(bufs, __iocp_buff_size);
|
||||
|
||||
DWORD dwFlags = 0;
|
||||
int32_t ret = WSASend((SOCKET)sock->GetSocket(), (LPWSABUF)&(*bufs.begin()), (DWORD)bufs.size(), nullptr, dwFlags, &context->_overlapped, nullptr);
|
||||
int32_t ret = WSASend((SOCKET)sock->GetSocket(), (LPWSABUF) & (*bufs.begin()), (DWORD)bufs.size(), nullptr, dwFlags, &context->_overlapped, nullptr);
|
||||
|
||||
if ((SOCKET_ERROR == ret) && (WSA_IO_PENDING != WSAGetLastError())) {
|
||||
LOG_WARN("IOCP post send event failed! error code:%d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
//rw_sock->OnDisConnect(CEC_CLOSED);
|
||||
DelEvent(event);
|
||||
rw_sock->SetShutdown();
|
||||
rw_sock->OnDisConnect(event, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -126,7 +94,7 @@ bool IOCPEventActions::AddSendEvent(std::shared_ptr<Event>& event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
bool IOCPEventActions::AddRecvEvent(Event* event) {
|
||||
if (event->GetType() & ET_READ || event->GetType() & ET_DISCONNECT) {
|
||||
LOG_WARN_S << "repeat recv event";
|
||||
return false;
|
||||
@@ -134,44 +102,36 @@ bool IOCPEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddSendEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddSendEvent");
|
||||
return false;
|
||||
}
|
||||
auto rw_sock = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (rw_sock->IsShutdown()) {
|
||||
LOG_WARN_S << "socket is shutdown when recv";
|
||||
rw_sock->OnDisConnect(event, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(event->GetType() & ET_INACTIONS)) {
|
||||
if (!AddToIOCP(sock->GetSocket())) {
|
||||
LOG_WARN_S << "add to iocp failed when recv";
|
||||
return false;
|
||||
}
|
||||
event->AddType(ET_INACTIONS);
|
||||
}
|
||||
|
||||
EventOverlapped* context = (EventOverlapped*)event->GetData();
|
||||
|
||||
|
||||
if (!context) {
|
||||
context = rw_sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)&event;
|
||||
context->_event = (void*)event;
|
||||
event->SetData(context);
|
||||
}
|
||||
context->_event_type = ET_READ;
|
||||
|
||||
auto buffer = rw_sock->GetReadBuffer();
|
||||
auto buffer = event->GetBuffer();
|
||||
std::vector<Iovec> bufs;
|
||||
buffer->GetFreeMemoryBlock(bufs, __iocp_buff_size);
|
||||
|
||||
DWORD dwFlags = 0;
|
||||
int32_t ret = WSARecv((SOCKET)sock->GetSocket(), (LPWSABUF)&(*bufs.begin()), (DWORD)bufs.size(), nullptr, &dwFlags, &context->_overlapped, nullptr);
|
||||
int32_t ret = WSARecv((SOCKET)sock->GetSocket(), (LPWSABUF) & (*bufs.begin()), (DWORD)bufs.size(), nullptr, &dwFlags, &context->_overlapped, nullptr);
|
||||
|
||||
if ((SOCKET_ERROR == ret) && (WSA_IO_PENDING != WSAGetLastError())) {
|
||||
LOG_WARN("IOCP post recv event failed! error code: %d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
//rw_sock->OnDisConnect(CEC_CLOSED);
|
||||
DelEvent(event);
|
||||
rw_sock->SetShutdown();
|
||||
rw_sock->OnDisConnect(event, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
}
|
||||
event->AddType(ET_READ);
|
||||
@@ -179,7 +139,7 @@ bool IOCPEventActions::AddRecvEvent(std::shared_ptr<Event>& event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
bool IOCPEventActions::AddAcceptEvent(Event* event) {
|
||||
if (event->GetType() & ET_ACCEPT) {
|
||||
LOG_WARN_S << "repeat accept event";
|
||||
return false;
|
||||
@@ -187,29 +147,21 @@ bool IOCPEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddAcceptEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddWinAcceptEvent");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto accept_sock = std::dynamic_pointer_cast<WinConnectSocket>(sock);
|
||||
if (!accept_sock->GetInActions()) {
|
||||
if (!AddToIOCP(sock->GetSocket())) {
|
||||
LOG_WARN_S << "add to iocp failed when accept";
|
||||
return false;
|
||||
}
|
||||
accept_sock->SetInActions(true);
|
||||
}
|
||||
|
||||
auto accept_event = std::dynamic_pointer_cast<AcceptEvent>(event);
|
||||
auto accept_event = dynamic_cast<WinAcceptEvent*>(event);
|
||||
|
||||
EventOverlapped* context = (EventOverlapped*)event->GetData();
|
||||
if (!context) {
|
||||
context = new EventOverlapped();
|
||||
context->_event = (void*)&event;
|
||||
context->_event = (void*)event;
|
||||
event->SetData(context);
|
||||
}
|
||||
context->_event_type = ET_ACCEPT;
|
||||
|
||||
|
||||
DWORD dwBytes = 0;
|
||||
uint32_t ret = AcceptEx((SOCKET)sock->GetSocket(), (SOCKET)accept_event->GetClientSocket(), accept_event->GetBuf(), __iocp_buff_size - ((sizeof(SOCKADDR_STORAGE) + 16) * 2),
|
||||
sizeof(SOCKADDR_STORAGE) + 16, sizeof(SOCKADDR_STORAGE) + 16, &dwBytes, &context->_overlapped);
|
||||
@@ -217,18 +169,17 @@ bool IOCPEventActions::AddAcceptEvent(std::shared_ptr<Event>& event) {
|
||||
if (0 == ret) {
|
||||
if (WSA_IO_PENDING != WSAGetLastError()) {
|
||||
LOG_ERROR("IOCP post accept failed! error code:%d", WSAGetLastError());
|
||||
DelEvent(event);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
event->AddType(ET_ACCEPT);
|
||||
LOG_DEBUG("post a new accept event");
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::AddConnection(std::shared_ptr<Event>& event, Address& address) {
|
||||
bool IOCPEventActions::AddConnection(Event* event, Address& address) {
|
||||
if (event->GetType() & ET_CONNECT) {
|
||||
LOG_WARN_S << "repeat connect event";
|
||||
return false;
|
||||
@@ -236,57 +187,58 @@ bool IOCPEventActions::AddConnection(std::shared_ptr<Event>& event, Address& add
|
||||
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddSendEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddSendEvent");
|
||||
return false;
|
||||
}
|
||||
|
||||
EventOverlapped* context = (EventOverlapped*)event->GetData();
|
||||
if (!context) {
|
||||
context = sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)&event;
|
||||
auto rw_sock = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
context = rw_sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)event;
|
||||
event->SetData(context);
|
||||
}
|
||||
|
||||
if (address.GetType() == AT_IPV4) {
|
||||
SOCKADDR_IN local;
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_port = htons(0);
|
||||
local.sin_addr.S_un.S_addr = INADDR_ANY;
|
||||
if (bind(sock->GetSocket(), (sockaddr*)&local, sizeof(local)) != 0) {
|
||||
LOG_FATAL("bind local host failed! error code:%d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
}
|
||||
|
||||
} else {
|
||||
SOCKADDR_IN6 local;
|
||||
if (address.GetType() == AT_IPV4) {
|
||||
SOCKADDR_IN local;
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_port = htons(0);
|
||||
local.sin_addr.S_un.S_addr = INADDR_ANY;
|
||||
if (bind(sock->GetSocket(), (sockaddr*)&local, sizeof(local)) != 0) {
|
||||
LOG_FATAL("bind local host failed! error code:%d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
}
|
||||
|
||||
} else {
|
||||
SOCKADDR_IN6 local;
|
||||
local.sin6_flowinfo = 0;
|
||||
local.sin6_scope_id = 0;
|
||||
local.sin6_family = AF_INET6;
|
||||
local.sin6_port = 0;
|
||||
local.sin6_addr = in6addr_any;
|
||||
if (bind(sock->GetSocket(), (sockaddr*)&local, sizeof(local)) != 0) {
|
||||
LOG_FATAL("bind local host failed! error code:%d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
}
|
||||
}
|
||||
if (bind(sock->GetSocket(), (sockaddr*)&local, sizeof(local)) != 0) {
|
||||
LOG_FATAL("bind local host failed! error code:%d, info:%s", WSAGetLastError(), ErrnoInfo(WSAGetLastError()));
|
||||
}
|
||||
}
|
||||
|
||||
DWORD dwBytes = 0;
|
||||
int32_t ret = -1;
|
||||
if (address.GetType() == AT_IPV4) {
|
||||
SOCKADDR_IN addr;
|
||||
addr.sin_family = AF_INET6;
|
||||
addr.sin_port = htons(address.GetAddrPort());
|
||||
addr.sin_addr.S_un.S_addr = inet_addr(address.GetIp().c_str());
|
||||
SOCKADDR_IN addr;
|
||||
addr.sin_family = AF_INET6;
|
||||
addr.sin_port = htons(address.GetAddrPort());
|
||||
addr.sin_addr.S_un.S_addr = inet_addr(address.GetIp().c_str());
|
||||
ConnectEx((SOCKET)sock->GetSocket(), (sockaddr*)&addr, sizeof(addr), nullptr, 0, &dwBytes, &context->_overlapped);
|
||||
|
||||
} else {
|
||||
SOCKADDR_IN6 addr;
|
||||
addr.sin6_flowinfo = 0;
|
||||
addr.sin6_scope_id = 0;
|
||||
addr.sin6_family = AF_INET6;
|
||||
addr.sin6_port = htons(address.GetAddrPort());
|
||||
inet_pton(AF_INET6, address.GetIp().c_str(), &addr.sin6_addr);
|
||||
SOCKADDR_IN6 addr;
|
||||
addr.sin6_flowinfo = 0;
|
||||
addr.sin6_scope_id = 0;
|
||||
addr.sin6_family = AF_INET6;
|
||||
addr.sin6_port = htons(address.GetAddrPort());
|
||||
inet_pton(AF_INET6, address.GetIp().c_str(), &addr.sin6_addr);
|
||||
ConnectEx((SOCKET)sock->GetSocket(), (sockaddr*)&addr, sizeof(addr), nullptr, 0, &dwBytes, &context->_overlapped);
|
||||
}
|
||||
|
||||
|
||||
setsockopt((SOCKET)sock->GetSocket(), SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
|
||||
|
||||
auto rw_sock = std::dynamic_pointer_cast<RWSocket>(sock);
|
||||
@@ -302,13 +254,12 @@ bool IOCPEventActions::AddConnection(std::shared_ptr<Event>& event, Address& add
|
||||
return true;
|
||||
}
|
||||
}
|
||||
context->_event_type = ET_CONNECT;
|
||||
DoEvent(context, 0);
|
||||
|
||||
return false;
|
||||
rw_sock->OnConnect(CEC_CONNECT_REFUSE);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
bool IOCPEventActions::AddDisconnection(Event* event) {
|
||||
if (event->GetType() & ET_DISCONNECT) {
|
||||
LOG_WARN_S << "repeat disconnect event";
|
||||
return false;
|
||||
@@ -316,7 +267,7 @@ bool IOCPEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddSendEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddSendEvent");
|
||||
return false;
|
||||
}
|
||||
auto rw_sock = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
@@ -326,8 +277,9 @@ bool IOCPEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
|
||||
EventOverlapped* context = (EventOverlapped*)event->GetData();
|
||||
if (!context) {
|
||||
context = sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)&event;
|
||||
auto rw_sock = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
context = rw_sock->GetAlloter()->PoolNew<EventOverlapped>();
|
||||
context->_event = (void*)event;
|
||||
event->SetData(context);
|
||||
}
|
||||
|
||||
@@ -341,16 +293,13 @@ bool IOCPEventActions::AddDisconnection(std::shared_ptr<Event>& event) {
|
||||
LOG_DEBUG("post a new disconnect event");
|
||||
|
||||
event->AddType(ET_DISCONNECT);
|
||||
//DelEvent(event);
|
||||
//auto rw_sock = std::dynamic_pointer_cast<RWSocket>(sock);
|
||||
//rw_sock->OnDisConnect(CEC_CLOSED);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IOCPEventActions::DelEvent(std::shared_ptr<Event>& event) {
|
||||
bool IOCPEventActions::DelEvent(Event* event) {
|
||||
auto sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_WARN("socket is already distroyed! event %s", "AddSendEvent");
|
||||
LOG_WARN("socket is already destroyed! event %s", "AddSendEvent");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -393,7 +342,9 @@ void IOCPEventActions::ProcessEvent(int32_t wait_ms) {
|
||||
// do nothing
|
||||
|
||||
} else if (ERROR_SEM_TIMEOUT == dw_err ||
|
||||
WSAENOTCONN == dw_err ||
|
||||
WSAENOTCONN == dw_err ||
|
||||
WSAECONNABORTED == dw_err ||
|
||||
WSAENOTSOCK == dw_err ||
|
||||
ERROR_OPERATION_ABORTED == dw_err) {
|
||||
context->_event_type = INC_CONNECTION_BREAK;
|
||||
|
||||
@@ -415,7 +366,7 @@ void IOCPEventActions::Wakeup() {
|
||||
|
||||
bool IOCPEventActions::AddToIOCP(uint64_t sock) {
|
||||
if (CreateIoCompletionPort((HANDLE)sock, _iocp_handler, 0, 0) == NULL) {
|
||||
LOG_ERROR("IOCP bind socket to io completion port failed!");
|
||||
LOG_ERROR("IOCP bind socket to IO completion port failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -423,9 +374,9 @@ bool IOCPEventActions::AddToIOCP(uint64_t sock) {
|
||||
|
||||
void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
std::shared_ptr<Socket> sock;
|
||||
std::shared_ptr<Event> event;
|
||||
Event* event;
|
||||
|
||||
event = *(std::shared_ptr<Event>*)context->_event;
|
||||
event = (Event*)context->_event;
|
||||
sock = event->GetSocket();
|
||||
if (!sock) {
|
||||
LOG_ERROR("socket point is already destroy");
|
||||
@@ -437,7 +388,7 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
case ET_ACCEPT: {
|
||||
context->_event_type = 0;
|
||||
event->RemoveType(ET_ACCEPT);
|
||||
auto accpet_event = std::dynamic_pointer_cast<AcceptEvent>(event);
|
||||
auto accpet_event = dynamic_cast<WinAcceptEvent*>(event);
|
||||
accpet_event->SetBufOffset(bytes);
|
||||
std::shared_ptr<WinConnectSocket> connect_sock = std::dynamic_pointer_cast<WinConnectSocket>(sock);
|
||||
connect_sock->OnAccept(accpet_event);
|
||||
@@ -449,10 +400,10 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
std::shared_ptr<WinRWSocket> rw_socket = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (bytes == 0) {
|
||||
rw_socket->SetShutdown();
|
||||
rw_socket->OnDisConnect(CEC_CLOSED);
|
||||
rw_socket->OnDisConnect(event, CEC_CLOSED);
|
||||
|
||||
} else {
|
||||
rw_socket->OnRead(bytes);
|
||||
rw_socket->OnRead(event, bytes);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -462,10 +413,10 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
std::shared_ptr<WinRWSocket> rw_socket = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (bytes == 0) {
|
||||
rw_socket->SetShutdown();
|
||||
rw_socket->OnDisConnect(CEC_CLOSED);
|
||||
rw_socket->OnDisConnect(event, CEC_CLOSED);
|
||||
|
||||
} else {
|
||||
rw_socket->OnWrite(bytes);
|
||||
rw_socket->OnWrite(event, bytes);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -480,11 +431,11 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
case INC_CONNECTION_CLOSE:
|
||||
case ET_DISCONNECT: {
|
||||
context->_event_type = 0;
|
||||
event->RemoveType(ET_DISCONNECT);
|
||||
event->ForceSetType(ET_DISCONNECT);
|
||||
std::shared_ptr<WinRWSocket> rw_socket = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (rw_socket) {
|
||||
rw_socket->SetShutdown();
|
||||
rw_socket->OnDisConnect(CEC_CLOSED);
|
||||
rw_socket->OnDisConnect(event, CEC_CLOSED);
|
||||
|
||||
} else {
|
||||
LOG_WARN_S << "disconnect empty socket";
|
||||
@@ -493,11 +444,11 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
}
|
||||
case INC_CONNECTION_BREAK: {
|
||||
context->_event_type = 0;
|
||||
event->RemoveType(ET_DISCONNECT);
|
||||
event->ForceSetType(ET_DISCONNECT);
|
||||
std::shared_ptr<WinRWSocket> rw_socket = std::dynamic_pointer_cast<WinRWSocket>(sock);
|
||||
if (rw_socket) {
|
||||
rw_socket->SetShutdown();
|
||||
rw_socket->OnDisConnect(CEC_CONNECT_BREAK);
|
||||
rw_socket->OnDisConnect(event, CEC_CONNECT_BREAK);
|
||||
} else {
|
||||
LOG_WARN_S << "connect break empty socket";
|
||||
}
|
||||
@@ -505,7 +456,7 @@ void IOCPEventActions::DoEvent(EventOverlapped *context, uint32_t bytes) {
|
||||
}
|
||||
case INC_CONNECTION_REFUSE: {
|
||||
context->_event_type = 0;
|
||||
event->RemoveType(ET_CONNECT);
|
||||
event->ForceSetType(ET_DISCONNECT);
|
||||
std::shared_ptr<RWSocket> rw_socket = std::dynamic_pointer_cast<RWSocket>(sock);
|
||||
rw_socket->OnConnect(CEC_CONNECT_REFUSE);
|
||||
break;
|
||||
|
||||
@@ -6,11 +6,31 @@
|
||||
#ifndef CPPNET_EVENT_WIN_IOCP_ACTION
|
||||
#define CPPNET_EVENT_WIN_IOCP_ACTION
|
||||
|
||||
#include <WS2tcpip.h>
|
||||
#include "../action_interface.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
struct EventOverlapped;
|
||||
enum IOCP_NOTIFY_CODE {
|
||||
INC_WEAK_UP = 0xAAAAFFFF,
|
||||
INC_CONNECTION_BREAK = 0x100,
|
||||
INC_CONNECTION_REFUSE = 0x200,
|
||||
INC_CONNECTION_CLOSE = 0x400,
|
||||
};
|
||||
|
||||
struct EventOverlapped {
|
||||
OVERLAPPED _overlapped;
|
||||
uint32_t _event_type;
|
||||
void* _event;
|
||||
|
||||
EventOverlapped():
|
||||
_event_type(0),
|
||||
_event(nullptr){
|
||||
memset(&_overlapped, 0, sizeof(_overlapped));
|
||||
}
|
||||
|
||||
~EventOverlapped() {}
|
||||
};
|
||||
|
||||
// iocp event interface
|
||||
class IOCPEventActions:
|
||||
@@ -22,20 +42,21 @@ public:
|
||||
virtual bool Init(uint32_t thread_num = 0);
|
||||
virtual bool Dealloc();
|
||||
// net io event
|
||||
virtual bool AddSendEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddRecvEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddAcceptEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool AddSendEvent(Event* event);
|
||||
virtual bool AddRecvEvent(Event* event);
|
||||
virtual bool AddAcceptEvent(Event* event);
|
||||
virtual bool AddConnection(Event* event, Address& address);
|
||||
virtual bool AddDisconnection(Event* event);
|
||||
|
||||
virtual bool AddConnection(std::shared_ptr<Event>& event, Address& address);
|
||||
virtual bool AddDisconnection(std::shared_ptr<Event>& event);
|
||||
|
||||
virtual bool DelEvent(std::shared_ptr<Event>& event);
|
||||
virtual bool DelEvent(Event* event);
|
||||
// io thread process
|
||||
virtual void ProcessEvent(int32_t wait_ms);
|
||||
// weak up net io thread
|
||||
virtual void Wakeup();
|
||||
private:
|
||||
|
||||
bool AddToIOCP(uint64_t sock);
|
||||
|
||||
private:
|
||||
void DoEvent(EventOverlapped *socket_context, uint32_t bytes);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
// Use of this source code is governed by a BSD 3-Clause License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Author: caozhiyi (caozhiyi5@gmail.com)
|
||||
|
||||
#ifndef CPPNET_EVENT_WIN_RW_EVENT
|
||||
#define CPPNET_EVENT_WIN_RW_EVENT
|
||||
|
||||
#include "cppnet/cppnet_config.h"
|
||||
#include "cppnet/event/event_interface.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class RWEvent:
|
||||
public Event{
|
||||
public:
|
||||
RWEvent():
|
||||
_ex_data(nullptr) {}
|
||||
virtual ~RWEvent() {}
|
||||
|
||||
void SetExData(void* data) { _ex_data = data; }
|
||||
void* GetExData() { return _ex_data; }
|
||||
|
||||
private:
|
||||
void* _ex_data;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -27,14 +27,7 @@ ConnectSocket::ConnectSocket() {
|
||||
}
|
||||
|
||||
ConnectSocket::~ConnectSocket() {
|
||||
if (_sock > 0) {
|
||||
#ifdef __win__
|
||||
__all_socket_map.Erase(_sock);
|
||||
#else
|
||||
__all_socket_map.erase(_sock);
|
||||
#endif
|
||||
OsHandle::Close(_sock);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ConnectSocket::Bind(const std::string& ip, uint16_t port) {
|
||||
@@ -53,7 +46,7 @@ bool ConnectSocket::Bind(const std::string& ip, uint16_t port) {
|
||||
auto ret = OsHandle::Bind(_sock, _addr);
|
||||
|
||||
if (ret._return_value < 0) {
|
||||
LOG_FATAL("linux bind socket filed! error:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
LOG_FATAL("bind socket filed! error:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
OsHandle::Close(_sock);
|
||||
return false;
|
||||
}
|
||||
@@ -64,7 +57,7 @@ bool ConnectSocket::Bind(const std::string& ip, uint16_t port) {
|
||||
bool ConnectSocket::Listen() {
|
||||
auto ret = OsHandle::Listen(_sock);
|
||||
if (ret._return_value < 0) {
|
||||
LOG_FATAL("linux listen socket filed! error:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
LOG_FATAL("listen socket filed! error:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
OsHandle::Close(_sock);
|
||||
return false;
|
||||
}
|
||||
@@ -77,16 +70,4 @@ bool ConnectSocket::Listen() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void ConnectSocket::Accept() {
|
||||
if (!_accept_event) {
|
||||
_accept_event = std::make_shared<Event>();
|
||||
_accept_event->SetSocket(shared_from_this());
|
||||
}
|
||||
__all_socket_map[_sock] = shared_from_this();
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddAcceptEvent(_accept_event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,12 +25,11 @@ public:
|
||||
|
||||
virtual bool Bind(const std::string& ip, uint16_t port);
|
||||
virtual bool Listen();
|
||||
virtual void Accept();
|
||||
virtual void Accept() {}
|
||||
virtual void Accept(uint16_t index) {}
|
||||
virtual void Close() {}
|
||||
|
||||
virtual void OnAccept() {}
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Event> _accept_event;
|
||||
virtual void OnAccept(Event* event) {}
|
||||
};
|
||||
|
||||
std::shared_ptr<ConnectSocket> MakeConnectSocket();
|
||||
|
||||
@@ -9,12 +9,14 @@
|
||||
|
||||
#include "cppnet/cppnet_base.h"
|
||||
#include "cppnet/socket/rw_socket.h"
|
||||
#include "cppnet/event/event_interface.h"
|
||||
|
||||
#include "common/log/log.h"
|
||||
#include "common/os/convert.h"
|
||||
#include "common/network/socket.h"
|
||||
#include "common/network/io_handle.h"
|
||||
#include "common/alloter/pool_alloter.h"
|
||||
#include "cppnet/event/action_interface.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
@@ -22,15 +24,30 @@ std::shared_ptr<ConnectSocket> MakeConnectSocket() {
|
||||
return std::make_shared<PosixConnectSocket>();
|
||||
}
|
||||
|
||||
PosixConnectSocket::PosixConnectSocket() {
|
||||
PosixConnectSocket::PosixConnectSocket():
|
||||
_accept_event(nullptr) {
|
||||
|
||||
}
|
||||
|
||||
PosixConnectSocket::~PosixConnectSocket() {
|
||||
|
||||
if (_accept_event) {
|
||||
delete _accept_event;
|
||||
}
|
||||
}
|
||||
|
||||
void PosixConnectSocket::OnAccept() {
|
||||
void PosixConnectSocket::Accept() {
|
||||
if (!_accept_event) {
|
||||
_accept_event = new Event();
|
||||
_accept_event->SetSocket(shared_from_this());
|
||||
}
|
||||
__all_socket_map[_sock] = shared_from_this();
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddAcceptEvent(_accept_event);
|
||||
}
|
||||
}
|
||||
|
||||
void PosixConnectSocket::OnAccept(Event*) {
|
||||
while (true) {
|
||||
std::shared_ptr<AlloterWrap> alloter = std::make_shared<AlloterWrap>(MakePoolAlloterPtr());
|
||||
Address address;
|
||||
|
||||
@@ -10,13 +10,17 @@
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class Event;
|
||||
class PosixConnectSocket:
|
||||
public ConnectSocket {
|
||||
public:
|
||||
PosixConnectSocket();
|
||||
~PosixConnectSocket();
|
||||
|
||||
virtual void OnAccept();
|
||||
virtual void Accept();
|
||||
virtual void OnAccept(Event* event);
|
||||
private:
|
||||
Event* _accept_event;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -11,10 +11,15 @@
|
||||
#include "cppnet/event/event_interface.h"
|
||||
#include "cppnet/event/action_interface.h"
|
||||
|
||||
#include "common/log/log.h"
|
||||
#include "common/buffer/buffer_queue.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
std::shared_ptr<RWSocket> MakeRWSocket() {
|
||||
return std::make_shared<PosixRWSocket>();
|
||||
}
|
||||
|
||||
std::shared_ptr<RWSocket> MakeRWSocket(std::shared_ptr<AlloterWrap> alloter) {
|
||||
return std::make_shared<PosixRWSocket>(alloter);
|
||||
}
|
||||
@@ -23,23 +28,51 @@ std::shared_ptr<RWSocket> MakeRWSocket(uint64_t sock, std::shared_ptr<AlloterWra
|
||||
return std::make_shared<PosixRWSocket>(sock, alloter);
|
||||
}
|
||||
|
||||
PosixRWSocket::PosixRWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(alloter) {
|
||||
PosixRWSocket::PosixRWSocket():
|
||||
RWSocket(),
|
||||
_event(nullptr) {
|
||||
|
||||
_write_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
}
|
||||
|
||||
PosixRWSocket::PosixRWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(alloter),
|
||||
_event(nullptr) {
|
||||
|
||||
_write_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
}
|
||||
|
||||
PosixRWSocket::PosixRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(sock, alloter) {
|
||||
RWSocket(sock, alloter),
|
||||
_event(nullptr) {
|
||||
|
||||
_write_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
}
|
||||
|
||||
PosixRWSocket::~PosixRWSocket() {
|
||||
if (_event) {
|
||||
_alloter->PoolDelete<Event>(_event);
|
||||
}
|
||||
}
|
||||
|
||||
void PosixRWSocket::Read() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNew<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddRecvEvent(_event);
|
||||
}
|
||||
}
|
||||
|
||||
bool PosixRWSocket::Write(const char* src, uint32_t len) {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event = _alloter->PoolNew<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
@@ -62,14 +95,66 @@ bool PosixRWSocket::Write(const char* src, uint32_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
void PosixRWSocket::OnRead(uint32_t len) {
|
||||
void PosixRWSocket::Connect(const std::string& ip, uint16_t port) {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNew<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
if (_sock == 0) {
|
||||
auto ret = OsHandle::TcpSocket();
|
||||
if (ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. error:%d", ret._errno);
|
||||
return;
|
||||
}
|
||||
_sock = ret._return_value;
|
||||
}
|
||||
|
||||
|
||||
_addr.SetIp(ip);
|
||||
_addr.SetAddrPort(port);
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddConnection(_event, _addr);
|
||||
}
|
||||
}
|
||||
|
||||
void PosixRWSocket::Disconnect() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNew<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddDisconnection(_event);
|
||||
}
|
||||
}
|
||||
|
||||
void PosixRWSocket::OnRead(Event*, uint32_t len) {
|
||||
Recv(len);
|
||||
}
|
||||
|
||||
void PosixRWSocket::OnWrite(uint32_t len) {
|
||||
void PosixRWSocket::OnWrite(Event*,uint32_t len) {
|
||||
Send();
|
||||
}
|
||||
|
||||
void PosixRWSocket::OnDisConnect(Event*, uint16_t err) {
|
||||
auto sock = shared_from_this();
|
||||
__all_socket_map.erase(_sock);
|
||||
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (cppnet_base) {
|
||||
cppnet_base->OnDisConnect(sock, err);
|
||||
}
|
||||
|
||||
// not active disconnection
|
||||
if (_event && !(_event->GetType() & ET_DISCONNECT)) {
|
||||
OsHandle::Close(_sock);
|
||||
}
|
||||
}
|
||||
|
||||
bool PosixRWSocket::Recv(uint32_t len) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
@@ -102,12 +187,12 @@ bool PosixRWSocket::Recv(uint32_t len) {
|
||||
break;
|
||||
|
||||
} else {
|
||||
OnDisConnect(CEC_CONNECT_BREAK);
|
||||
OnDisConnect(nullptr, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (ret._return_value == 0) {
|
||||
OnDisConnect(CEC_CLOSED);
|
||||
OnDisConnect(nullptr, CEC_CLOSED);
|
||||
return false;
|
||||
|
||||
} else {
|
||||
@@ -120,7 +205,7 @@ bool PosixRWSocket::Recv(uint32_t len) {
|
||||
need_expend = true;
|
||||
}
|
||||
}
|
||||
cppnet_base->OnRead(shared_from_this(), off_set);
|
||||
cppnet_base->OnRead(shared_from_this(), _read_buffer, off_set);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,11 +234,11 @@ bool PosixRWSocket::Send() {
|
||||
return false;
|
||||
|
||||
} else if (errno == EBADMSG) {
|
||||
OnDisConnect(CEC_CONNECT_BREAK);
|
||||
OnDisConnect(nullptr, CEC_CONNECT_BREAK);
|
||||
return false;
|
||||
|
||||
} else {
|
||||
OnDisConnect(CEC_CLOSED);
|
||||
OnDisConnect(nullptr, CEC_CLOSED);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,22 +10,33 @@
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class Event;
|
||||
class PosixRWSocket:
|
||||
public RWSocket {
|
||||
|
||||
public:
|
||||
PosixRWSocket();
|
||||
PosixRWSocket(std::shared_ptr<AlloterWrap> alloter);
|
||||
PosixRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter);
|
||||
virtual ~PosixRWSocket();
|
||||
|
||||
virtual void Read();
|
||||
virtual bool Write(const char* src, uint32_t len);
|
||||
virtual void Connect(const std::string& ip, uint16_t port);
|
||||
virtual void Disconnect();
|
||||
|
||||
virtual void OnRead(uint32_t len = 0);
|
||||
virtual void OnWrite(uint32_t len = 0);
|
||||
virtual void OnRead(Event* event, uint32_t len = 0);
|
||||
virtual void OnWrite(Event* event, uint32_t len = 0);
|
||||
virtual void OnDisConnect(Event* event, uint16_t err);
|
||||
|
||||
private:
|
||||
bool Recv(uint32_t len);
|
||||
bool Send();
|
||||
|
||||
private:
|
||||
Event* _event;
|
||||
std::shared_ptr<BufferQueue> _write_buffer;
|
||||
std::shared_ptr<BufferQueue> _read_buffer;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include <errno.h>
|
||||
|
||||
#include "rw_socket.h"
|
||||
|
||||
#include "cppnet/dispatcher.h"
|
||||
#include "cppnet/cppnet_base.h"
|
||||
#include "cppnet/cppnet_config.h"
|
||||
@@ -16,36 +15,33 @@
|
||||
#include "common/log/log.h"
|
||||
#include "common/alloter/pool_block.h"
|
||||
#include "common/buffer/buffer_queue.h"
|
||||
#include "common/alloter/pool_alloter.h"
|
||||
#include "common/alloter/alloter_interface.h"
|
||||
|
||||
namespace cppnet {
|
||||
RWSocket::RWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
Socket(alloter) {
|
||||
|
||||
_block_pool = _alloter->PoolNewSharePtr<BlockMemoryPool>(__mem_block_size, __mem_block_add_step);
|
||||
_write_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
RWSocket::RWSocket():
|
||||
RWSocket(0, std::make_shared<AlloterWrap>(MakePoolAlloterPtr())) {
|
||||
}
|
||||
|
||||
RWSocket::RWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(0, alloter) {
|
||||
|
||||
}
|
||||
|
||||
RWSocket::RWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter):
|
||||
Socket(sock, alloter) {
|
||||
|
||||
Socket(sock),
|
||||
_alloter(alloter) {
|
||||
_block_pool = _alloter->PoolNewSharePtr<BlockMemoryPool>(__mem_block_size, __mem_block_add_step);
|
||||
_write_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
}
|
||||
|
||||
RWSocket::~RWSocket() {
|
||||
// free buffer early than pool
|
||||
_write_buffer.reset();
|
||||
_read_buffer.reset();
|
||||
_block_pool.reset();
|
||||
|
||||
}
|
||||
|
||||
bool RWSocket::GetAddress(std::string& ip, uint16_t& port) {
|
||||
ip = _addr.GetIp();
|
||||
port = _addr.GetAddrPort();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,55 +50,6 @@ bool RWSocket::Close() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void RWSocket::Read() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddRecvEvent(_event);
|
||||
}
|
||||
}
|
||||
|
||||
void RWSocket::Connect(const std::string& ip, uint16_t port) {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
if (_sock == 0) {
|
||||
auto ret = OsHandle::TcpSocket();
|
||||
if (ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. error:%d", ret._errno);
|
||||
return;
|
||||
}
|
||||
_sock = ret._return_value;
|
||||
}
|
||||
|
||||
|
||||
_addr.SetIp(ip);
|
||||
_addr.SetAddrPort(port);
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddConnection(_event, _addr);
|
||||
}
|
||||
}
|
||||
|
||||
void RWSocket::Disconnect() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddDisconnection(_event);
|
||||
}
|
||||
}
|
||||
|
||||
void RWSocket::OnTimer() {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
@@ -126,7 +73,7 @@ void RWSocket::StopTimer(uint64_t timer_id) {
|
||||
}
|
||||
}
|
||||
|
||||
void RWSocket::OnConnect(uint16_t err) {
|
||||
void RWSocket::OnConnect(Event* event, uint16_t err) {
|
||||
auto sock = shared_from_this();
|
||||
if (err == CEC_SUCCESS) {
|
||||
__all_socket_map[_sock] = sock;
|
||||
@@ -142,22 +89,4 @@ void RWSocket::OnConnect(uint16_t err) {
|
||||
}
|
||||
}
|
||||
|
||||
void RWSocket::OnDisConnect(uint16_t err) {
|
||||
auto sock = shared_from_this();
|
||||
#ifdef __win__
|
||||
__all_socket_map.Erase(_sock);
|
||||
#else
|
||||
__all_socket_map.erase(_sock);
|
||||
#endif
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (cppnet_base) {
|
||||
cppnet_base->OnDisConnect(sock, err);
|
||||
}
|
||||
|
||||
// not active disconnection
|
||||
if (_event && !(_event->GetType() & ET_DISCONNECT)) {
|
||||
OsHandle::Close(_sock);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace cppnet {
|
||||
|
||||
class Event;
|
||||
class Dispatcher;
|
||||
class BufferQueue;
|
||||
class AlloterWrap;
|
||||
class BlockMemoryPool;
|
||||
@@ -23,6 +22,7 @@ class RWSocket:
|
||||
public std::enable_shared_from_this<RWSocket> {
|
||||
|
||||
public:
|
||||
RWSocket();
|
||||
RWSocket(std::shared_ptr<AlloterWrap> alloter);
|
||||
RWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter);
|
||||
virtual ~RWSocket();
|
||||
@@ -31,32 +31,33 @@ public:
|
||||
|
||||
virtual bool Close();
|
||||
|
||||
virtual void Read();
|
||||
virtual void Read() {}
|
||||
virtual bool Write(const char* src, uint32_t len) { return false; }
|
||||
virtual void Connect(const std::string& ip, uint16_t port);
|
||||
virtual void Disconnect();
|
||||
virtual void Connect(const std::string& ip, uint16_t port) {}
|
||||
virtual void Disconnect() {}
|
||||
|
||||
virtual uint64_t AddTimer(uint32_t interval, bool always = false);
|
||||
virtual void StopTimer(uint64_t timer_id);
|
||||
|
||||
virtual void OnTimer();
|
||||
virtual void OnRead(uint32_t len = 0) {}
|
||||
virtual void OnWrite(uint32_t len = 0) {}
|
||||
virtual void OnConnect(uint16_t err);
|
||||
virtual void OnDisConnect(uint16_t err);
|
||||
virtual void OnRead(Event* event, uint32_t len = 0) {}
|
||||
virtual void OnWrite(Event* event, uint32_t len = 0) {}
|
||||
virtual void OnConnect(Event* event, uint16_t err);
|
||||
virtual void OnDisConnect(Event* event, uint16_t err) {}
|
||||
|
||||
virtual void SetShutdown() { }
|
||||
virtual bool IsShutdown() { return false; }
|
||||
|
||||
virtual std::shared_ptr<BufferQueue> GetReadBuffer() { return nullptr; }
|
||||
|
||||
std::shared_ptr<AlloterWrap> GetAlloter() { return _alloter; }
|
||||
|
||||
std::shared_ptr<BufferQueue> GetReadBuffer() { return _read_buffer; }
|
||||
std::shared_ptr<BufferQueue> GetWriteBuffer() { return _write_buffer; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<AlloterWrap> _alloter;
|
||||
std::shared_ptr<BlockMemoryPool> _block_pool;
|
||||
|
||||
std::shared_ptr<Event> _event;
|
||||
|
||||
std::shared_ptr<BufferQueue> _write_buffer;
|
||||
std::shared_ptr<BufferQueue> _read_buffer;
|
||||
};
|
||||
|
||||
std::shared_ptr<RWSocket> MakeRWSocket();
|
||||
std::shared_ptr<RWSocket> MakeRWSocket(std::shared_ptr<AlloterWrap> alloter);
|
||||
std::shared_ptr<RWSocket> MakeRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter);
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
namespace cppnet {
|
||||
|
||||
#ifdef __win__
|
||||
ThreadSafeUnorderedMap<uint64_t, std::shared_ptr<Socket>> Socket::__all_socket_map;
|
||||
ThreadSafeUnorderedMap<uint64_t, std::shared_ptr<Socket>> Socket::__all_socket_map;
|
||||
#else
|
||||
thread_local std::unordered_map<uint64_t, std::shared_ptr<Socket>> Socket::__all_socket_map;
|
||||
thread_local std::unordered_map<uint64_t, std::shared_ptr<Socket>> Socket::__all_socket_map;
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -8,28 +8,25 @@
|
||||
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/network/address.h"
|
||||
#ifdef __win__
|
||||
#include "common/structure/thread_safe_unordered_map.h"
|
||||
#else
|
||||
#include <unordered_map>
|
||||
#endif
|
||||
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class Buffer;
|
||||
class Address;
|
||||
class CppNetBase;
|
||||
class Dispatcher;
|
||||
class AlloterWrap;
|
||||
class EventActions;
|
||||
class Socket {
|
||||
public:
|
||||
Socket(): _sock(0), _addr() {}
|
||||
Socket(std::shared_ptr<AlloterWrap> alloter):
|
||||
_sock(0), _alloter(alloter) {}
|
||||
Socket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter):
|
||||
_sock(sock), _alloter(alloter) {}
|
||||
Socket(): _sock(0) {}
|
||||
Socket(uint64_t sock): _sock(sock) {}
|
||||
virtual ~Socket() {}
|
||||
|
||||
void SetSocket(const uint64_t& sock) { _sock = sock; }
|
||||
@@ -47,15 +44,10 @@ public:
|
||||
void SetDispatcher(std::shared_ptr<Dispatcher> dis) { _dispatcher = dis; }
|
||||
std::shared_ptr<Dispatcher> GetDispatcher() { return _dispatcher.lock(); }
|
||||
|
||||
void SetAlloter(std::shared_ptr<AlloterWrap> alloter) { _alloter = alloter; }
|
||||
std::shared_ptr<AlloterWrap> GetAlloter() { return _alloter; }
|
||||
|
||||
protected:
|
||||
uint64_t _sock;
|
||||
Address _addr;
|
||||
Address _addr;
|
||||
|
||||
std::shared_ptr<AlloterWrap> _alloter;
|
||||
|
||||
std::weak_ptr<CppNetBase> _cppnet_base;
|
||||
std::weak_ptr<EventActions> _event_actions;
|
||||
std::weak_ptr<Dispatcher> _dispatcher;
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
#include "cppnet/cppnet_base.h"
|
||||
#include "cppnet/socket/rw_socket.h"
|
||||
#include "cppnet/event/win/expend_func.h"
|
||||
#include "cppnet/event/win/iocp_action.h"
|
||||
#include "cppnet/event/win/accept_event.h"
|
||||
#include "cppnet/event/action_interface.h"
|
||||
#include "cppnet/socket/win/win_rw_socket.h"
|
||||
|
||||
#include "common/log/log.h"
|
||||
#include "common/os/convert.h"
|
||||
@@ -29,16 +30,52 @@ std::shared_ptr<ConnectSocket> MakeConnectSocket() {
|
||||
return std::make_shared<WinConnectSocket>();
|
||||
}
|
||||
|
||||
WinConnectSocket::WinConnectSocket():
|
||||
_in_actions(false) {
|
||||
WinConnectSocket::WinConnectSocket() {
|
||||
// create all accept event.
|
||||
for (uint16_t i = 0; i < __iocp_accept_event_num; i++) {
|
||||
auto event = std::make_shared<AcceptEvent>(i);
|
||||
auto event = new WinAcceptEvent(i);
|
||||
_accept_event_vec.emplace_back(event);
|
||||
}
|
||||
}
|
||||
|
||||
WinConnectSocket::~WinConnectSocket() {
|
||||
__all_socket_map.Erase(_sock);
|
||||
for (auto iter = _accept_event_vec.begin(); iter != _accept_event_vec.end(); iter++) {
|
||||
delete *iter;
|
||||
}
|
||||
}
|
||||
|
||||
bool WinConnectSocket::Bind(const std::string& ip, uint16_t port) {
|
||||
if (_sock == 0) {
|
||||
auto ret = OsHandle::TcpSocket();
|
||||
if (ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. errno:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
return false;
|
||||
}
|
||||
_sock = ret._return_value;
|
||||
}
|
||||
|
||||
// add to iocp.
|
||||
auto action = GetEventActions();
|
||||
auto iocp = std::dynamic_pointer_cast<IOCPEventActions>(action);
|
||||
if (!iocp->AddToIOCP(_sock)) {
|
||||
LOG_FATAL("add accept socket to iocp failed!");
|
||||
OsHandle::Close(_sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
_addr.SetIp(ip);
|
||||
_addr.SetAddrPort(port);
|
||||
|
||||
auto ret = OsHandle::Bind(_sock, _addr);
|
||||
|
||||
if (ret._return_value < 0) {
|
||||
LOG_FATAL("window bind socket filed! error:%d, info:%s", ret._errno, ErrnoInfo(ret._errno));
|
||||
OsHandle::Close(_sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WinConnectSocket::Accept() {
|
||||
@@ -49,29 +86,34 @@ void WinConnectSocket::Accept() {
|
||||
}
|
||||
|
||||
void WinConnectSocket::Accept(uint16_t index) {
|
||||
auto& event = _accept_event_vec[index];
|
||||
auto accept_event = std::dynamic_pointer_cast<AcceptEvent>(event);
|
||||
// create a new socket
|
||||
auto sock_ret = OsHandle::TcpSocket();
|
||||
if (sock_ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. errno:%d, info:%s", sock_ret._errno, ErrnoInfo(sock_ret._errno));
|
||||
return;
|
||||
}
|
||||
auto event = _accept_event_vec[index];
|
||||
auto accept_event = dynamic_cast<WinAcceptEvent*>(event);
|
||||
// create a new socket
|
||||
auto sock_ret = OsHandle::TcpSocket();
|
||||
if (sock_ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. errno:%d, info:%s", sock_ret._errno, ErrnoInfo(sock_ret._errno));
|
||||
return;
|
||||
}
|
||||
setsockopt(sock_ret._return_value, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
|
||||
(char *)&_sock, sizeof(_sock));
|
||||
|
||||
accept_event->SetClientSocket(sock_ret._return_value);
|
||||
if (!accept_event->GetSocket()) {
|
||||
accept_event->SetSocket(shared_from_this());
|
||||
}
|
||||
if (!accept_event->GetSocket()) {
|
||||
accept_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
actions->AddAcceptEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WinConnectSocket::OnAccept(std::shared_ptr<AcceptEvent> event) {
|
||||
void WinConnectSocket::Close() {
|
||||
__all_socket_map.Erase(_sock);
|
||||
OsHandle::Close(_sock);
|
||||
}
|
||||
|
||||
void WinConnectSocket::OnAccept(Event* event) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
return;
|
||||
@@ -82,18 +124,18 @@ void WinConnectSocket::OnAccept(std::shared_ptr<AcceptEvent> event) {
|
||||
SOCKADDR_STORAGE* LocalAddr = NULL;
|
||||
int localLen = sizeof(SOCKADDR_STORAGE);
|
||||
|
||||
auto accept_event = dynamic_cast<WinAcceptEvent*>(event);
|
||||
|
||||
// accept a socket and read msg
|
||||
AcceptExSockAddrs(event->GetBuf(), __iocp_buff_size - ((sizeof(SOCKADDR_STORAGE) + 16) * 2),
|
||||
AcceptExSockAddrs(accept_event->GetBuf(), __iocp_buff_size - ((sizeof(SOCKADDR_STORAGE) + 16) * 2),
|
||||
sizeof(SOCKADDR_STORAGE) + 16, sizeof(SOCKADDR_STORAGE) + 16, (LPSOCKADDR*)&LocalAddr, &localLen, (LPSOCKADDR*)&client_addr, &remote_len);
|
||||
|
||||
// Does this call have any effect ?
|
||||
setsockopt(event->GetClientSocket(), SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
|
||||
setsockopt(accept_event->GetClientSocket(), SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
|
||||
(char *)&_sock, sizeof(_sock));
|
||||
|
||||
// create a new rw socket
|
||||
std::shared_ptr<AlloterWrap> alloter = std::make_shared<AlloterWrap>(MakePoolAlloterPtr());
|
||||
|
||||
Address address;
|
||||
|
||||
SOCKADDR* addr_pt = (SOCKADDR*)client_addr;
|
||||
void* addr = nullptr;
|
||||
switch (addr_pt->sa_family) {
|
||||
@@ -115,25 +157,32 @@ void WinConnectSocket::OnAccept(std::shared_ptr<AcceptEvent> event) {
|
||||
inet_ntop(AF_INET6, addr, str_addr, sizeof(str_addr));
|
||||
address.SetIp(str_addr);
|
||||
|
||||
auto sock = MakeRWSocket(event->GetClientSocket(), std::move(alloter));
|
||||
// create a new socket
|
||||
std::shared_ptr<AlloterWrap> alloter = std::make_shared<AlloterWrap>(MakePoolAlloterPtr());
|
||||
auto sock = MakeRWSocket(accept_event->GetClientSocket(), std::move(alloter));
|
||||
|
||||
sock->SetCppNetBase(cppnet_base);
|
||||
sock->SetEventActions(_event_actions);
|
||||
sock->SetAddress(std::move(address));
|
||||
sock->SetDispatcher(GetDispatcher());
|
||||
|
||||
__all_socket_map[event->GetClientSocket()] = sock;
|
||||
auto buffer = sock->GetReadBuffer();
|
||||
buffer->Write(accept_event->GetBuf(), accept_event->GetBufOffset());
|
||||
|
||||
auto buffer = sock->GetReadBuffer();
|
||||
buffer->Write(event->GetBuf(), event->GetBufOffset());
|
||||
// add socket to iocp
|
||||
auto action = GetEventActions();
|
||||
auto iocp = std::dynamic_pointer_cast<IOCPEventActions>(action);
|
||||
iocp->AddToIOCP(accept_event->GetClientSocket());
|
||||
|
||||
// add socket global cache.
|
||||
__all_socket_map[accept_event->GetClientSocket()] = sock;
|
||||
|
||||
// call accept call back function
|
||||
cppnet_base->OnAccept(sock);
|
||||
cppnet_base->OnRead(sock, event->GetBufOffset());
|
||||
cppnet_base->OnRead(sock, accept_event->GetBufOffset());
|
||||
|
||||
//post accept again
|
||||
Accept(event->GetIndex());
|
||||
Accept(accept_event->GetIndex());
|
||||
|
||||
// wait for read
|
||||
sock->Read();
|
||||
|
||||
@@ -10,24 +10,23 @@
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class AcceptEvent;
|
||||
class Event;
|
||||
class WinAcceptEvent;
|
||||
class WinConnectSocket:
|
||||
public ConnectSocket {
|
||||
public:
|
||||
WinConnectSocket();
|
||||
~WinConnectSocket();
|
||||
|
||||
virtual bool Bind(const std::string& ip, uint16_t port);
|
||||
virtual void Accept();
|
||||
void Accept(uint16_t index);
|
||||
virtual void Accept(uint16_t index);
|
||||
virtual void Close();
|
||||
|
||||
void OnAccept(std::shared_ptr<AcceptEvent> event);
|
||||
|
||||
void SetInActions(bool in) { _in_actions = in; }
|
||||
bool GetInActions() { return _in_actions; }
|
||||
virtual void OnAccept(Event* event);
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Event>> _accept_event_vec;
|
||||
bool _in_actions;
|
||||
std::vector<Event*> _accept_event_vec;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,220 @@
|
||||
|
||||
#include "cppnet/cppnet_base.h"
|
||||
#include "cppnet/cppnet_config.h"
|
||||
#include "cppnet/event/win/rw_event.h"
|
||||
#include "cppnet/event/action_interface.h"
|
||||
#include "cppnet/event/event_interface.h"
|
||||
#include "cppnet/event/win/iocp_action.h"
|
||||
|
||||
#include "common/log/log.h"
|
||||
#include "common/buffer/buffer_queue.h"
|
||||
#include "common/alloter/pool_alloter.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
WinRWSocket::WinRWSocket():
|
||||
RWSocket() {
|
||||
}
|
||||
|
||||
WinRWSocket::WinRWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(alloter) {
|
||||
}
|
||||
|
||||
WinRWSocket::WinRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(sock, alloter),
|
||||
_shutdown(false),
|
||||
_is_reading(false) {
|
||||
|
||||
}
|
||||
|
||||
WinRWSocket::~WinRWSocket() {
|
||||
|
||||
}
|
||||
|
||||
void WinRWSocket::Read() {
|
||||
if (_is_reading) {
|
||||
LOG_WARN_S << "already in reading. sock:" << _sock;
|
||||
return;
|
||||
}
|
||||
|
||||
auto rw_event = _alloter->PoolNew<Event>();
|
||||
auto buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
rw_event->SetBuffer(buffer);
|
||||
rw_event->SetSocket(shared_from_this());
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddRecvEvent(rw_event)) {
|
||||
_is_reading = true;
|
||||
AddEvent(rw_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WinRWSocket::Write(const char* src, uint32_t len) {
|
||||
// create new write buffer
|
||||
auto buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
buffer->Write(src, len);
|
||||
|
||||
// create new write event
|
||||
auto rw_event = _alloter->PoolNew<Event>();
|
||||
rw_event->SetBuffer(buffer);
|
||||
rw_event->SetSocket(shared_from_this());
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddSendEvent(rw_event)) {
|
||||
AddEvent(rw_event);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void WinRWSocket::Connect(const std::string& ip, uint16_t port) {
|
||||
if (_sock == 0) {
|
||||
auto ret = OsHandle::TcpSocket();
|
||||
if (ret._return_value < 0) {
|
||||
LOG_ERROR("create socket failed. error:%d", ret._errno);
|
||||
return;
|
||||
}
|
||||
_sock = ret._return_value;
|
||||
}
|
||||
|
||||
_addr.SetIp(ip);
|
||||
_addr.SetAddrPort(port);
|
||||
|
||||
auto rw_event = _alloter->PoolNew<Event>();
|
||||
rw_event->SetSocket(shared_from_this());
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddConnection(rw_event, _addr)) {
|
||||
AddEvent(rw_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WinRWSocket::Disconnect() {
|
||||
auto rw_event = _alloter->PoolNew<Event>();
|
||||
rw_event->SetSocket(shared_from_this());
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddDisconnection(rw_event)) {
|
||||
AddEvent(rw_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WinRWSocket::OnRead(Event* event, uint32_t len) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
return;
|
||||
}
|
||||
if (len == 0) {
|
||||
LOG_ERROR("read invalid length. sock:%d", _sock);
|
||||
return;
|
||||
}
|
||||
|
||||
auto rw_event = dynamic_cast<Event*>(event);
|
||||
auto buffer = rw_event->GetBuffer();
|
||||
buffer->MoveWritePt(len);
|
||||
|
||||
if (!_read_buffer) {
|
||||
_read_buffer = buffer;
|
||||
|
||||
} else {
|
||||
buffer->Read(_read_buffer, len);
|
||||
}
|
||||
|
||||
RemvoeEvent(event);
|
||||
_is_reading = false;
|
||||
|
||||
cppnet_base->OnRead(shared_from_this(), _read_buffer, len);
|
||||
|
||||
if (_read_buffer->GetCanReadLength() == 0) {
|
||||
_read_buffer.reset();
|
||||
}
|
||||
|
||||
// read again
|
||||
Read();
|
||||
}
|
||||
|
||||
void WinRWSocket::OnWrite(Event* event, uint32_t len) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
return;
|
||||
}
|
||||
if (len == 0) {
|
||||
LOG_ERROR("read invalid length. sock:%d", _sock);
|
||||
return;
|
||||
}
|
||||
|
||||
auto rw_event = dynamic_cast<Event*>(event);
|
||||
auto buffer = rw_event->GetBuffer();
|
||||
buffer->MoveReadPt(len);
|
||||
|
||||
cppnet_base->OnWrite(shared_from_this(), len);
|
||||
|
||||
// post send event again.
|
||||
if (buffer->GetCanReadLength() > 0) {
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddSendEvent(event)) {
|
||||
LOG_ERROR("post send event. sock:%d", _sock);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
RemvoeEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void WinRWSocket::OnDisConnect(Event* event, uint16_t err) {
|
||||
RemvoeEvent(event);
|
||||
|
||||
if (EventEmpty() && IsShutdown()) {
|
||||
auto sock = shared_from_this();
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (cppnet_base) {
|
||||
cppnet_base->OnDisConnect(sock, err);
|
||||
}
|
||||
|
||||
__all_socket_map.Erase(_sock);
|
||||
OsHandle::Close(_sock);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<BufferQueue> WinRWSocket::GetReadBuffer() {
|
||||
if (!_read_buffer) {
|
||||
_read_buffer = _alloter->PoolNewSharePtr<BufferQueue>(_block_pool, _alloter);
|
||||
}
|
||||
return _read_buffer;
|
||||
}
|
||||
|
||||
void WinRWSocket::AddEvent(Event* event) {
|
||||
std::lock_guard<std::mutex> lock(_event_mutex);
|
||||
_event_set.insert(std::move(event));
|
||||
}
|
||||
|
||||
void WinRWSocket::RemvoeEvent(Event* event) {
|
||||
std::lock_guard<std::mutex> lock(_event_mutex);
|
||||
_event_set.erase(event);
|
||||
EventOverlapped* data = (EventOverlapped*)event->GetData();
|
||||
if (data) {
|
||||
_alloter->PoolDelete<EventOverlapped>(data);
|
||||
}
|
||||
_alloter->PoolDelete<Event>(event);
|
||||
}
|
||||
|
||||
bool WinRWSocket::EventEmpty() {
|
||||
std::lock_guard<std::mutex> lock(_event_mutex);
|
||||
return _event_set.empty();
|
||||
}
|
||||
|
||||
std::shared_ptr<RWSocket> MakeRWSocket() {
|
||||
return std::make_shared<WinRWSocket>();
|
||||
}
|
||||
|
||||
std::shared_ptr<RWSocket> MakeRWSocket(std::shared_ptr<AlloterWrap> alloter) {
|
||||
return std::make_shared<WinRWSocket>(alloter);
|
||||
}
|
||||
@@ -23,145 +229,5 @@ std::shared_ptr<RWSocket> MakeRWSocket(uint64_t sock, std::shared_ptr<AlloterWra
|
||||
return std::make_shared<WinRWSocket>(sock, alloter);
|
||||
}
|
||||
|
||||
WinRWSocket::WinRWSocket(std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(alloter),
|
||||
_ref_count(0),
|
||||
_shutdown(false) {
|
||||
|
||||
}
|
||||
|
||||
WinRWSocket::WinRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter):
|
||||
RWSocket(sock, alloter),
|
||||
_ref_count(0),
|
||||
_shutdown(false) {
|
||||
|
||||
}
|
||||
|
||||
WinRWSocket::~WinRWSocket() {
|
||||
|
||||
}
|
||||
|
||||
void WinRWSocket::Read() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<RWEvent>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddRecvEvent(_event)) {
|
||||
Incref();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WinRWSocket::Write(const char* src, uint32_t len) {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<RWEvent>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
//can't send now
|
||||
if (_write_buffer->GetCanReadLength() > 0) {
|
||||
if (_write_buffer->GetCanReadLength() > __max_write_cache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_write_buffer->Write(src, len);
|
||||
return false;
|
||||
|
||||
} else {
|
||||
_write_buffer->Write(src, len);
|
||||
return Send();
|
||||
}
|
||||
}
|
||||
|
||||
void WinRWSocket::Connect(const std::string& ip, uint16_t port) {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<RWEvent>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
RWSocket::Connect(ip, port);
|
||||
}
|
||||
|
||||
void WinRWSocket::Disconnect() {
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
if (!_event) {
|
||||
_event = _alloter->PoolNewSharePtr<Event>();
|
||||
_event->SetSocket(shared_from_this());
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions) {
|
||||
if (actions->AddDisconnection(_event)) {
|
||||
Incref();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WinRWSocket::OnRead(uint32_t len) {
|
||||
Recv(len);
|
||||
if (!Decref()) {
|
||||
return;
|
||||
}
|
||||
// wait for read again
|
||||
Read();
|
||||
}
|
||||
|
||||
void WinRWSocket::OnWrite(uint32_t len) {
|
||||
Send(len);
|
||||
Decref();
|
||||
}
|
||||
|
||||
void WinRWSocket::OnDisConnect(uint16_t err) {
|
||||
Decref(err);
|
||||
}
|
||||
|
||||
bool WinRWSocket::Decref(uint16_t err) {
|
||||
int16_t ref = _ref_count.fetch_sub(1);
|
||||
if (ref == 1 && IsShutdown()) {
|
||||
RWSocket::OnDisConnect(err);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WinRWSocket::Recv(uint32_t len) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
return false;
|
||||
}
|
||||
if (len == 0) {
|
||||
LOG_ERROR("read invalid length. sock:%d", _sock);
|
||||
}
|
||||
|
||||
_read_buffer->MoveWritePt(len);
|
||||
cppnet_base->OnRead(shared_from_this(), len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WinRWSocket::Send(uint32_t len) {
|
||||
auto cppnet_base = _cppnet_base.lock();
|
||||
if (!cppnet_base) {
|
||||
return false;
|
||||
}
|
||||
if (len > 0) {
|
||||
_write_buffer->MoveReadPt(len);
|
||||
cppnet_base->OnWrite(shared_from_this(), len);
|
||||
}
|
||||
|
||||
auto actions = GetEventActions();
|
||||
if (actions && _write_buffer->GetCanReadLength() > 0) {
|
||||
if (actions->AddSendEvent(_event)) {
|
||||
Incref();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,15 +6,20 @@
|
||||
#ifndef CPPNET_SOCKET_WIN_READ_WRITE_SOCKET
|
||||
#define CPPNET_SOCKET_WIN_READ_WRITE_SOCKET
|
||||
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <unordered_set>
|
||||
#include "../rw_socket.h"
|
||||
|
||||
namespace cppnet {
|
||||
|
||||
class Event;
|
||||
class AlloterWrap;
|
||||
class WinRWSocket:
|
||||
public RWSocket {
|
||||
|
||||
public:
|
||||
WinRWSocket();
|
||||
WinRWSocket(std::shared_ptr<AlloterWrap> alloter);
|
||||
WinRWSocket(uint64_t sock, std::shared_ptr<AlloterWrap> alloter);
|
||||
virtual ~WinRWSocket();
|
||||
@@ -24,23 +29,30 @@ public:
|
||||
virtual void Connect(const std::string& ip, uint16_t port);
|
||||
virtual void Disconnect();
|
||||
|
||||
virtual void OnRead(uint32_t len = 0);
|
||||
virtual void OnWrite(uint32_t len = 0);
|
||||
virtual void OnDisConnect(uint16_t err);
|
||||
virtual void OnRead(Event* event, uint32_t len = 0);
|
||||
virtual void OnWrite(Event* event, uint32_t len = 0);
|
||||
virtual void OnDisConnect(Event* event, uint16_t err);
|
||||
|
||||
void Incref() { _ref_count.fetch_add(1); }
|
||||
bool Decref(uint16_t err = CEC_CLOSED);
|
||||
virtual void SetShutdown() { _shutdown = true; }
|
||||
virtual bool IsShutdown() { return _shutdown; }
|
||||
|
||||
void SetShutdown() { _shutdown = true; }
|
||||
bool IsShutdown() { return _shutdown; }
|
||||
virtual std::shared_ptr<BufferQueue> GetReadBuffer();
|
||||
|
||||
private:
|
||||
bool Recv(uint32_t len);
|
||||
bool Send(uint32_t len = 0);
|
||||
void AddEvent(Event* event);
|
||||
void RemvoeEvent(Event* event);
|
||||
bool EventEmpty();
|
||||
|
||||
private:
|
||||
std::atomic_int16_t _ref_count;
|
||||
std::atomic_bool _shutdown;
|
||||
std::atomic_bool _is_reading;
|
||||
|
||||
// only need read cache. data to send is saved to event buffer.
|
||||
std::shared_ptr<BufferQueue> _read_buffer;
|
||||
|
||||
// all event
|
||||
std::mutex _event_mutex;
|
||||
std::unordered_set<Event*> _event_set;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
2
makefile
2
makefile
@@ -35,7 +35,7 @@ INCLUDES = -I.
|
||||
|
||||
#debug
|
||||
|
||||
#CCFLAGS = -lpthread -fPIC -m64 -g -std=c++11 -lstdc++ -pipe
|
||||
#CCFLAGS = -lpthread -fPIC -m64 -g -std=c++11 -lstdc++ -pipe -Wall
|
||||
|
||||
CCFLAGS = -lpthread -fPIC -m64 -O2 -std=c++11 -lstdc++ -pipe
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{25B16872-AB29-4667-B345-04979B149DD7}</ProjectGuid>
|
||||
<RootNamespace>EchoClient</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5}</ProjectGuid>
|
||||
<RootNamespace>EchoServer</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}</ProjectGuid>
|
||||
<RootNamespace>http</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
@@ -53,13 +53,13 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -68,7 +68,7 @@ void DisConnectionFunc(const cppnet::Handle& , uint32_t ) {
|
||||
|
||||
int main() {
|
||||
cppnet::CppNet net;
|
||||
net.Init(4);
|
||||
net.Init(8);
|
||||
|
||||
HttpServer server;
|
||||
server.SetHttpCallback(OnRequest);
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{B50A1C13-79C7-4773-B650-78BF0FF6C285}</ProjectGuid>
|
||||
<RootNamespace>Client</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{51DD936B-DB06-4592-9A5E-887F6B920302}</ProjectGuid>
|
||||
<RootNamespace>Server</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{D87B16E8-470A-409A-B271-950E9D320F1D}</ProjectGuid>
|
||||
<RootNamespace>RpcClient</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{D75D2954-2E9C-4C5A-A71F-553139FF03E1}</ProjectGuid>
|
||||
<RootNamespace>RpcServer</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{71A74A81-4259-4EBA-A6A7-6D5D7ECDA358}</ProjectGuid>
|
||||
<RootNamespace>SendFileCli</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{9ABE1F25-8A2E-4D68-ADC2-EA07B919A311}</ProjectGuid>
|
||||
<RootNamespace>SendFileSer</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{8C4CE7D6-9C74-4FEC-BEFC-EC99EA8A0D0A}</ProjectGuid>
|
||||
<RootNamespace>CppNetCli</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{6FE7E867-AD3A-4681-A0DA-2A5E0DB16887}</ProjectGuid>
|
||||
<RootNamespace>CppNetSev</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
|
||||
Reference in New Issue
Block a user