Skip to content

Commit

Permalink
调试,tcp 多线程服务+ 客户端测试
Browse files Browse the repository at this point in the history
  • Loading branch information
achilsh committed Apr 24, 2018
1 parent 3973657 commit 3ce6ecc
Show file tree
Hide file tree
Showing 25 changed files with 2,115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
tags
TcpClientMain
TcpSrvMain
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
INC_SVR_COMM = -I. -I/usr/local/include
LIB_SVR_COMM = -L/usr/local/lib -levent -lpthread

INC_ALL=$(INC_SVR_COMM)
LIB_ALL=$(LIB_SVR_COMM)

BINARY = TcpSrvMain TcpClientMain

all:$(BINARY)


.SUFFIXES: .o .cpp
CXX = g++
CC = gcc

CXXFLAGS= -g -Wall

%.o:%.cpp
$(CXX) $(CFLAGS) -c $^ $(INC_ALL)

TcpSrvMain:t_main.o t_tcpsrv.o t_socket.o t_proto.o t_eventpipe.o t_eventconn.o t_eventbase.o t_worktask.o t_eventlisten.o t_thread.o
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIB_ALL)
TcpClientMain:t_client.o t_tcpsrv.o t_socket.o t_proto.o t_eventpipe.o t_eventconn.o t_eventbase.o t_worktask.o t_eventlisten.o t_thread.o
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIB_ALL)
clean:
rm -f *.o *~ $(BINARY)

strip:
strip $(BINARY)
217 changes: 217 additions & 0 deletions t_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#include "t_client.hpp"

namespace T_CLIENT
{
TestOneClient::TestOneClient(const std::string& sRemoteIp, int iRemotePort, int iSendTimes)
:m_sRemoteIp(sRemoteIp), m_iRemotePort(iRemotePort), m_iTestNums(iSendTimes), m_iFd(-1),
m_pCodec(NULL)
{
}

TestOneClient::~TestOneClient()
{
if (m_iFd > 0)
{
close(m_iFd);
m_iFd = -1;
}
///
if (m_pCodec)
{
delete m_pCodec;
m_pCodec = NULL;
}
}

int TestOneClient::Init()
{
m_iFd = socket( AF_INET, SOCK_STREAM , 0 );
if (m_iFd < 0)
{
std::cout << "sockect() call fail" << std::endl;
return -1;
}

m_stDestAddr.sin_family = AF_INET;
m_stDestAddr.sin_addr.s_addr = inet_addr(const_cast<char*> (m_sRemoteIp.c_str()));
m_stDestAddr.sin_port=htons(m_iRemotePort);
int iRet = ::connect( m_iFd,(struct sockaddr *)&m_stDestAddr, sizeof(m_stDestAddr) );
if (iRet < 0)
{
perror("connect remote tcp srv fail");
return -1;
}
m_pCodec = new BusiCodec(1);

return 0;
}
//
int TestOneClient::main()
{
for (int i = 0; i < m_iTestNums; ++i)
{
char buf[1024];
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "%d'client is test!", i);
int iSendLen = 0;
char sSendBuf[1024] = {0};
if ( RET_RECV_OK != m_pCodec->EnCode(buf, strlen(buf)+1, sSendBuf, &iSendLen) )
{
std::cout << "encode client data fail" << std::endl;
break;
}

std::string sBuf;
sBuf.assign(buf, strlen(buf)+1);
int iRet = 0;
while ( iSendLen > 0 )
{
iRet = ::send(m_iFd, sSendBuf, iSendLen, 0);
if (iRet < 0)
{
std::cout << "send client data fail" << std::endl;
break;
}
iSendLen -= iRet;
}
std::cout << "send client data done, next to recv response, send data: " << sBuf << std::endl;

char rBuf[1024];
char rBodybuf[1024];
::memset(rBuf, 0, sizeof(rBuf));
iRet = 0;
int iRLen = 0;
int iDecodeMsgLen = 0;

do {
iRet = ::recv(m_iFd, rBuf + iRet, sizeof(rBuf) - iRet, 0);
if (iRet < 0)
{
std::cout << "recv data from srv fail" << std::endl;
break;
}
else if (iRet == 0)
{
break;
}

iRLen += iRet;
enum CODERET retDecod = m_pCodec->DeCode(rBuf, iRLen, rBodybuf, &iDecodeMsgLen );
if (retDecod == RET_RECV_OK)
{
break;
}
else
{

}
} while(1);

std::string sRBuf;
sRBuf.assign(rBodybuf, iDecodeMsgLen - sizeof(4));
std::cout << "recv reponse: " << sRBuf << std::endl;
}
this->Stop();
return 0;
}

/***********************************************************************
*
*
**********************************************************************/
TestClient::TestClient(int argc, char **argv) :m_bOk(false), m_iClientNums(0),m_iReqNums(0)
{
if (ParseCmd(argc, argv) == false)
{
Usage(argv[0]);
return ;
}
m_bOk = true;
}

