BaseServerUDP

juhgiyo edited this page Nov 14, 2012 · 4 revisions

BaseServerUDP Usage

Linking

// SomeServerUDP.h
#include "epse.h"
using namespace epl;
using namespace epse;

Server Declaration Example

class SomeServerUDP : public BaseServerUDP
{
public:
   SomeServerUDP(const TCHAR * port=_T("1234"));
   SomeServerUDP(const SomeServerUDP b);
   virtual ~SomeServerUDP();
   SomeServerUDP & operator=(const SomeServerUDP&b)
   {
      if(this!=&b)
      {
         BaseServerUDP::operator =(b);
      }
      return *this;
   }
protected:
   virtual BaseServerWorkerUDP* createNewWorker();
};

Server Implementation Example

#include "SomeServerUDP.h"
#include "SomeServerWorkerUDP.h"
SomeServerUDP::SomeServerUDP(const TCHAR * port) :BaseServerUDP(port)
{
}

SomeServerUDP::SomeServerUDP(const SomeServerUDP& b):BaseServerUDP(b)
{
}

SomeServerUDP::~SomeServerUDP()
{
}

BaseServerWorkerUDP* SomeServerUDP::createNewWorker()
{
   // create new worker that is related to SomeServerUDP and return the pointer
   // this will automatically deleted when Server stops or Client disconnects
   return new SomeServerWorkerUDP();
}

Server Start/Stop Example

#include "SomeServerUDP.h"
SomeServerUDP m_server=SomeServerUDP(_T("1020"));
...
if(m_server.StartServer())
{
   // server started
}
else
{
   // server failed starting
}

...
if(m_server.IsServerStarted()) // check if server is started
{
   m_server.StopServer();  // Disconnect all clients connected and stop listening
}
  • * Note that a server object creates one "listening thread", and for each client connected, the "listening thread" creates a thread (BaseServerWorkerUDP object) to communicate with the client.

Server Broadcast Example

#include "SomeServerUDP.h"
SomeServerUDP m_server=SomeServerUDP(_T("1020"));
...
// Server Starts
// See above example
...
if(m_server.IsServerStarted()) // check if server is started
{
    PacketContainer<SendPacketType> packetContainer=PacketContainer<SendPacketType>();
    packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DIDSOMETHING;
    Packet sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
    m_server.Broadcast(sendPacket);
}
  • * Note that the packet byte size, broadcasting, must NOT be greater than size returned from GetMaxPacketByteSize().
  • * For the details of PacketContainer class, see PacketContainer
  • * For the details of Packet class, see Packet

Server ShutdownAllClient Example

#include "SomeServerUDP.h"
SomeServerUDP m_server=SomeServerUDP(_T("1020"));
...
// Server Starts
// See above example
...
if(m_server.IsServerStarted()) // check if server is started
{
   ShutdownAllClient(); // Disconnects all clients connected.
}
  • This disconnects all clients, connected, from the server.