From 8104c8f70605ee24230e5988d7bf68e3fce7465a Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Sat, 28 Oct 2006 01:42:42 +0000 Subject: [PATCH] libt2n: (gerd) add hello messages --- src/command_client.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/command_client.hxx | 6 +++--- src/command_server.cpp | 21 ++++++++++++++++++--- src/command_server.hxx | 2 +- src/connection.cpp | 14 ++++++++------ src/connection.hxx | 5 +++-- 6 files changed, 81 insertions(+), 15 deletions(-) diff --git a/src/command_client.cpp b/src/command_client.cpp index a603634..c0e863c 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.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; diff --git a/src/command_client.hxx b/src/command_client.hxx index 640faac..495192a 100644 --- a/src/command_client.hxx +++ b/src/command_client.hxx @@ -31,10 +31,10 @@ class command_client private: client_connection &c; + void read_hello(); + public: - command_client(client_connection& _c) - : c(_c) - { } + command_client(client_connection& _c); void send_command(command* cmd, result_container &res); }; diff --git a/src/command_server.cpp b/src/command_server.cpp index 2a4a7b4..2aa4fc0 100644 --- a/src/command_server.cpp +++ b/src/command_server.cpp @@ -34,6 +34,10 @@ #include "container.hxx" #include "log.hxx" +#ifdef HAVE_CONFIG_H +#include +#endif + using namespace std; namespace libt2n @@ -43,12 +47,23 @@ command_server::command_server(server& _s) : s(_s) { // register callback - s.add_callback(new_connection,bind(&command_server::new_connection_callback, boost::ref(*this), _1)); + s.add_callback(new_connection,bind(&command_server::send_hello, boost::ref(*this), _1)); } -void command_server::new_connection_callback(unsigned int conn_id) +void command_server::send_hello(unsigned int conn_id) { - cerr << "new connection callback: " << conn_id << endl; + server_connection* sc=s.get_connection(conn_id); + + ostringstream hello; + + hello << "T2Nv" << PROTOCOL_VERSION << ';'; + + int byteordercheck=1; + hello.write((char*)&byteordercheck,sizeof(byteordercheck)); + + hello << ';'; + + sc->write(hello.str()); } /// handle a command including deserialization and answering diff --git a/src/command_server.hxx b/src/command_server.hxx index 686be14..cb41a76 100644 --- a/src/command_server.hxx +++ b/src/command_server.hxx @@ -37,7 +37,7 @@ class command_server void handle(long long usec_timeout=-1); - void new_connection_callback(unsigned int conn_id); + void send_hello(unsigned int conn_id); }; } diff --git a/src/connection.cpp b/src/connection.cpp index 361cfda..9cffb89 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -21,6 +21,8 @@ #include #include +#include + #include "connection.hxx" namespace libt2n @@ -31,13 +33,13 @@ connection::packet_size_indicator connection::bytes_available() // max packet size is unsigned int // no size information -> no packet - if (buffer.size() < sizeof(unsigned int)) + if (buffer.size() < sizeof(packet_size_indicator)) return 0; - packet_size_indicator psize=*((packet_size_indicator*)(buffer.data())); + packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data()))); // enough data for one packet in buffer? - if (buffer.size() < sizeof(unsigned int)+psize) + if (buffer.size() < sizeof(packet_size_indicator)+psize) return 0; // ok, full packet there @@ -55,8 +57,8 @@ bool connection::get_packet(std::string& data) if ((psize=bytes_available())) { - data.assign(buffer,sizeof(unsigned int),psize); - buffer.erase(0,sizeof(unsigned int)+psize); + data.assign(buffer,sizeof(packet_size_indicator),psize); + buffer.erase(0,sizeof(packet_size_indicator)+psize); return true; } else @@ -67,7 +69,7 @@ bool connection::get_packet(std::string& data) void connection::write(const std::string& data) { // prepend packet size to data - packet_size_indicator psize=data.size(); + packet_size_indicator psize=htonl(data.size()); std::string send_data(data); send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); diff --git a/src/connection.hxx b/src/connection.hxx index 4092fde..2343984 100644 --- a/src/connection.hxx +++ b/src/connection.hxx @@ -21,8 +21,9 @@ #include -#include "types.hxx" +#include +#include "types.hxx" namespace libt2n { @@ -40,7 +41,7 @@ class connection std::string buffer; - typedef unsigned int packet_size_indicator; + typedef uint32_t packet_size_indicator; packet_size_indicator bytes_available(); -- 1.7.1