fixed off by 1 error in sync
[anytun.git] / syncClientSocket.cpp
blob6b791bc5359a2eb031e456e40a65a253d2582c5d
1 #include <sstream>
2 #include <iostream>
3 #include <string>
5 #include <boost/archive/text_oarchive.hpp>
6 #include <boost/archive/text_iarchive.hpp>
9 #include "log.h"
10 //#include "connectionParam.h"
11 #include "Sockets/Utility.h"
12 #include "syncClientSocket.h"
13 #include "buffer.h"
16 SyncClientSocket::SyncClientSocket(ISocketHandler& h,ConnectionList & cl)
17 :TcpSocket(h),cl_(cl),missing_chars(-1)
19 // initial connection timeout setting and number of retries
20 SetConnectTimeout(12);
21 SetConnectionRetry(-1); //infinite reties
23 // Also reconnect broken link
24 SetReconnect(true);
28 bool SyncClientSocket::OnConnectRetry()
30 return true;
34 void SyncClientSocket::OnReconnect()
36 cLog.msg(Log::PRIO_NOTICE) << "reconnected with " << GetRemoteHostname() << std::endl;
37 // ...
38 //Send("Welcome back\r\n");
42 void SyncClientSocket::OnRawData(const char *buf,size_t len)
43 //void SyncClientSocket::OnLine(const std::string& line)
45 for(size_t index=0;index<len;index++)
47 std::cout << buf[index];
48 iss_ << buf[index];
50 while (1)
52 if(missing_chars==-1 && iss_.str().size()>5)
54 iss_>>missing_chars;
55 } else
56 if(missing_chars>0 && missing_chars<static_cast<int16_t>(iss_.str().size()))
58 char * buffer = new char [missing_chars+1];
59 iss_.read(buffer,missing_chars+1);
60 std::stringstream tmp;
61 tmp.write(buffer+1,missing_chars);
62 cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion from " << GetRemoteHostname() <<" \""<<tmp.str()<<'"'<< std::endl;
63 boost::archive::text_iarchive ia(tmp);
64 SyncCommand scom(cl_);
65 ia >> scom;
66 missing_chars=-1;
67 delete buffer;
68 } else
69 break;
72 //u_int16_t mux = scom.getMux();
73 //const ConnectionParam & conn = cl_.getConnection(mux)->second;
74 //cLog.msg(Log::PRIO_NOTICE) << "sync connection #"<<mux<<" remote host " << conn.remote_host_ << ":" << conn.remote_port_ << std::endl;
77 //void StatusClientSocket::InitSSLServer()
78 //{
79 // InitializeContext("server.pem", "keypwd", SSLv23_method());
80 //}
83 //void StatusClientSocket::Init()
84 //{
85 // EnableSSL();
86 //}