<commit_before>/* GNE - Game Networking Engine, a portable multithreaded networking library.
 * Copyright (C) 2001 <NAME> (<EMAIL>)
 * Project website: http://www.rit.edu/~jpw9607/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "../include/gnelib/gneintern.h"
#include "../include/gnelib/ServerConnection.h"
#include "../include/gnelib/ConnectionListener.h"
#include "../include/gnelib/Connection.h"
#include "../include/gnelib/ConnectionParams.h"
#include "../include/gnelib/SyncConnection.h"
#include "../include/gnelib/ServerConnectionListener.h"
#include "../include/gnelib/Address.h"
#include "../include/gnelib/EventThread.h"
#include "../include/gnelib/RawPacket.h"
#include "../include/gnelib/Error.h"
#include "../include/gnelib/Errors.h"
#include "../include/gnelib/SocketPair.h"
#include "../include/gnelib/GNE.h"

namespace GNE {

/**
 * Simply just a class to temporarily hold connection parameters until the
 * ClientConnection is connected, then it is useless.
 */
class ServerConnectionParams {
public:
  ConnectionParams cp;
  ServerConnectionListener::sptr creator;
  bool doJoin;
};

ServerConnection::ServerConnection()
: Thread("SrvrConn", Thread::HIGH_PRI) {
  gnedbgo(5, "created");
}

void ServerConnection::init( const ConnectionParams& p,
                        NLsocket rsocket2,
                        const SmartPtr<ServerConnectionListener>& creator ) {
  setType( CONNECTION );
  assert(!p);
  sockets.r = rsocket2;
  setListener(p.getListener());
  setTimeout(p.getTimeout());
  params = new ServerConnectionParams;
  params->cp = p;
  params->creator = creator;
  params->doJoin = true;
}

ServerConnection::sptr
ServerConnection::create(const ConnectionParams& p, NLsocket rsocket,
                         const SmartPtr<ServerConnectionListener>& creator) {
  sptr ret( new ServerConnection() );
  ret->Thread::setThisPointer( ret );
  ret->Connection::setThisPointer( ret );
  ret->init( p, rsocket, creator );
  return ret;
}

ServerConnection::~ServerConnection() {
  delete params;
  gnedbgo(5, "destroyed");
}

/**
 * \bug if GNE shuts down while connecting... well that's just bad news.  This
 *      thread may not connect and end in time, causing undefined behavior.
 * \bug The PacketFeeder is set after onNewConn, so it is possible that if
 *      the user sets a different PacketFeeder right after connecting, it
 *      might "get lost" and if set during onNewConn, is definitely lost.
 */
