<commit_before>/*
**  Copyright (C) 2012 <NAME>
**  See COPYING for the license
*/
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <qi/log.hpp>
#include <cerrno>

#include <boost/asio.hpp>

#include <boost/lexical_cast.hpp>

#include <qimessaging/transportserver.hpp>
#include <qimessaging/transportsocket.hpp>
#include "tcptransportsocket.hpp"

#include <qi/eventloop.hpp>

#include "transportserver_p.hpp"
#include "transportserverasio_p.hpp"

namespace qi
{

  void TransportServerAsioPrivate::onAccept(const boost::system::error_code& erc,
    boost::asio::ip::tcp::socket* s,
    boost::shared_ptr<bool> live)
  {
    qiLogDebug("qimessaging.server.listen") << this << " onAccept";
    if (!(*live))
    {
      delete s;
      return;
    }
    if (erc)
    {
      qiLogDebug("qimessaging.server.listen") << "accept error " << erc.message();
      delete s;
      self->acceptError(erc.value());
      return;
    }
    qi::TransportSocketPtr socket = qi::TcpTransportSocketPtr(new TcpTransportSocket(s, context));
    self->newConnection(socket);
    s = new boost::asio::ip::tcp::socket(_acceptor.get_io_service());
    _acceptor.async_accept(*s,
      boost::bind(&TransportServerAsioPrivate::onAccept, this, _1, s, live));
  }

  void TransportServerAsioPrivate::close() {
    qiLogDebug("qimessaging.server.listen") << this << " close";
    *_live = false;
    _acceptor.close();
  }

  bool TransportServerAsioPrivate::listen()
  {
    using namespace boost::asio;
    // resolve endpoint
    ip::tcp::resolver r(_acceptor.get_io_service());
    ip::tcp::resolver::query q(listenUrl.host(), boost::lexical_cast<std::string>(listenUrl.port()));
    ip::tcp::resolver::iterator it = r.resolve(q);
    if (it == ip::tcp::resolver::iterator())
    {
      qiLogError("qimessaging.server.listen") << "Listen error: no endpoint.";
      return false;
    }
    ip::tcp::endpoint ep = *it;
    qiLogDebug("qimessaging.server.listen") <<"Will listen on " << ep.address().to_string() << ' ' << ep.port();
    _acceptor.open(ep.protocol());
    _acceptor.bind(ep);
    _acceptor.listen();
    unsigned port = _acceptor.local_endpoint().port();// already in host byte orde
    qiLogDebug("qimessaging.server.listen") << "Effective port io_service" << port;<commit_msg>TransportServer: Always set SO_REUSEADDR.<commit_after>/*
**  Copyright (C) 2012 Aldebaran Robotics
**  See COPYING for the license
*/
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <qi/log.hpp>
#include <cerrno>

#include <boost/asio.hpp>

#include <boost/lexical_cast.hpp>

#include <qimessaging/transportserver.hpp>
#include <qimessaging/transportsocket.hpp>
#include "tcptransportsocket.hpp"

#include <qi/eventloop.hpp>

#include "transportserver_p.hpp"
#include "transportserverasio_p.hpp"

namespace qi
{

  void TransportServerAsioPrivate::onAccept(const boost::system::error_code& erc,
    boost::asio::ip::tcp::socket* s,
    boost::shared_ptr<bool> live)
  {
    qiLogDebug("qimessaging.server.listen") << this << " onAccept";
    if (!(*live))
    {
      delete s;
      return;
    }
    if (erc)
    {
      qiLogDebug("qimessaging.server.listen") << "accept error " << erc.message();
      delete s;
      self->acceptError(erc.value());
      return;
    }
    qi::TransportSocketPtr socket = qi::TcpTransportSocketPtr(new TcpTransportSocket(s, context));
    self->newConnection(socket);
    s = new boost::asio::ip::tcp::socket(_acceptor.get_io_service());
    _acceptor.async_accept(*s,
      boost::bind(&TransportServerAsioPrivate::onAccept, this, _1, s, live));
  }

  void TransportServerAsioPrivate::close() {
    qiLogDebug("qimessaging.server.listen") << this << " close";
    *_live = false;
    _acceptor.close();
  }

  bool TransportServerAsioPrivate::listen()
  {
    using namespace boost::asio;
    // resolve endpoint
    ip::tcp::resolver r(_acceptor.get_io_service());
    ip::tcp::resolver::query q(listenUrl.host(), boost::lexical_cast<std::string>(listenUrl.port()));
    ip::tcp::resolver::iterator it = r.resolve(q);
    if (it == ip::tcp::resolver::iterator())
    {
      qiLogError("qimessaging.server.listen") << "Listen error: no endpoint.";
      return false;
    }
    ip::tcp::endpoint ep = *it;
    qiLogDebug("qimessaging.server.listen") <<"Will listen on " << ep.address().to_string() << ' ' << ep.port();
    _acceptor.open(ep.protocol());
    boost::asio::socket_base::reuse_address option(true);
    _acceptor.set_option(option);
    _acceptor.bind(ep);
    _acceptor.listen();
    unsigned port = _acceptor.local_endpoint().port();// already in host byte orde
    qiLogDebug("qimessaging.server.listen") << "Effective port io_service" << port;
{'fraction_non_alphanumeric': 0.16380753138075313, 'fraction_numerical': 0.0020920502092050207, 'mean_word_length': 4.347874720357942, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4096630', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1587, 'n_words': 288}