test_tcpserver.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <iostream>
  2. #include <string>
  3. #include "tcpserver.h"
  4. #include "mswin_special/sys/DumpFile.h"
  5. class TestTCPProtocol: public TCPServerProtocolProcess
  6. {
  7. public:
  8. TestTCPProtocol(){}
  9. virtual ~TestTCPProtocol(){}
  10. virtual const std::string& ParsePacket(const NetPacket& packet, const unsigned char* buf){
  11. static char senddata[256];
  12. sprintf(senddata,"****recv datalen %d",packet.datalen);
  13. fprintf(stdout,"%s\n",senddata);
  14. NetPacket tmppack = packet;
  15. tmppack.datalen = (std::min)(strlen(senddata),sizeof(senddata)-1);
  16. pro_packet_ = PacketData(tmppack,(const unsigned char*)senddata);
  17. return pro_packet_;
  18. }
  19. private:
  20. std::string pro_packet_;
  21. };
  22. using namespace std;
  23. using namespace uv;
  24. bool is_eist = false;
  25. int call_time = 0;
  26. TCPServer server(0x01,0x02);
  27. void CloseCB(int clientid, void* userdata)
  28. {
  29. fprintf(stdout,"cliend %d close\n",clientid);
  30. TCPServer *theclass = (TCPServer *)userdata;
  31. //is_eist = true;
  32. }
  33. void NewConnect(int clientid, void* userdata)
  34. {
  35. fprintf(stdout,"new connect:%d\n",clientid);
  36. server.SetRecvCB(clientid,NULL,NULL);
  37. }
  38. int main(int argc, char** argv)
  39. {
  40. TestTCPProtocol protocol;
  41. DeclareDumpFile();
  42. TCPServer::StartLog("log/");
  43. server.SetNewConnectCB(NewConnect,&server);
  44. server.SetPortocol(&protocol);
  45. if(!server.Start("0.0.0.0",12345)) {
  46. fprintf(stdout,"Start Server error:%s\n",server.GetLastErrMsg());
  47. }
  48. server.SetKeepAlive(1,60);//enable Keepalive, 60s
  49. fprintf(stdout,"server return on main.\n");
  50. while(!is_eist) {
  51. Sleep(10);
  52. }
  53. return 0;
  54. }