发送cell基类
#ifndef _CELL_TASK_H_
#include<thread>
#include<mutex>
#include<list>//快速增加删除任务
#include<functional>
#include<iostream>
//任务类型-基类
class CellTask
{
public:
CellTask()
{
}
virtual ~CellTask()
{
}
virtual void doTask()
{
}
};
//执行任务类
class CellTaskServer
{
public:
CellTaskServer()
{
}
virtual ~CellTaskServer()
{
}
void addTask(CellTask* task)
{
std::lock_guard<std::mutex> addmutex(_mutex);
_taskBuf.push_back(task);
}
//启动工作线程
void Start()
{
std::thread t(std::mem_fn(&CellTaskServer::OnRun), this);
ptest = &t;
if (ptest)
{
t.detach();
std::cout << "The send thread is " << _threadid << std::endl;
}
else
{
std::cout << "Create thread failling." << std::endl;
}
}
protected:
//工作函数
void OnRun()
{
while (true)
{
//从缓冲区取出数据
if (!_taskBuf.empty())
{
std::lock_guard<std::mutex>lock(_mutex);
for (auto pTask : _taskBuf)
{
_tasks.push_back(pTask);
}
_taskBuf.clear();
}
//如果没有任务
if (_tasks.empty())
{
std::chrono::milliseconds t(1);
std::this_thread::sleep_for(t);
continue;
}
for (auto pTask:_tasks)
{
pTask->doTask();
delete pTask;
}
_tasks.clear();
}
}
private:
//任务数据
std::list<CellTask*>_tasks;
//任务数据缓冲区
std::list<CellTask*>_taskBuf;
//改变数据缓冲区时需要加锁
std::mutex _mutex;
//Judge whether the thread is created successfully
std::thread* ptest = nullptr;
};
#endif // !_CELL_TASK_H_
Server.hpp
#ifndef _EasyTcpServer_hpp_
#define _EasyTcpServer_hpp_
#define _CRT_SECURE_NO_WARNINGS
#ifdef _WIN32
#define FD_SETSIZE 2506
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<windows.h>
#include<WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
#else
#include<unistd.h> //uni std
#include<arpa/inet.h>
#include<string.h>
#define SOCKET int
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
#endif
#include<stdio.h>
#include<vector>
#include<map>
#include<thread>
#include<mutex>
#include<atomic>
#include<functional>
#include"MessageHeader.hpp"
#include"CELLTimestamp.hpp"
#include"CELLTask.hpp"
//缓冲区最小单元大小
#ifndef RECV_BUFF_SZIE
#define RECV_BUFF_SZIE 10240*5
#define SEND_BUFF_SZIE RECV_BUFF_SZIE
#endif // !RECV_BUFF_SZIE
//客户端数据类型
class ClientSocket
{
public:
ClientSocket(SOCKET sockfd = INVALID_SOCKET)
{
_sockfd = sockfd;
memset(_szMsgBuf, 0, RECV_BUFF_SZIE);
_lastPos = 0;
memset(_szSendBuf, 0, SEND_BUFF_SZIE);
_lastSendPos = 0;
}
SOCKET sockfd()
{
return _sockfd;
}
char* msgBuf()
{
return _szMsgBuf;
}
int getLastPos()
{
return _lastPos;
}
void setLastPos(int pos)
{
_lastPos = pos;
}
//发送数据
int SendData(DataHeader* header)
{
int ret = SOCKET_ERROR;
//要发送的数据长度
int nSendLen = header->dataLength;
//要发送的数据
const char* pSendData = (const char*)header;
while (true)
{
if (_lastSendPos + nSendLen >= SEND_BUFF_SZIE)
{
//计算可拷贝的数据长度
int nCopyLen = SEND_BUFF_SZIE - _lastSendPos;
//拷贝数据
memcpy(_szSendBuf + _lastSendPos, pSendData, nCopyLen);
//计算剩余数据位置
pSendData += nCopyLen;
//计算剩余数据长度
//nSendLen -= nSendLen;
nSendLen -= nCopyLen;
//发送数据
ret = send(_sockfd, _szSendBuf, SEND_BUFF_SZIE, 0);
//数据尾部位置清零
_lastSendPos = 0;
//发送错误
if (SOCKET_ERROR == ret)
{
return re