diff --git a/CppNet.vcxproj b/CppNet.vcxproj
index bdde085..8117de2 100644
--- a/CppNet.vcxproj
+++ b/CppNet.vcxproj
@@ -123,9 +123,10 @@
-
+
+
@@ -151,8 +152,8 @@
-
+
diff --git a/CppNet.vcxproj.filters b/CppNet.vcxproj.filters
index f53737e..1fd3a92 100644
--- a/CppNet.vcxproj.filters
+++ b/CppNet.vcxproj.filters
@@ -66,7 +66,10 @@
net\linux
-
+
+ net
+
+
net\linux
@@ -140,8 +143,8 @@
net\linux
-
- net\linux
+
+ net
\ No newline at end of file
diff --git a/CppNet_client.vcxproj b/CppNet_client.vcxproj
index 46581af..50fe4dc 100644
--- a/CppNet_client.vcxproj
+++ b/CppNet_client.vcxproj
@@ -29,6 +29,7 @@
+
@@ -55,6 +56,7 @@
+
@@ -133,6 +135,7 @@
260046848
+ /FS %(AdditionalOptions)
diff --git a/CppNet_client.vcxproj.filters b/CppNet_client.vcxproj.filters
index aa6a6ba..c4020d2 100644
--- a/CppNet_client.vcxproj.filters
+++ b/CppNet_client.vcxproj.filters
@@ -69,6 +69,9 @@
net\linux
+
+ net
+
@@ -140,5 +143,8 @@
net\linux
+
+ net
+
\ No newline at end of file
diff --git a/LinuxCppNet.vcxproj b/LinuxCppNet.vcxproj
index 209e5c6..770005d 100644
--- a/LinuxCppNet.vcxproj
+++ b/LinuxCppNet.vcxproj
@@ -71,6 +71,7 @@
+
@@ -93,6 +94,7 @@
+
diff --git a/LinuxCppNet.vcxproj.filters b/LinuxCppNet.vcxproj.filters
index f2667a8..03e1fe7 100644
--- a/LinuxCppNet.vcxproj.filters
+++ b/LinuxCppNet.vcxproj.filters
@@ -54,6 +54,9 @@
net\linux
+
+ net
+
@@ -119,5 +122,8 @@
net\linux
+
+ net
+
\ No newline at end of file
diff --git a/base/Buffer.cpp b/base/Buffer.cpp
index c758668..2146b15 100644
--- a/base/Buffer.cpp
+++ b/base/Buffer.cpp
@@ -4,7 +4,7 @@
#include "LoopBuffer.h"
#include "Log.h"
-CBuffer::CBuffer(std::shared_ptr& pool) :
+CBuffer::CBuffer(std::shared_ptr& pool) :
_pool(pool),
_buffer_num(0),
_buffer_start(nullptr),
diff --git a/base/Buffer.h b/base/Buffer.h
index 3f514c8..91f3f9d 100644
--- a/base/Buffer.h
+++ b/base/Buffer.h
@@ -12,11 +12,11 @@
static const int __max_node_size = 12;
class CLoopBuffer;
-class CMemaryPool;
+class CMemoryPool;
class CBuffer
{
public:
- CBuffer(std::shared_ptr& pool);
+ CBuffer(std::shared_ptr& pool);
~CBuffer();
int Read(char* res, int len);
@@ -58,6 +58,6 @@ public:
CLoopBuffer* _buffer_write;
std::mutex _mutex;
- std::shared_ptr _pool;
+ std::shared_ptr _pool;
};
#endif
\ No newline at end of file
diff --git a/base/Log.cpp b/base/Log.cpp
index cd2af5f..8e7e531 100644
--- a/base/Log.cpp
+++ b/base/Log.cpp
@@ -22,7 +22,9 @@ void CLog::Run() {
_log_file << log << std::endl;
}
_pool.PoolLargeFree(log);
- }
+ } else {
+ break;
+ }
}
}
diff --git a/base/Log.h b/base/Log.h
index d989c1f..299477c 100644
--- a/base/Log.h
+++ b/base/Log.h
@@ -46,13 +46,13 @@ private:
std::fstream _log_file;
int _log_level;
int _cur_date;
- CMemaryPool _pool;
+ CMemoryPool _pool;
};
-#define LOG_DEBUG(log, ...) CLog::Instance().LogDebug(__FILE__, __LINE__, log, __VA_ARGS__);
-#define LOG_INFO(log, ...) CLog::Instance().LogInfo(__FILE__, __LINE__, log, __VA_ARGS__);
-#define LOG_WARN(log, ...) CLog::Instance().LogWarn(__FILE__, __LINE__, log, __VA_ARGS__);
-#define LOG_ERROR(log, ...) CLog::Instance().LogError(__FILE__, __LINE__, log, __VA_ARGS__);
-#define LOG_FATAL(log, ...) CLog::Instance().LogFatal(__FILE__, __LINE__, log, __VA_ARGS__);
+#define LOG_DEBUG(log, ...) CLog::Instance().LogDebug(__FILE__, __LINE__, log, ##__VA_ARGS__);
+#define LOG_INFO(log, ...) CLog::Instance().LogInfo(__FILE__, __LINE__, log, ##__VA_ARGS__);
+#define LOG_WARN(log, ...) CLog::Instance().LogWarn(__FILE__, __LINE__, log, ##__VA_ARGS__);
+#define LOG_ERROR(log, ...) CLog::Instance().LogError(__FILE__, __LINE__, log, ##__VA_ARGS__);
+#define LOG_FATAL(log, ...) CLog::Instance().LogFatal(__FILE__, __LINE__, log, ##__VA_ARGS__);
#endif
\ No newline at end of file
diff --git a/base/LoopBuffer.cpp b/base/LoopBuffer.cpp
index 8cd1aba..f3f0e48 100644
--- a/base/LoopBuffer.cpp
+++ b/base/LoopBuffer.cpp
@@ -5,7 +5,7 @@
#include "MemaryPool.h"
-CLoopBuffer::CLoopBuffer(std::shared_ptr& pool, int index) :
+CLoopBuffer::CLoopBuffer(std::shared_ptr& pool, int index) :
_can_read(false),
_next(nullptr),
_pool(pool),
@@ -18,7 +18,7 @@ CLoopBuffer::CLoopBuffer(std::shared_ptr& pool, int index) :
}
-CLoopBuffer::CLoopBuffer(std::shared_ptr& pool, int size, int index) :
+CLoopBuffer::CLoopBuffer(std::shared_ptr& pool, int size, int index) :
_can_read(false),
_next(nullptr),
_pool(pool),
diff --git a/base/LoopBuffer.h b/base/LoopBuffer.h
index 04f0e9c..6905558 100644
--- a/base/LoopBuffer.h
+++ b/base/LoopBuffer.h
@@ -2,12 +2,12 @@
#define HEADER_LOOPBUFFER
#include
#include
-class CMemaryPool;
+class CMemoryPool;
class CLoopBuffer {
public:
//maybe throw exception
- explicit CLoopBuffer(std::shared_ptr& pool, int index);
- explicit CLoopBuffer(std::shared_ptr& pool, int size, int index);
+ explicit CLoopBuffer(std::shared_ptr& pool, int index);
+ explicit CLoopBuffer(std::shared_ptr& pool, int size, int index);
~CLoopBuffer();
int Read(char* res, int len);
@@ -59,7 +59,7 @@ private:
std::mutex _mutex;
CLoopBuffer* _next; //point to next node
- std::shared_ptr _pool;
+ std::shared_ptr _pool;
};
#endif
diff --git a/base/MemaryPool.cpp b/base/MemaryPool.cpp
index 311d251..19f9b4d 100644
--- a/base/MemaryPool.cpp
+++ b/base/MemaryPool.cpp
@@ -5,7 +5,7 @@
#include
using namespace std;
-CMemaryPool::CMemaryPool() {
+CMemoryPool::CMemoryPool() {
for (int i = 0; i < __number_of_free_lists; i++) {
_free_list[i] = nullptr;
}
@@ -14,7 +14,7 @@ CMemaryPool::CMemaryPool() {
_create_thread_id = std::this_thread::get_id();
}
-CMemaryPool::CMemaryPool(const int large_sz, const int add_num) : _large_size(RoundUp(large_sz)), _number_large_add_nodes(add_num){
+CMemoryPool::CMemoryPool(const int large_sz, const int add_num) : _large_size(RoundUp(large_sz)), _number_large_add_nodes(add_num){
for (int i = 0; i < __number_of_free_lists; i++) {
_free_list[i] = nullptr;
}
@@ -23,7 +23,7 @@ CMemaryPool::CMemaryPool(const int large_sz, const int add_num) : _large_size(Ro
_create_thread_id = std::this_thread::get_id();
}
-CMemaryPool::~CMemaryPool() {
+CMemoryPool::~CMemoryPool() {
//assert(_create_thread_id == std::this_thread::get_id());
for (auto iter = _malloc_vec.begin(); iter != _malloc_vec.end(); ++iter) {
if (*iter) {
@@ -32,15 +32,15 @@ CMemaryPool::~CMemaryPool() {
}
}
-std::thread::id CMemaryPool::GetCreateThreadId() {
+std::thread::id CMemoryPool::GetCreateThreadId() {
return _create_thread_id;
}
-int CMemaryPool::GetLargeSize() const {
+int CMemoryPool::GetLargeSize() const {
return _large_size;
}
-void* CMemaryPool::ReFill(int size, int num, bool is_large) {
+void* CMemoryPool::ReFill(int size, int num, bool is_large) {
int nums = num;
char* chunk = nullptr;
@@ -98,7 +98,7 @@ void* CMemaryPool::ReFill(int size, int num, bool is_large) {
return res;
}
-void* CMemaryPool::ChunkAlloc(int size, int& nums, bool is_large) {
+void* CMemoryPool::ChunkAlloc(int size, int& nums, bool is_large) {
char* res;
int need_bytes = size * nums;
int left_bytes = _pool_end - _pool_start;
diff --git a/base/MemaryPool.h b/base/MemaryPool.h
index 373fcf4..9b01b61 100644
--- a/base/MemaryPool.h
+++ b/base/MemaryPool.h
@@ -15,12 +15,12 @@ static const int __max_bytes = 256;
static const int __number_of_free_lists = __max_bytes / __align;
static const int __number_add_nodes = 20;
-class CMemaryPool {
+class CMemoryPool {
public:
- CMemaryPool();
+ CMemoryPool();
//bulk memory size. everytime add nodes num
- CMemaryPool(const int large_sz, const int add_num);
- ~CMemaryPool();
+ CMemoryPool(const int large_sz, const int add_num);
+ ~CMemoryPool();
//for object. invocation of constructors and destructors
template
@@ -82,7 +82,7 @@ private:
};
template
-T* CMemaryPool::PoolNew(Args&&... args) {
+T* CMemoryPool::PoolNew(Args&&... args) {
int sz = sizeof(T);
if (sz > __max_bytes) {
void* bytes = malloc(sz);
@@ -104,7 +104,7 @@ T* CMemaryPool::PoolNew(Args&&... args) {
}
template
-void CMemaryPool::PoolDelete(T* &c) {
+void CMemoryPool::PoolDelete(T* &c) {
if (!c) {
return;
}
@@ -127,7 +127,7 @@ void CMemaryPool::PoolDelete(T* &c) {
}
template
-T* CMemaryPool::PoolMalloc(int sz) {
+T* CMemoryPool::PoolMalloc(int sz) {
if (sz > __max_bytes) {
void* bytes = malloc(sz);
memset(bytes, 0, sz);
@@ -149,7 +149,7 @@ T* CMemaryPool::PoolMalloc(int sz) {
}
template
-void CMemaryPool::PoolFree(T* &m, int len) {
+void CMemoryPool::PoolFree(T* &m, int len) {
if (!m) {
return;
}
@@ -170,7 +170,7 @@ void CMemaryPool::PoolFree(T* &m, int len) {
}
template
-T* CMemaryPool::PoolLargeMalloc() {
+T* CMemoryPool::PoolLargeMalloc() {
if (_number_large_add_nodes == 0 || _large_size == 0) {
throw std::exception(std::logic_error("Large block of memory is not set!"));
return nullptr;
@@ -195,7 +195,7 @@ T* CMemaryPool::PoolLargeMalloc() {
}
template
-void CMemaryPool::PoolLargeFree(T* &m) {
+void CMemoryPool::PoolLargeFree(T* &m) {
if (!m) {
return;
}
@@ -214,7 +214,7 @@ void CMemaryPool::PoolLargeFree(T* &m) {
}
template
-T* CMemaryPool::PoolLargeMalloc(int size, int& res) {
+T* CMemoryPool::PoolLargeMalloc(int size, int& res) {
if (_number_large_add_nodes == 0 || _large_size == 0) {
throw std::exception(std::logic_error("Large block of memory is not set!"));
return nullptr;
@@ -241,7 +241,7 @@ T* CMemaryPool::PoolLargeMalloc(int size, int& res) {
}
template
-void CMemaryPool::PoolLargeFree(T* &m, int size) {
+void CMemoryPool::PoolLargeFree(T* &m, int size) {
if (!m) {
return;
}
diff --git a/base/PoolSharedPtr.h b/base/PoolSharedPtr.h
index c56ab2e..74b2d14 100644
--- a/base/PoolSharedPtr.h
+++ b/base/PoolSharedPtr.h
@@ -2,6 +2,7 @@
#define HEADER_CPOOLSHAREDPTR
#include
#include
+#include
#include "MemaryPool.h"
@@ -68,7 +69,7 @@ private:
};
template
-inline void EnableShared(T *ptr, CRefCount *ref_ptr, CMemaryPool* pool, int size = 0, MemoryType type = TYPE_NEW);
+inline void EnableShared(T *ptr, CRefCount *ref_ptr, CMemoryPool* pool, int size = 0, MemoryType type = TYPE_NEW);
// base class for CMemSharePtr and CMemWeakPtr
template
@@ -80,7 +81,7 @@ public:
CBasePtr() noexcept : _ptr(nullptr), _ref_count(nullptr), _pool(nullptr) {
EnableShared(_ptr, _ref_count, _pool);
}
- CBasePtr(T* ptr, CRefCount* ref, CMemaryPool* pool, MemoryType type, int large_size = 0) noexcept : _ptr(ptr), _ref_count(ref), _pool(pool), _memory_type(type), _malloc_size(large_size) {
+ CBasePtr(T* ptr, CRefCount* ref, CMemoryPool* pool, MemoryType type, int large_size = 0) noexcept : _ptr(ptr), _ref_count(ref), _pool(pool), _memory_type(type), _malloc_size(large_size) {
EnableShared(_ptr, _ref_count, _pool, _malloc_size, _memory_type);
}
@@ -164,7 +165,7 @@ public:
}
// release resource and take _Other_ptr through _Other_rep
- void Reset(T *other_ptr, CRefCount * other_rep, CMemaryPool* pool) {
+ void Reset(T *other_ptr, CRefCount * other_rep, CMemoryPool* pool) {
_Reset0(other_ptr, other_rep, pool);
}
@@ -178,11 +179,11 @@ public:
Resetw(other._ptr, other._ref_count, other._pool);
}
- void Resetw(T *other_ptr, CRefCount *other_rep, CMemaryPool* pool) {
+ void Resetw(T *other_ptr, CRefCount *other_rep, CMemoryPool* pool) {
_Resetw0(other_ptr, other_rep, pool);
}
- void Resetw(T *other_ptr, CRefCount *other_rep, CMemaryPool* pool, int size, MemoryType type) {
+ void Resetw(T *other_ptr, CRefCount *other_rep, CMemoryPool* pool, int size, MemoryType type) {
_Resetw0(other_ptr, other_rep, pool, type, size);
}
@@ -208,7 +209,7 @@ public:
}
// release resource and take new resource
- void _Reset0(T *other_ptr, CRefCount *other_rep, CMemaryPool* pool, int size = 0, MemoryType type = TYPE_NEW) {
+ void _Reset0(T *other_ptr, CRefCount *other_rep, CMemoryPool* pool, int size = 0, MemoryType type = TYPE_NEW) {
if (other_rep) {
other_rep->IncrefUse();
}
@@ -259,7 +260,7 @@ public:
}
// release resource and take new resource
- void _Resetw0(T *other_ptr, CRefCount *other_rep, CMemaryPool* pool) {
+ void _Resetw0(T *other_ptr, CRefCount *other_rep, CMemoryPool* pool) {
if (other_rep) {
other_rep->IncrefWeak();
}
@@ -274,7 +275,7 @@ public:
}
// release resource and take new resource
- void _Resetw0(T *other_ptr, CRefCount *other_rep, CMemaryPool* pool, MemoryType type, int size) {
+ void _Resetw0(T *other_ptr, CRefCount *other_rep, CMemoryPool* pool, MemoryType type, int size) {
if (other_rep) {
other_rep->IncrefWeak();
}
@@ -321,7 +322,7 @@ public:
protected:
T *_ptr;
CRefCount *_ref_count;
- CMemaryPool *_pool;
+ CMemoryPool *_pool;
int _malloc_size;
MemoryType _memory_type;
@@ -336,7 +337,8 @@ public:
typedef CBasePtr _BasePtr;
// construct
CMemSharePtr() noexcept : _BasePtr() {}
- CMemSharePtr(T* ptr, CRefCount* ref, CMemaryPool* pool, MemoryType type, int large_size = 0) noexcept : _BasePtr(ptr, ref, pool, type, large_size) {}
+ CMemSharePtr(nullptr_t) noexcept : _BasePtr() {}
+ CMemSharePtr(T* ptr, CRefCount* ref, CMemoryPool* pool, MemoryType type, int large_size = 0) noexcept : _BasePtr(ptr, ref, pool, type, large_size) {}
CMemSharePtr(const _BasePtr& r) : _BasePtr(r) {}
CMemSharePtr(_BasePtr&& r) : _BasePtr(r) {}
@@ -457,13 +459,13 @@ protected:
private:
template
- friend void DoEnable(T1 *ptr, CEnableSharedFromThis *es, CRefCount *ref_ptr, CMemaryPool* pool = 0, int size = 0, MemoryType type = TYPE_NEW);
+ friend void DoEnable(T1 *ptr, CEnableSharedFromThis *es, CRefCount *ref_ptr, CMemoryPool* pool, int size, MemoryType type);
CMemWeakPtr _weak_ptr;
};
// reset internal weak pointer
template
-inline void DoEnable(T1 *ptr, CEnableSharedFromThis *es, CRefCount *ref_ptr, CMemaryPool* pool, int size, MemoryType type) {
+inline void DoEnable(T1 *ptr, CEnableSharedFromThis *es, CRefCount *ref_ptr, CMemoryPool* pool = nullptr, int size = 0, MemoryType type = TYPE_NEW) {
es->_weak_ptr.Resetw(ptr, ref_ptr, pool, size, type);
}
@@ -477,7 +479,7 @@ struct has_member_weak_ptr {
};
template
-inline void EnableShared(T *ptr, CRefCount *ref_ptr, CMemaryPool* pool, int size, MemoryType type) {
+inline void EnableShared(T *ptr, CRefCount *ref_ptr, CMemoryPool* pool, int size, MemoryType type) {
if (ptr) {
if (has_member_weak_ptr::value > 0) {
DoEnable(ptr, (CEnableSharedFromThis*)ptr, ref_ptr, pool, size, type);
@@ -486,28 +488,28 @@ inline void EnableShared(T *ptr, CRefCount *ref_ptr, CMemaryPool* pool, int size
}
template
-CMemSharePtr MakeNewSharedPtr(CMemaryPool* pool, Args&&... args) {
+CMemSharePtr MakeNewSharedPtr(CMemoryPool* pool, Args&&... args) {
T* o = pool->PoolNew(std::forward(args)...);
CRefCount* ref = pool->PoolNew();
return CMemSharePtr(o, ref, pool, TYPE_NEW);
}
template
-CMemSharePtr MakeMallocSharedPtr(CMemaryPool* pool, int size) {
+CMemSharePtr MakeMallocSharedPtr(CMemoryPool* pool, int size) {
T* o = (T*)pool->PoolMalloc(size);
CRefCount* ref = pool->PoolNew();
return CMemSharePtr(o, ref, pool, TYPE_MALLOC, size);
}
template
-CMemSharePtr MakeLargeSharedPtr(CMemaryPool* pool) {
+CMemSharePtr MakeLargeSharedPtr(CMemoryPool* pool) {
T* o = pool->PoolLargeMalloc();
CRefCount* ref = pool->PoolNew();
return CMemSharePtr(o, ref, pool, TYPE_LARGE);
}
template
-CMemSharePtr MakeLargeSharedPtr(CMemaryPool* pool, int size) {
+CMemSharePtr MakeLargeSharedPtr(CMemoryPool* pool, int size) {
T* o = pool->PoolLargeMalloc(size);
CRefCount* ref = pool->PoolNew();
return CMemSharePtr(o, ref, pool, TYPE_LARGE_WITH_SIZE, size);
diff --git a/base/TimeTool.h b/base/TimeTool.h
index 7c7f317..a5535c3 100644
--- a/base/TimeTool.h
+++ b/base/TimeTool.h
@@ -3,7 +3,7 @@
#include
-class CMemaryPool;
+class CMemoryPool;
class CTimeTool
{
public:
diff --git a/net/AcceptSocket.h b/net/AcceptSocket.h
index 3c85a33..add2543 100644
--- a/net/AcceptSocket.h
+++ b/net/AcceptSocket.h
@@ -9,9 +9,9 @@
class CEventHandler;
class CAcceptEventHandler;
-class CMemaryPool;
+class CMemoryPool;
-class CAcceptSocket : public CSocketBase {
+class CAcceptSocket : public CSocketBase, public CEnableSharedFromThis {
public:
CAcceptSocket(std::shared_ptr& event_actions);
~CAcceptSocket();
@@ -20,14 +20,9 @@ public:
bool Listen(unsigned int listen_size);
-#ifndef __linux__
- void SyncAccept(const std::function&, int error)>& accept_back = nullptr,
- const std::function&, int error)>& read_back = nullptr);
- void SetReadCallBack(const std::function&, int error)>& call_back);
-#else
- void SyncAccept(const std::function&, int error)>& call_back = nullptr);
-#endif
+ void SyncAccept();
+ void SetReadCallBack(const std::function&, int error)>& call_back);
void SetAcceptCallBack(const std::function&, int error)>& call_back);
public:
diff --git a/net/EventActions.h b/net/EventActions.h
index 5be9032..1dd69c7 100644
--- a/net/EventActions.h
+++ b/net/EventActions.h
@@ -10,14 +10,18 @@ public:
virtual ~CEventActions() {}
//param is net io thread num, default cpu number
- virtual bool Init(int thread_num = 0) = 0;
+ virtual bool Init() = 0;
virtual bool Dealloc() = 0;
virtual bool AddTimerEvent(unsigned int interval, int event_flag, CMemSharePtr& event) = 0;
virtual bool AddSendEvent(CMemSharePtr& event) = 0;
virtual bool AddRecvEvent(CMemSharePtr& event) = 0;
virtual bool AddAcceptEvent(CMemSharePtr& event) = 0;
+#ifndef __linux__
+ virtual bool AddConnection(CMemSharePtr& event, const std::string& ip, short port, char* buf, int buf_len) = 0;
+#else
virtual bool AddConnection(CMemSharePtr& event, const std::string& ip, short port) = 0;
+#endif
virtual bool AddDisconnection(CMemSharePtr& event) = 0;
virtual bool DelEvent(CMemSharePtr& event) = 0;
virtual void ProcessEvent() = 0;
diff --git a/net/EventHandler.h b/net/EventHandler.h
index 0b8cc06..ec46468 100644
--- a/net/EventHandler.h
+++ b/net/EventHandler.h
@@ -13,13 +13,13 @@ enum EVENT_FLAG {
EVENT_ACCEPT = 0x0004, //accept event
EVENT_TIMER = 0x0008, //timer event
EVENT_CONNECT = 0x0010, //connect event
- EVENT_DISCONNECT = 0x0020 //disconnect event
+ EVENT_DISCONNECT = 0x0020 //disconnect event
};
enum EVENT_ERROR {
- EVENT_ERROR_NO = 0,
- EVENT_ERROR_TIMEOUT = 1,
- EVENT_ERROR_CLOSED = 2
+ EVENT_ERROR_NO = 0x0100,
+ EVENT_ERROR_TIMEOUT = 0x0200,
+ EVENT_ERROR_CLOSED = 0x0400
};
class Cevent {
@@ -45,7 +45,7 @@ class CAcceptEventHandler : public Cevent {
public:
CMemSharePtr _client_socket;
- CAcceptSocket* _accept_socket = nullptr;
+ CMemSharePtr _accept_socket = nullptr;
std::function&, int error)> _call_back;
};
#endif
\ No newline at end of file
diff --git a/net/NetObject.cpp b/net/NetObject.cpp
new file mode 100644
index 0000000..2fc44ab
--- /dev/null
+++ b/net/NetObject.cpp
@@ -0,0 +1,247 @@
+#include
+
+#include "NetObject.h"
+#include "EventActions.h"
+#include "OSInfo.h"
+#include "Log.h"
+#include "Runnable.h"
+#ifdef __linux__
+#include "CEpoll.h"
+#include "LinuxFunc.h"
+#else
+#include "IOCP.h"
+#endif
+
+CNetObject::CNetObject() {
+
+}
+
+CNetObject::~CNetObject() {
+
+}
+
+void CNetObject::Init(int thread_num) {
+#ifndef __linux__
+ InitScoket();
+#else
+ SetCoreFileUnlimit();
+#endif // __linux__
+
+ int cpus = GetCpuNum();
+ if (thread_num == 0 || thread_num > cpus * 2) {
+ thread_num = cpus;
+ }
+ for (int i = 0; i < thread_num; i++) {
+#ifdef __linux__
+ std::shared_ptr event_actions(new CEpoll);
+#else
+ static std::shared_ptr event_actions(new CIOCP);
+ //std::shared_ptr event_actions(new CIOCP);
+#endif
+ event_actions->Init();
+ std::shared_ptr thd(new std::thread(std::bind(&CEventActions::ProcessEvent, event_actions)));
+ _actions_map[thd->get_id()] = event_actions;
+ _thread_vec.push_back(thd);
+ }
+}
+
+void CNetObject::Dealloc() {
+ for (auto iter = _actions_map.begin(); iter != _actions_map.end(); ++iter) {
+ iter->second->Dealloc();
+ }
+ _actions_map.clear();
+#ifndef __linux__
+ DeallocSocket();
+#endif // __linux__
+}
+
+void CNetObject::MainLoop() {
+ for (;;) {
+ CRunnable::Sleep(50000);
+ }
+}
+
+void CNetObject::Join() {
+ for (size_t i = 0; i < _thread_vec.size(); ++i) {
+ _thread_vec[i]->join();
+ }
+}
+
+void CNetObject::SetReadCallback(const call_back& func) {
+ _read_call_back = func;
+}
+
+void CNetObject::SetWriteCallback(const call_back& func) {
+ _write_call_back = func;
+}
+
+void CNetObject::SetDisconnectionCallback(const call_back& func) {
+ _disconnection_call_back = func;
+}
+
+void CNetObject::SetAcceptCallback(const call_back& func) {
+ _accept_call_back = func;
+}
+
+bool CNetObject::ListenAndAccept(int port, std::string ip) {
+ if (!_accept_call_back) {
+ LOG_WARN("accept call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return false;
+ }
+
+ if (!_read_call_back) {
+ LOG_WARN("read call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return false;
+ }
+
+ if (_actions_map.size() <= 0) {
+ LOG_WARN("CNetObject obj is not inited!, port : %d, ip : %s ", port, ip.c_str());
+ return false;
+ }
+
+ for (auto iter = _actions_map.begin(); iter != _actions_map.end(); ++iter) {
+ CMemSharePtr accept_socket = MakeNewSharedPtr(&_pool, _actions_map.begin()->second);
+ if (!accept_socket->Bind(port, ip)) {
+ return false;
+ }
+
+ if (!accept_socket->Listen(100)) {
+ return false;
+ }
+
+ accept_socket->SetAcceptCallBack(std::bind(&CNetObject::_AcceptFunction, this, std::placeholders::_1, std::placeholders::_2));
+ accept_socket->SetReadCallBack(std::bind(&CNetObject::_ReadFunction, this, std::placeholders::_1, std::placeholders::_2));
+
+ accept_socket->SyncAccept();
+ _accept_socket[accept_socket->GetSocket()] = accept_socket;
+#ifndef __linux__
+ break;
+#endif
+ }
+ return true;
+}
+
+void CNetObject::SetConnectionCallback(const call_back& func) {
+ _connection_call_back = func;
+}
+
+CMemSharePtr CNetObject::Connection(int port, std::string ip, char* buf, int buf_len) {
+ if (!_connection_call_back) {
+ LOG_WARN("connection call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return nullptr;
+ }
+ if (!_write_call_back) {
+ LOG_WARN("read call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return nullptr;
+ }
+
+ auto actions = RandomGetActions();
+ CMemSharePtr sock = MakeNewSharedPtr(&_pool, actions);
+ sock->SetWriteCallBack(std::bind(&CNetObject::_WriteFunction, this, std::placeholders::_1, std::placeholders::_2));
+
+#ifndef __linux__
+ sock->SetReadCallBack(std::bind(&CNetObject::_ReadFunction, this, std::placeholders::_1, std::placeholders::_2));
+ sock->SyncConnection(ip, port, buf, buf_len);
+#else
+ auto func = [buf, buf_len, sock, this](CMemSharePtr& event, int err) {
+ if (err = EVENT_ERROR_NO) {
+ sock->SyncWrite(buf, buf_len);
+ }
+ sock->SetReadCallBack(std::bind(&CNetObject::_ReadFunction, this, std::placeholders::_1, std::placeholders::_2));
+ };
+ sock->SetReadCallBack(func);
+#endif
+ return sock;
+}
+
+CMemSharePtr CNetObject::Connection(int port, std::string ip) {
+ if (!_connection_call_back) {
+ LOG_WARN("connection call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return nullptr;
+ }
+ if (!_write_call_back) {
+ LOG_WARN("read call back function is null!, port : %d, ip : %s ", port, ip.c_str());
+ return nullptr;
+ }
+
+ auto actions = RandomGetActions();
+ CMemSharePtr sock = MakeNewSharedPtr(&_pool, actions);
+ sock->SetWriteCallBack(std::bind(&CNetObject::_WriteFunction, this, std::placeholders::_1, std::placeholders::_2));
+ sock->SetReadCallBack(std::bind(&CNetObject::_ReadFunction, this, std::placeholders::_1, std::placeholders::_2));
+
+#ifndef __linux__
+ sock->SyncConnection(ip, port, "", 0);
+#else
+ sock->SyncConnection(ip, port);
+#endif
+ return sock;
+}
+
+void CNetObject::_AcceptFunction(CMemSharePtr& event, int err) {
+ if (!event) {
+ return;
+ }
+
+ auto socket_ptr = event->_client_socket;
+ if (err & EVENT_ERROR_NO) {
+ socket_ptr->SetReadCallBack(std::bind(&CNetObject::_ReadFunction, this, std::placeholders::_1, std::placeholders::_2));
+ socket_ptr->SetWriteCallBack(std::bind(&CNetObject::_WriteFunction, this, std::placeholders::_1, std::placeholders::_2));
+ if (_accept_call_back) {
+ _accept_call_back(socket_ptr, err);
+ }
+ static int num = 0;
+ num++;
+ LOG_ERROR("get client num : %d", num);
+
+ std::unique_lock lock(_mutex);
+ _socket_map[event->_client_socket->GetSocket()] = event->_client_socket;
+ }
+}
+
+void CNetObject::_ReadFunction(CMemSharePtr& event, int err) {
+ if (!event) {
+ return;
+ }
+ auto socket_ptr = event->_client_socket.Lock();
+ if (err & EVENT_READ && _read_call_back) {
+ err &= ~EVENT_READ;
+ _read_call_back(socket_ptr, err);
+ if (err == EVENT_ERROR_CLOSED) {
+ std::unique_lock lock(_mutex);
+ _socket_map.erase(socket_ptr->GetSocket());
+ }
+
+ } else if (err & EVENT_CONNECT && _connection_call_back) {
+ err &= ~EVENT_CONNECT;
+ _connection_call_back(socket_ptr, err);
+ } else if (err & EVENT_DISCONNECT && _disconnection_call_back) {
+ err &= ~EVENT_DISCONNECT;
+ _disconnection_call_back(socket_ptr, err);
+ }
+}
+
+void CNetObject::_WriteFunction(CMemSharePtr& event, int err) {
+ if (!event) {
+ return;
+ }
+ auto socket_ptr = event->_client_socket.Lock();
+ if (err & EVENT_WRITE && _write_call_back) {
+ err &= ~EVENT_WRITE;
+ _write_call_back(socket_ptr, err);
+ if (err == EVENT_ERROR_CLOSED) {
+ std::unique_lock lock(_mutex);
+ _socket_map.erase(socket_ptr->GetSocket());
+ }
+ }
+}
+
+std::shared_ptr& CNetObject::RandomGetActions() {
+ std::random_device rd;
+ std::mt19937 mt(rd());
+ int index = mt() % int(_actions_map.size());
+ auto iter = _actions_map.begin();
+ for (int i = 0; i < index; i++) {
+ iter++;
+ }
+ return iter->second;
+}
\ No newline at end of file
diff --git a/net/NetObject.h b/net/NetObject.h
new file mode 100644
index 0000000..4b956fe
--- /dev/null
+++ b/net/NetObject.h
@@ -0,0 +1,63 @@
+#ifndef HEADER_CNETOBJECT
+#define HEADER_CNETOBJECT
+
+#include