CSocketClient.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef __CSOCKETCLIENT_H_
  2. #define __CSOCKETCLIENT_H_
  3. #include "ErrorCode.h"
  4. #include <deque>
  5. #include <boost/bind.hpp>
  6. #include "boost/bind.hpp"
  7. #include <boost/asio.hpp>
  8. #include <boost/function.hpp>
  9. #include <boost/thread.hpp>
  10. #include "CMessage.h"
  11. #include "ISocketCallback.h"
  12. #include <vector>
  13. #include "baseEx.h"
  14. #include <iostream>
  15. //extern class CWebsocketServer;
  16. bool checkHttpThreadFun(const std::string url);
  17. std::pair<bool, std::string> DetectActiveHttp(std::vector<std::string> urlArr);
  18. namespace Chromium {
  19. typedef std::deque<CMessage> CMessageQueue;
  20. class CSocketClient {
  21. typedef CSocketClient this_type;
  22. typedef boost::asio::ip::tcp::acceptor acceptor_type;
  23. typedef boost::asio::ip::tcp::endpoint endpoint_type;
  24. typedef boost::asio::ip::tcp::socket socket_type;
  25. typedef boost::asio::ip::address address_type;
  26. typedef boost::shared_ptr<socket_type> sock_ptr;
  27. typedef std::vector<char> buffer_type;
  28. public:
  29. explicit CSocketClient(CEntityBase* pEntity, unsigned int id);
  30. CSocketClient(boost::asio::io_service& ios, const char* ipAddr, const char* port, CEntityBase* pEntity, unsigned int id);
  31. ~CSocketClient();
  32. ErrorCodeEnum Connect();
  33. ErrorCodeEnum Close();
  34. ErrorCodeEnum Reconnect();
  35. ErrorCodeEnum Write(CMessage* pMsg);
  36. ErrorCodeEnum StartSocketService();
  37. ErrorCodeEnum StopSocketService();
  38. ErrorCodeEnum SetMessageHandler(ISocketCallback* obj);
  39. private:
  40. void handle_connect(const boost::system::error_code& err);
  41. void handle_close(const boost::system::error_code& err);
  42. void handle_read(const boost::system::error_code& err,
  43. const size_t bytes_transferred, CMessage& msg);
  44. void handle_write(const boost::system::error_code& err);
  45. void thread_recv();
  46. void hexdump(const char* buf, const int num);
  47. std::string hexdumpToString(const char* buf, const int num);
  48. private:
  49. // new test
  50. endpoint_type m_ep;
  51. sock_ptr m_psocket;
  52. buffer_type m_buf;
  53. boost::thread* m_pNetThread;
  54. unsigned int mID;
  55. std::vector<char> m_vbuf;
  56. boost::function<void(CMessage&, unsigned int)> mMessageHandler;
  57. CMessageQueue write_msgs_;
  58. CEntityBase* m_pEntity;
  59. };
  60. }
  61. #endif