Dealing with connection handshake to server in ana.
This commit is contained in:
parent
76d02ebcd0
commit
424755447a
6 changed files with 46 additions and 13 deletions
|
@ -57,8 +57,8 @@ namespace ana
|
|||
* @param size : The size of the buffer.
|
||||
*/
|
||||
read_buffer_implementation( size_t size ) :
|
||||
base_( new char[ size ] ),
|
||||
size_( size )
|
||||
base_( new char[ size ] ),
|
||||
size_( size )
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -123,9 +123,9 @@ namespace ana
|
|||
{
|
||||
public:
|
||||
copying_buffer( boost::asio::const_buffer buffer, ana::send_type copy_buffer) :
|
||||
size_(boost::asio::detail::buffer_size_helper(buffer) ),
|
||||
base_( NULL ),
|
||||
copy_( copy_buffer )
|
||||
size_(boost::asio::detail::buffer_size_helper(buffer) ),
|
||||
base_( NULL ),
|
||||
copy_( copy_buffer )
|
||||
{
|
||||
if (copy_buffer)
|
||||
{
|
||||
|
|
|
@ -168,6 +168,12 @@ class ChatClient : public ana::listener_handler,
|
|||
|
||||
if (msg == "/quit")
|
||||
std::cout << "\nExiting.\n";
|
||||
if (msg == "/empty")
|
||||
{
|
||||
const std::string empty_str;
|
||||
|
||||
client_->send( ana::buffer( empty_str ), this );
|
||||
}
|
||||
else
|
||||
if (msg.size() > 0)
|
||||
client_->send( ana::buffer( msg ), this);
|
||||
|
|
|
@ -129,7 +129,9 @@ class ChatServer : public listener_handler,
|
|||
{
|
||||
std::string msg = buffer->string();
|
||||
|
||||
if (msg[0] == '/')
|
||||
if (msg.empty())
|
||||
std::cout << "Received empty buffer. Size: " << buffer->size() << "\n";
|
||||
else if (msg[0] == '/')
|
||||
parse_command(client, msg);
|
||||
else
|
||||
{
|
||||
|
|
|
@ -205,10 +205,11 @@ void asio_client::handle_sent_header(const boost::system::error_code& ec,
|
|||
|
||||
if ( ! ec )
|
||||
{
|
||||
boost::asio::async_write(socket_, boost::asio::buffer(buffer->base(), buffer->size() ),
|
||||
boost::bind(&asio_client::handle_send,this,
|
||||
boost::asio::placeholders::error,
|
||||
buffer, handler));
|
||||
if ( buffer->size() != 0 )
|
||||
boost::asio::async_write(socket_, boost::asio::buffer(buffer->base(), buffer->size() ),
|
||||
boost::bind(&asio_client::handle_send,this,
|
||||
boost::asio::placeholders::error,
|
||||
buffer, handler));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -104,6 +104,15 @@ void asio_listener::handle_header(char* header, const boost::system::error_code&
|
|||
boost::asio::placeholders::error,
|
||||
listener));
|
||||
}
|
||||
else
|
||||
{
|
||||
// copy the header to a read_buffer
|
||||
ana::detail::read_buffer read_buf ( new ana::detail::read_buffer_implementation( ana::HEADER_LENGTH ) );
|
||||
for (size_t i(0); i< ana::HEADER_LENGTH; ++i)
|
||||
static_cast<char*>(read_buf->base())[i] = header[i];
|
||||
|
||||
listener->handle_message( ec, id(), read_buf );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
|
|
|
@ -330,7 +330,8 @@ class clients_manager : public ana::connection_handler
|
|||
std::set<ana::net_id> ids_;
|
||||
};
|
||||
|
||||
class ana_network_manager : public ana::listener_handler
|
||||
class ana_network_manager : public ana::listener_handler,
|
||||
public ana::send_handler
|
||||
{
|
||||
public:
|
||||
ana_network_manager() :
|
||||
|
@ -378,7 +379,7 @@ class ana_network_manager : public ana::listener_handler
|
|||
|
||||
ana_connect_handler handler(mutex, connect_timer_);
|
||||
|
||||
connect_timer_->wait( ana::time::seconds(3), // 3 seconds to connection timeout
|
||||
connect_timer_->wait( ana::time::seconds(10), // 10 seconds to connection timeout
|
||||
boost::bind(&ana_connect_handler::handle_timeout, &handler,
|
||||
boost::asio::error::make_error_code( boost::asio::error::timed_out ) ) );
|
||||
|
||||
|
@ -386,7 +387,7 @@ class ana_network_manager : public ana::listener_handler
|
|||
|
||||
client->set_listener_handler( this );
|
||||
client->run();
|
||||
// client->start_logging();
|
||||
client->start_logging();
|
||||
|
||||
mutex.lock(); // just wait for handler to release it
|
||||
mutex.unlock(); // unlock for destruction
|
||||
|
@ -394,7 +395,13 @@ class ana_network_manager : public ana::listener_handler
|
|||
delete connect_timer_;
|
||||
|
||||
if( ! handler.error() )
|
||||
{
|
||||
//Send handshake
|
||||
const std::string empty_str;
|
||||
client->send( ana::buffer( empty_str ), this );
|
||||
|
||||
return network::connection( client->id() );
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
@ -540,6 +547,8 @@ class ana_network_manager : public ana::listener_handler
|
|||
|
||||
network::statistics get_send_stats(network::connection handle)
|
||||
{
|
||||
std::cout << "DEBUG: in get_send_stats to " << handle << "\n";
|
||||
|
||||
if ( handle != 0 )
|
||||
{
|
||||
ana::net_id id( handle );
|
||||
|
@ -587,6 +596,12 @@ class ana_network_manager : public ana::listener_handler
|
|||
}
|
||||
|
||||
private:
|
||||
virtual void handle_send(ana::error_code error_code, ana::net_id client)
|
||||
{
|
||||
if ( error_code )
|
||||
network::disconnect( client );
|
||||
}
|
||||
|
||||
virtual void handle_message( ana::error_code error,
|
||||
ana::net_id client,
|
||||
ana::detail::read_buffer buffer)
|
||||
|
|
Loading…
Add table
Reference in a new issue