X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fcommand_client.cpp;h=41c74de07f6060c8fd31d7b1221a930d121cc52c;hp=a603634226a72cd3d2cc33408c5d4ee00eba34d4;hb=04d86ba4ad4f14ab08f38804e772ec46a8ac92b0;hpb=d535333ffe637c9e547e68b792f334c229641520 diff --git a/src/command_client.cpp b/src/command_client.cpp index a603634..41c74de 100644 --- a/src/command_client.cpp +++ b/src/command_client.cpp @@ -26,13 +26,61 @@ #include #include +#include + #include "command_client.hxx" +#ifdef HAVE_CONFIG_H +#include +#endif + using namespace std; namespace libt2n { +command_client::command_client(client_connection& _c) + : c(_c) +{ + // for reconnects + c.add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this))); + + read_hello(); +} + +void command_client::read_hello() +{ + // TODO: fix timeout + string resultpacket; + while(!c.get_packet(resultpacket) && !c.is_closed()) + c.fill_buffer(); + + istringstream hello(resultpacket); + + char chk[5]; + hello.read(chk,4); + chk[4]=0; + if (hello.fail() || hello.eof() || string("T2Nv") != chk) + throw t2n_version_mismatch("illegal hello received (T2N)"); + + int prot_version; + hello >> prot_version; + if (hello.fail() || hello.eof() || prot_version != PROTOCOL_VERSION) + throw t2n_version_mismatch("not compatible with the server protocol version"); + + hello.read(chk,1); + if (hello.fail() || hello.eof() || chk[0] != ';') + throw t2n_version_mismatch("illegal hello received (1. ;)"); + + hello.read(chk,4); + if (hello.fail() || hello.eof() || *((int*)chk) != 1) + throw t2n_version_mismatch("host byte order not matching"); + + hello.read(chk,1); + if (hello.fail() || hello.eof() || chk[0] != ';') + throw t2n_version_mismatch("illegal hello received (2. ;)"); +} + void command_client::send_command(command* cmd, result_container &res) { ostringstream ofs;