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.
 
 
 
 
 
 

95 lines
2.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "tcp_messenger.h"
  21. #include "cppcms_error.h"
  22. #include <booster/aio/socket.h>
  23. #include <booster/aio/buffer.h>
  24. #include <booster/aio/endpoint.h>
  25. #include <booster/system_error.h>
  26. namespace cppcms {
  27. namespace impl {
  28. void messenger::connect(std::string ip,int port)
  29. {
  30. ip_=ip;
  31. port_=port;
  32. booster::system::error_code e;
  33. booster::aio::endpoint ep(ip,port);
  34. socket_.open(ep.family(),booster::aio::sock_stream,e);
  35. if(!e)
  36. socket_.connect(ep,e);
  37. if(e)
  38. throw cppcms_error("connect:"+e.message());
  39. socket_.set_option(booster::aio::socket::tcp_no_delay,true);
  40. }
  41. messenger::messenger(std::string ip,int port) :
  42. socket_()
  43. {
  44. connect(ip,port);
  45. }
  46. messenger::messenger() :
  47. socket_()
  48. {
  49. }
  50. void messenger::transmit(tcp_operation_header &h,std::string &data)
  51. {
  52. bool done=false;
  53. int times=0;
  54. do {
  55. try {
  56. // FIXME use buffers
  57. socket_.write(booster::aio::buffer(&h,sizeof(h)));
  58. if(h.size>0) {
  59. socket_.write(booster::aio::buffer(data.c_str(),h.size));
  60. }
  61. socket_.read(booster::aio::buffer(&h,sizeof(h)));
  62. if(h.size>0) {
  63. std::vector<char> d(h.size);
  64. socket_.read(booster::aio::buffer(d));
  65. data.assign(d.begin(),d.begin()+h.size);
  66. }
  67. done=true;
  68. }
  69. catch(booster::system::system_error const &e) {
  70. if(times) {
  71. throw cppcms_error(std::string("tcp_cache:")+e.what());
  72. }
  73. socket_.close();
  74. booster::aio::endpoint ep(ip_,port_);
  75. booster::system::error_code er;
  76. socket_.open(ep.family(),booster::aio::sock_stream,er);
  77. if(!er)
  78. socket_.connect(ep,er);
  79. if(er) throw cppcms_error("reconnect:"+er.message());
  80. times++;
  81. }
  82. }while(!done);
  83. }
  84. } // impl
  85. } // namespace cppcms