#pragma once
#include "AftrConfig.h"
#ifdef AFTR_CONFIG_USE_BOOST
#include <boost/asio.hpp>
#include "NetMessengerMessageQueue.h"

namespace Aftr
{

class NetMessengerServerSession;
class NetMessengerServerUDPConn;

class NetMessengerServerListener
{
public:
   NetMessengerServerListener( boost::asio::io_context& io_context, unsigned short port );
   ~NetMessengerServerListener();

   void handle_accept();

   NetMessengerMessageQueue& getNewlyArrivedMsgs();
   NetMessengerMessageQueue* getRecvMsgQueue() noexcept { return &this->newlyArrivedMsgs; }

   unsigned short getUDPListenerPort() const;
   unsigned short getTCPListenerPort() const;

protected:
   boost::asio::io_context& ioService;
   boost::asio::ip::tcp::acceptor acceptor;

   //This represents the UDP:PORT endpoint this Aftr instance is listening on for all UDP Traffic on this UDP:PORT.
   //TCP ends up creating a new TCP Session (NetMessengerServerSession) for each TCP connection.
   std::shared_ptr<NetMessengerServerUDPConn> udpListener; ///< At any time there is always exactly 1 unstarted connection 
                                                        ///< waiting to be started when a new connection is formed.
                                                        ///< On destruction, this must be deleted to avoid leaks.


   //Many different Sessions could insert their newly arrived msgs into this "global" network queue. Optionally,
   //the c'tors for other NetMessenegerServerSession (TCP) or NetMessenegerServerUDPConn (UDP) can specify which
   //queue they would like to populate when messages arrive on those connections.
   NetMessengerMessageQueue newlyArrivedMsgs; //sln, could cause lifetime issues (crash on exit), if NetMessengerServerSession lives longer than listener
};

} //namespace Aftr

#endif //boost