void ServerConnection::run() {
  assert(sockets.r != NL_INVALID);
  assert(getListener());
  //endConnect will set the null listener to discard the events, so we
  //have to cache the current listener.
  ConnectionListener::sptr origListener = getListener();

  //We cache the remote address because after an error occurs we may not be
  //able to get it to pass to the listen failure function.
  Address rAddr = getRemoteAddress(true);
  gnedbgo1(1, "New connection incoming from %s", rAddr.toString().c_str());

  //Do the GNE protocol handshake.
  try {
    doHandshake();
  } catch (Error& e) {
    params->creator->onListenFailure(e, rAddr, origListener);
    //We'll get deleted from the SmartPtr cleanup
    return;
  }
  gnedbgo(2, "GNE Protocol Handshake Successful.");

  //Start up the connecting SyncConnection and start the onNewConn event.
  bool onNewConnFinished = false;
  SyncConnection::sptr sConnPtr = SyncConnection::create( this_.lock() );
  SyncConnection& sConn = *sConnPtr;
  try {
    sConn.startConnect();
    ps->start();
    connecting = true;
    startEventThread();
    reg(true, (sockets.u != NL_INVALID));

    //Setup the packet feeder
    ps->setFeederTimeout( params->cp.getFeederTimeout() );
    ps->setLowPacketThreshold( params->cp.getLowPacketThreshold() );

    gnedbgo2(2, "Starting onNewConn r: %i, u: %i", sockets.r, sockets.u);
    getListener()->onNewConn(sConn); //SyncConnection will relay this
    onNewConnFinished = true;

    params->creator->onListenSuccess(origListener);

    finishedConnecting();<commit_msg>removed redundant test on reg.<commit_after>/* GNE - Game Networking Engine, a portable multithreaded networking library.
 * Copyright (C) 2001 <NAME> (<EMAIL>)
 * Project website: http://www.rit.edu/~jpw9607/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "../include/gnelib/gneintern.h"
#include "../include/gnelib/ServerConnection.h"
#include "../include/gnelib/ConnectionListener.h"
#include "../include/gnelib/Connection.h"
#include "../include/gnelib/ConnectionParams.h"
#include "../include/gnelib/SyncConnection.h"
#include "../include/gnelib/ServerConnectionListener.h"
#include "../include/gnelib/Address.h"
#include "../include/gnelib/EventThread.h"
#include "../include/gnelib/RawPacket.h"
#include "../include/gnelib/Error.h"
#include "../include/gnelib/Errors.h"
#include "../include/gnelib/SocketPair.h"
#include "../include/gnelib/GNE.h"

namespace GNE {

/**
 * Simply just a class to temporarily hold connection parameters until the
 * ClientConnection is connected, then it is useless.
 */
class ServerConnectionParams {
public:
  ConnectionParams cp;
  ServerConnectionListener::sptr creator;
  bool doJoin;
};

ServerConnection::ServerConnection()
: Thread("SrvrConn", Thread::HIGH_PRI) {
  gnedbgo(5, "created");
}

void ServerConnection::init( const ConnectionParams& p,
                        NLsocket rsocket2,
                        const SmartPtr<ServerConnectionListener>& creator ) {
  setType( CONNECTION );
  assert(!p);
  sockets.r = rsocket2;
  setListener(p.getListener());
  setTimeout(p.getTimeout());
  params = new ServerConnectionParams;
  params->cp = p;
  params->creator = creator;
  params->doJoin = true;
}

ServerConnection::sptr
ServerConnection::create(const ConnectionParams& p, NLsocket rsocket,
                         const SmartPtr<ServerConnectionListener>& creator) {
  sptr ret( new ServerConnection() );
  ret->Thread::setThisPointer( ret );
  ret->Connection::setThisPointer( ret );
  ret->init( p, rsocket, creator );
  return ret;
}

ServerConnection::~ServerConnection() {
  delete params;
  gnedbgo(5, "destroyed");
}

/**
 * \bug if GNE shuts down while connecting... well that's just bad news.  This
 *      thread may not connect and end in time, causing undefined behavior.
 * \bug The PacketFeeder is set after onNewConn, so it is possible that if
 *      the user sets a different PacketFeeder right after connecting, it
 *      might "get lost" and if set during onNewConn, is definitely lost.
 */
void ServerConnection::run() {
  assert(sockets.r != NL_INVALID);
  assert(getListener());
  //endConnect will set the null listener to discard the events, so we
  //have to cache the current listener.
  ConnectionListener::sptr origListener = getListener();

  //We cache the remote address because after an error occurs we may not be
  //able to get it to pass to the listen failure function.
  Address rAddr = getRemoteAddress(true);
  gnedbgo1(1, "New connection incoming from %s", rAddr.toString().c_str());

  //Do the GNE protocol handshake.
  try {
    doHandshake();
  } catch (Error& e) {
    params->creator->onListenFailure(e, rAddr, origListener);
    //We'll get deleted from the SmartPtr cleanup
    return;
  }
  gnedbgo(2, "GNE Protocol Handshake Successful.");

  //Start up the connecting SyncConnection and start the onNewConn event.
  bool onNewConnFinished = false;
  SyncConnection::sptr sConnPtr = SyncConnection::create( this_.lock() );
  SyncConnection& sConn = *sConnPtr;
  try {
    sConn.startConnect();
    ps->start();
    connecting = true;
    startEventThread();
    reg(true, true);

    //Setup the packet feeder
    ps->setFeederTimeout( params->cp.getFeederTimeout() );
    ps->setLowPacketThreshold( params->cp.getLowPacketThreshold() );

    gnedbgo2(2, "Starting onNewConn r: %i, u: %i", sockets.r, sockets.u);
    getListener()->onNewConn(sConn); //SyncConnection will relay this
    onNewConnFinished = true;

    params->creator->onListenSuccess(origListener);

    finishedConnecting();
{'fraction_non_alphanumeric': 0.114910790144435, 'fraction_numerical': 0.007009345794392523, 'mean_word_length': 3.8767477990678403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5502046', 'n_tokens_mistral': 2775, 'n_tokens_neox': 2701, 'n_words': 902}