Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/liblinebasedcomms/tcp_server.cpp
00001 // copyright 2010 t. schneider tes@mit.edu
00002 //
00003 //
00004 // This program is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This software is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "tcp_server.h"
00018 
00019 boost::shared_ptr<goby::util::TCPConnection> goby::util::TCPConnection::create()
00020 {
00021     return boost::shared_ptr<TCPConnection>(new TCPConnection());
00022 }        
00023 
00024 void goby::util::TCPConnection::socket_write(const protobuf::Datagram& line) // give it a string
00025 {
00026     bool write_in_progress = !out().empty(); // is there anything currently being written?
00027     out().push_back(line); // store in write buffer
00028     if (!write_in_progress) // if nothing is currently being written, then start
00029         write_start();
00030 }
00031 
00032 void goby::util::TCPConnection::socket_close(const boost::system::error_code& error)
00033 { // something has gone wrong, so close the socket & make this object inactive
00034     if (error == boost::asio::error::operation_aborted) // if this call is the result of a timer cancel()
00035         return; // ignore it because the connection cancelled the timer
00036     
00037     if(error)
00038         socket_.close();
00039 }
00040 
00041     
00042 void goby::util::TCPServer::do_write(const protobuf::Datagram& line)
00043 {
00044     BOOST_FOREACH(boost::shared_ptr<TCPConnection> c, connections_)
00045         c->write(line);
00046 }
00047 
00048 // close all the connections, it's up to the clients to try to reconnect
00049 void goby::util::TCPServer::do_close(const boost::system::error_code& error)
00050 {
00051     BOOST_FOREACH(boost::shared_ptr<TCPConnection> c, connections_)
00052         c->close(error);
00053 }    
00054 
00055 void goby::util::TCPServer::start_accept()
00056 {
00057     new_connection_ = TCPConnection::create();
00058     acceptor_.async_accept(new_connection_->socket(),
00059                            boost::bind(&TCPServer::handle_accept, this, new_connection_, boost::asio::placeholders::error));
00060 }
00061 
00062 void goby::util::TCPServer::handle_accept(boost::shared_ptr<TCPConnection> new_connection, const boost::system::error_code& error)
00063 {
00064     if (!error)
00065     {
00066         new_connection_->start();
00067         connections_.insert(new_connection_);
00068         start_accept();
00069     }
00070 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends