ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
1.2 KiB

  1. #include "tcp_messenger.h"
  2. namespace cppcms {
  3. void messenger::connect(string ip,int port)
  4. {
  5. ip_=ip;
  6. port_=port;
  7. error_code e;
  8. socket_.connect(tcp::endpoint(aio::ip::address::from_string(ip),port),e);
  9. if(e) throw cppcms_error("connect:"+e.message());
  10. tcp::no_delay nd(true);
  11. socket_.set_option(nd);
  12. }
  13. messenger::messenger(string ip,int port) :
  14. socket_(srv_)
  15. {
  16. connect(ip,port);
  17. }
  18. messenger::messenger() :
  19. socket_(srv_)
  20. {
  21. }
  22. void messenger::transmit(tcp_operation_header &h,string &data)
  23. {
  24. bool done=false;
  25. int times=0;
  26. do {
  27. try {
  28. aio::write(socket_,aio::buffer(&h,sizeof(h)));
  29. if(h.size>0) {
  30. aio::write(socket_,aio::buffer(data,h.size));
  31. }
  32. aio::read(socket_,aio::buffer(&h,sizeof(h)));
  33. if(h.size>0) {
  34. vector<char> d(h.size);
  35. aio::read(socket_,aio::buffer(d,h.size));
  36. data.assign(d.begin(),d.begin()+h.size);
  37. }
  38. done=true;
  39. }
  40. catch(system_error const &e) {
  41. if(times) {
  42. throw cppcms_error(string("tcp_cache:")+e.what());
  43. }
  44. socket_.close();
  45. error_code er;
  46. socket_.connect(
  47. tcp::endpoint(
  48. aio::ip::address::from_string(ip_),port_),er);
  49. if(er) throw cppcms_error("reconnect:"+er.message());
  50. times++;
  51. }
  52. }while(!done);
  53. }
  54. } // namespace cppcms