conexus logo

ipv4_tcp_server.cpp

Conexus::IPv4::TCPServer Example

/***************************************************************************
 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
 *   rvinyard@cs.nmsu.edu                                                  *
 *                                                                         *
 *   This file is part of the conexus library.                             *
 *                                                                         *
 *   The conexus library is free software; you can redistribute it and/or  *
 *   modify it under the terms of the GNU General Public License version 3 *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   The conexus library is distributed in the hope that it will be        *
 *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#include <conexus.h>

#include <sys/types.h>
#include <unistd.h>
#include <iostream>

// Smart pointers automatically delete the contained object when the last
// reference is de-scoped. Without this list that would occur in the
// on_new_connection() function for the endpoint parameter.
//
// By storing new connections in this list they remain referenced until
// they are erased in the on_disconnected() function from the list. When
// this occurs the connection is shut down.
typedef std::list<Conexus::Endpoint::pointer> Connections;
Connections connections;

// This function is connected to the TCPServer's new endpoint signal
void on_new_connection(Conexus::Endpoint::pointer endpoint);

// This function is connected to each connection's disconnect signal
// Note that this function takes an Endpoint* parameter and not an
// Endpoint::pointer parameter. If a copy of the Endpoint::pointer
// parameter were to be bound to the signal using sigc::bind a copy
// of the endpoint smart pointer would be made and we would need
// to explicitly store the signal connection and explicitly
// call disconnect
//
// The alternative is to use the raw pointer and search for the
// endpoint by iterating through the list.
void on_disconnected(Conexus::Endpoint* endpoint);

// Each of the following functions is called when each connection receives data
void print_data1(const Conexus::Data d);
void print_data2(const Conexus::Data d);
void print_data3(const Conexus::Data d);

int main() {
  // Initialize the Conexus library
  // This is needed because we will use threaded servers and this
  // will do all the behind the scenes work to initialize pthreads
  Conexus::init();

  // Declare the tcp server object
  Conexus::IPv4::TCPServer::pointer tcp = Conexus::IPv4::TCPServer::create(1500);

  tcp->enable_reuse_address();

  // The server connect method connects a provided sigc++ slot that will be called
  // back when the server receives any data.
  tcp->signal_new_endpoint().connect(sigc::ptr_fun(&on_new_connection));

  // Start the server. The server will spawn a separate thread to service
  // received data, so this call will immediately return after the thread
  // is spawned.
  tcp->start();

  // Set up a loop that will run for 20 seconds and print the time every 5
  // seconds. Since the server is threaded, the sleep(1) call will not effect
  // the servicing thread.
  std::cout << "Starting..." << std::endl;
  for (int i=1; i <= 10; i++) {
    if (i%5 == 0)
      std::cout << "Time: " << i << std::endl;
    sleep(1);
  }

  // Stop the server and prepare for shutdown
  tcp->stop();

  return 0;
}

void on_new_connection(Conexus::Endpoint::pointer endpoint) {
  connections.push_back(endpoint);                // Add the endpoint to the connection list to prevent it from being destroyed
  endpoint->signal_data().connect(&print_data1);  // Connect the endpoint's data signal to the three callback functions
  endpoint->signal_data().connect(&print_data2);
  endpoint->signal_data().connect(&print_data3);
  
  // Connect the disconnect signal to the cleanup function
  endpoint->signal_disconnected().connect( sigc::bind( sigc::ptr_fun(on_disconnected), endpoint.get() ) );

  endpoint->start();                              // Start the endpoint running in a service thread
}

void on_disconnected(Conexus::Endpoint* endpoint)
{
  for ( Connections::iterator iter = connections.begin(); iter != connections.end(); iter++ )
  {
    if ( iter->get() == endpoint )
    {
      connections.erase(iter);
      break;
    }
  }
}

// These are the three callback functions. Each simply prints out the data received.

void print_data1(const Conexus::Data d) {
  std::cout << "<1> Received " << d.size();
  if (d) std::cout << " bytes of data [" << std::string(d, d.size()) << "]";
  std::cout << std::endl;
}

void print_data2(const Conexus::Data d) {
  std::cout << "<2> Received " << d.size();
  if (d) std::cout << " bytes of data [" << std::string(d, d.size()) << "]";
  std::cout << std::endl;
}

void print_data3(const Conexus::Data d) {
  std::cout << "<3> Received " << d.size();
  if (d) std::cout << " bytes of data [" << std::string(d, d.size()) << "]";
  std::cout << std::endl;
}

Generated on Wed Jul 8 15:50:07 2009 for conexus by doxygen 1.5.8