//服务端test代码
void TestTcpSvrFunc()
{
uv::CLooper* ptrLooper = new uv::CLooper;
ptrLooper->InitLooper();
uv::CTcpServer server(DEF_PACK_HEAD_FLAG);
server.AttachLooper(ptrLooper);
server.OnNewConnectCBEvent([](int clientid, void* userdata){
uv::CTcpClientSession *theclass = (uv::CTcpClientSession *)userdata;
LOGI("new connect:%d\n", clientid);
});
server.OnTcpClientCloseCBEvent([](int clientid, void* userdata){
LOGI("cliend %d closed", clientid);
uv::CTcpClientSession *theclass = (uv::CTcpClientSession *)userdata;
});
server.OnTcpClientRecvCBEvent([](NetPacket* pNetPacket, void* userdata){
uv::CTcpClientSession *pSession = (uv::CTcpClientSession *)userdata;
char szRecvData[1024] = {0};
std::memcpy(szRecvData, pNetPacket->data, pNetPacket->dataSize);
LOGI("clientid=%d recv=%s", pSession->GetClientId(), szRecvData);
//send back
int pack_size = pNetPacket->dataSize + NET_PACKAGE_HEADLEN;
std::string buffer;
buffer.resize(pack_size);
memcpy(&(buffer[0]), (void*)pNetPacket, NET_PACKAGE_HEADLEN);
memcpy(&(buffer[NET_PACKAGE_HEADLEN]), pNetPacket->data, pNetPacket->dataSize);
pSession->Send((char*)&buffer[0], buffer.size());
//test shutdown
//pSession->ShutDown();
});
if(!server.Start("0.0.0.0", 6666)) {
LOGE("Start Server error:%s\n", server.GetLastErrMsg());
}
server.SetKeepAlive(1, 60);
LOGI("tcp server on main.");
if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, true))
{
while (1) { Sleep(1000); }
}
else
{
delete ptrLooper;
ptrLooper = nullptr;
printf("exit \r\n");
}
}
void TestUdpSvrFunc()
{
std::string strLocal = "127.0.0.1";
const int port = 6666;
uv::CLooper* ptrLooper = new uv::CLooper;
ptrLooper->InitLooper();
uv::CUvUdp* ptrSvr = new uv::CUvUdp();
ptrSvr->AttachLooper(ptrLooper);
ptrSvr->Bind(strLocal.c_str(), port);
ptrSvr->OnRecvCBEvent([ptrSvr](const char* buffer, int size, const struct sockaddr* pAddr, unsigned iFlag, void* userdata){
uv::CUvUdp* pClient = (uv::CUvUdp*)userdata;
char szRecvData[1024] = {0};
std::memcpy(szRecvData, buffer, size);
struct sockaddr_in *sock = ( struct sockaddr_in*)pAddr;
int port = ntohs(sock->sin_port);
LOGI("[RECV][ip=%s][port=%d] =>---%s", inet_ntoa(sock->sin_addr), port, szRecvData);
ptrSvr->Send(buffer, size, pAddr);
LOGI("[SEND_BACK] =>+++%s", szRecvData);
});
LOGI("udp server on main.");
if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, true))
{
while (1) { Sleep(1000); }
}
else
{
delete ptrLooper;
ptrLooper = nullptr;
printf("exit \r\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
TestTcpSvrFunc();
//TestUdpSvrFunc();
return 0;
}
//客户端
std::string PacketData(int clientId, const char* data, int dataSize)
{
uint32_t pack_size = dataSize + NET_PACKAGE_HEADLEN;
NetPacket netPacket;
netPacket.flag = DEF_PACK_HEAD_FLAG;
netPacket.type = clientId;
netPacket.dataSize = dataSize;
std::string buffer;
buffer.resize(pack_size);
memcpy(&(buffer[0]), (const char*)&netPacket, 12);
memcpy(&(buffer[12]), data, dataSize);
return buffer;
}
void SetConsoleColor(unsigned short ForeColor=0, unsigned short BackGroundColor=0)
{
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
}
int call_time = 0;
void TestTcpClientFunc()
{
const int clientsize = 5;
std::string strServerIp = "127.0.0.1";
const int port = 6666;
uv::CLooper* ptrLooper = new uv::CLooper;
ptrLooper->InitLooper();
std::map<int, uv::CTcpClient*> clientMap;
for (int i = 0; i < clientsize; ++i) {
uv::CTcpClient* ptrClient = new uv::CTcpClient(DEF_PACK_HEAD_FLAG);
clientMap[ptrClient->GetClientId()] = ptrClient;
ptrClient->AttachLooper(ptrLooper);
ptrClient->OnConnectCBEvent([](int status, void* userdata){
uv::CTcpClient* pClient = (uv::CTcpClient*)userdata;
if(status == uv::CTcpClient::TCP_STATUS_CONNECTED){
LOGI("client_id=%d connect succeed.", pClient->GetClientId());
}
else{
LOGI("client_id=%d connect error=%s.", pClient->GetClientId(), pClient->GetLastErrMsg());
}
});
ptrClient->OnRecvCBEvent([](NetPacket* pNetPacket, void* userdata){
uv::CTcpClient* pClient = (uv::CTcpClient*)userdata;
char szRecvData[1024] = {0};
std::memcpy(szRecvData, pNetPacket->data, pNetPacket->dataSize);
LOGI("[RECV][id=%d] =>---%s", pClient->GetClientId(), szRecvData);
});
ptrClient->OnCloseCBEvent([&clientMap](int clientid, void* userdata){
uv::CTcpClient* pClient = (uv::CTcpClient*)userdata;
LOGI("[CLOSE][id=%d]", clientid);
});
ptrClient->Connect(strServerIp.c_str(), port);
}
char senddata[256];
int nTry = 500;
while (nTry > 0)
{
for (auto iter = clientMap.begin(), iterEnd = clientMap.end();
iter != iterEnd; iter++) {
uv::CTcpClient* pClient = iter->second;
if(pClient->IsConnected())
{
memset(senddata, 0, sizeof(senddata));
sprintf(senddata, "%d", ++call_time);
std::string str = PacketData(pClient->GetClientId(), senddata, strlen(senddata));
if (pClient->Send(&str[0], str.length()) <= 0) {
LOGI("[ERROR][id=%d] =>%s", pClient->GetClientId(), pClient->GetLastErrMsg());
} else {
LOGI("[SEND][id=%d] =>+++%s", pClient->GetClientId(), senddata);
}
}
}
nTry--;
Sleep(10);
}
//shutdow all
for (auto iter = clientMap.begin(), iterEnd = clientMap.end();
iter != iterEnd; iter++) {
uv::CTcpClient* pClient = iter->second;
if(pClient->IsConnected())
{
pClient->Shutdown();
}
}
delete ptrLooper;
ptrLooper = nullptr;
//delete
for (auto iter = clientMap.begin(), iterEnd = clientMap.end();
iter != iterEnd; iter++) {
uv::CTcpClient* pClient = iter->second;
delete pClient;
pClient = nullptr;
}
clientMap.clear();
}
void TestUdpClientFunc()
{
std::string strServerIp = "127.0.0.1";
const int port = 6666;
uv::CLooper* ptrLooper = new uv::CLooper;
ptrLooper->InitLooper();
uv::CUvUdp* ptrClient = new uv::CUvUdp();
ptrClient->AttachLooper(ptrLooper);
ptrClient->Bind("127.0.0.1", 3333);
ptrClient->OnRecvCBEvent([](const char*_buff, int size, const struct sockaddr* pAddr, unsigned iFlag, void* userdata){
uv::CUvUdp* pClient = (uv::CUvUdp*)userdata;
char szRecvData[1024] = {0};
std::memcpy(szRecvData, _buff, size);
LOGI("[RECV] =>---%s", szRecvData);
});
char senddata[256];
int nTry = 500;
while (nTry > 0)
{
memset(senddata, 0, sizeof(senddata));
sprintf(senddata, "%d", ++call_time);
if (ptrClient->Send(senddata, strlen(senddata), strServerIp.c_str(), port) <= 0) {
LOGI("[ERROR] =>%s", ptrClient->GetLastErrMsg());
} else {
LOGI("[SEND] =>+++%s", senddata);
}
nTry--;
Sleep(10);
}
delete ptrLooper;
ptrLooper = nullptr;
//delete
delete ptrClient;
ptrClient = nullptr;
}
int _tmain(int argc, _TCHAR* argv[])
{
TestTcpClientFunc();
//TestUdpClientFunc();
system("pause");
return 0;
}
demo详细代码可以到github查看:https://github.com/zym479466697/TestLibuv.git