TestClient::~TestClient()
{
}

bool TestClient::ParseCmd(int argc, char **argv)
{
if (argc <5)
{
std::cout << "client run param less than 5" << std::endl;
return false;
}

if (strlen(argv[1]) == 0)
{
std::cout << "remote ip is empty" << std::endl;
return false;
}
m_sRemoteIp.assign(argv[1]);

int iPort = ::atoi(argv[2]);
if (iPort <= 0)
{
std::cout << "remote port less than 0" << std::endl;
return false;
}
m_iRemotePort = iPort;

m_iClientNums = ::atoi(argv[3]);
if (m_iClientNums <= 0)
{
std::cout << "client nums less than 0 " << std::endl;
return false;
}

m_iReqNums = ::atoi(argv[4]);
if (m_iReqNums <= 0)
{
std::cout << "req nums for each client less than 0 " << std::endl;
return false;
}
return true;
}

void TestClient::Usage(const char *pBinName)
{
std::cout <<"Usage: " << pBinName << " remoteIp " << " remotePort " <<
" clientnums " << " reqnum_in_one_client" <<std::endl;
}

//
int TestClient::main()
{
std::vector<TestOneClient*> vTestClientNode;
for (int i = 0; i < m_iClientNums; ++i)
{
TestOneClient* pNode = new TestOneClient(m_sRemoteIp, m_iRemotePort, m_iReqNums);
vTestClientNode.push_back(pNode);
}

for (std::vector<TestOneClient*>::iterator it = vTestClientNode.begin(); it != vTestClientNode.end(); ++it)
{
(*it)->Start();
}
std::cout << "all client thread start " << std::endl;

for (std::vector<TestOneClient*>::iterator it = vTestClientNode.begin(); it != vTestClientNode.end(); )
{
(*it)->JoinWork();
delete (*it);
it = vTestClientNode.erase(it);
}
return 0;
}

}


//////////////////////////////////////////////
/////////////////////////////////////////////
int main(int argc, char **argv)
{
T_CLIENT::TestClient client(argc, argv);
client.main();

return 0;
}
82 changes: 82 additions & 0 deletions t_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @file: t_client.hpp
* @brief:
* @author: wusheng Hu
* @version: v0x0001
* @date: 2018-04-19
*/

#ifndef _T_CLIENT_HPP_
#define _T_CLIENT_HPP_

#ifdef __cplusplus
extern "C" {
#endif

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifdef __cplusplus
}
#endif

#include <vector>
#include <iostream>
#include <string>
#include "t_thread.hpp"
#include "t_proto.hpp"

using namespace T_TCP;
using namespace std;


namespace T_CLIENT
{
class TestOneClient: public PthreadBase
{
public:
TestOneClient(const std::string& sRemoteIp, int iRemotePort, int iSendTimes);
virtual ~TestOneClient();
protected:
int main();
virtual int Init();
private:
std::string m_sRemoteIp;
int m_iRemotePort;
int m_iTestNums;
int m_iFd;
struct sockaddr_in m_stDestAddr;
BusiCodec* m_pCodec;
};

//////////////////////////
class TestClient
{
public:
TestClient(int argc, char **argv);
virtual ~TestClient();
int main();

private:
void Usage(const char *pBinName);
bool ParseCmd(int argc, char **argv);

private:
bool m_bOk;
int m_iClientNums;
std::string m_sRemoteIp;
int m_iRemotePort;
int m_iReqNums;
};
}

#endif

19 changes: 19 additions & 0 deletions t_eventbase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "t_eventbase.hpp"
#include <unistd.h>
#include <iostream>
namespace T_TCP
{
ConnBase::ConnBase(int iFd):m_iFd(iFd)
{
}

//
ConnBase::~ConnBase()
{
event_del(&m_stREvent);
event_del(&m_stWEvent);
std::cout << "close this conn, fd: " << m_iFd << std::endl;
::close(m_iFd);
}
//
}
38 changes: 38 additions & 0 deletions t_eventbase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file: t_eventbase.hpp
* @brief:
* @author: wusheng Hu
* @version:
* @date: 2018-04-23
*/

#ifndef _t_eventbase_hpp_
#define _t_eventbase_hpp_

#include <event2/event.h>
#include <event2/buffer.h>
#include <event.h>

#include <sys/types.h>
#include <sys/socket.h>

namespace T_TCP
{
class ConnBase
{
public:
ConnBase(int iFd);
virtual ~ConnBase();
//
virtual bool AddEvent(struct event_base *pEvBase, int iEvent, void *arg) = 0;
virtual bool DelEvent(struct event_base *pEvBase, int iEvent, void *arg) = 0;
//
protected:
int m_iFd;
//
struct event m_stREvent;
struct event m_stWEvent;
};
}

#endif
Loading

0 comments on commit 3ce6ecc

Please sign in to comment.