TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::NetListener Class Reference

#include <NetworkDevice.h>

Public Member Functions

 ~NetListener ()
 
ReliableConduitRef waitForConnection ()
 
bool clientWaiting () const
 
bool ok () const
 
- Public Member Functions inherited from G3D::ReferenceCountedObject
virtual ~ReferenceCountedObject ()
 

Static Public Member Functions

static NetListenerRef create (const uint16 port)
 

Private Member Functions

 NetListener (uint16 port)
 

Private Attributes

SOCKET sock
 

Friends

class NetworkDevice
 

Detailed Description

Runs on the server listening for clients trying to make reliable connections.

Deprecated:

Constructor & Destructor Documentation

G3D::NetListener::NetListener ( uint16  port)
private

Port is in host byte order.

1151  {
1153 
1154  // Start the listener socket
1155  Log::common()->print("Creating a listener ");
1156  sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
1157 
1158  if (sock == SOCKET_ERROR) {
1159  Log::common()->printf("FAIL");
1161  return;
1162  }
1163  Log::common()->println("Ok");
1164 
1165  const int T = true;
1166 
1167  // Set reuse address so that a new server can start up soon after
1168  // an old one has closed.
1169  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1170  (const char*)&T, sizeof(T)) == SOCKET_ERROR) {
1171 
1172  Log::common()->println("WARNING: Setting socket reuseaddr failed.");
1174  } else {
1175  Log::common()->println("Set socket option reuseaddr.");
1176  }
1177 
1178 
1179  if (! nd->bind(sock, NetAddress(0, port))) {
1180  Log::common()->printf("Unable to bind!\n");
1181  nd->closesocket(sock);
1182  sock = (SOCKET)SOCKET_ERROR;
1183  return;
1184  }
1185 
1186  Log::common()->printf("Listening on port %5d ", port);
1187 
1188  // listen is supposed to return 0 when there is no error.
1189  // The 2nd argument is the number of connections to allow pending
1190  // at any time.
1191  int L = listen(sock, 100);
1192  if (L == SOCKET_ERROR) {
1193  Log::common()->println("FAIL");
1195  nd->closesocket(sock);
1196  sock = (SOCKET)SOCKET_ERROR;
1197  return;
1198  }
1199  Log::common()->println("Ok");
1200  Log::common()->printf("Now listening on socket %d.\n\n", sock);
1201 }
SOCKET sock
Definition: NetworkDevice.h:552
static std::string socketErrorCode(int code)
Definition: networkHelpers.h:64
void print(const std::string &s)
Definition: Log.cpp:138
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
friend class NetworkDevice
Definition: NetworkDevice.h:550
static Log * common()
Definition: Log.cpp:100
void println(const std::string &s)
Definition: Log.cpp:144
#define SOCKET
Definition: netheaders.h:20
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

G3D::NetListener::~NetListener ( )
1204  {
1206  nd->closesocket(sock);
1207 }
SOCKET sock
Definition: NetworkDevice.h:552
friend class NetworkDevice
Definition: NetworkDevice.h:550
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109

+ Here is the call graph for this function:

Member Function Documentation

bool G3D::NetListener::clientWaiting ( ) const

True if a client is waiting (i.e. waitForConnection will return immediately).

1248  {
1249  return readWaiting(sock);
1250 }
SOCKET sock
Definition: NetworkDevice.h:552
static bool readWaiting(const SOCKET &sock)
Definition: NetworkDevice.cpp:72

+ Here is the call graph for this function:

NetListenerRef G3D::NetListener::create ( const uint16  port)
static
1146  {
1147  return NetListenerRef(new NetListener(port));
1148 }
shared_ptr< class NetListener > NetListenerRef
Definition: NetworkDevice.h:540
NetListener(uint16 port)
Definition: NetworkDevice.cpp:1151

+ Here is the call graph for this function:

bool G3D::NetListener::ok ( ) const
1243  {
1244  return (sock != 0) && (sock != SOCKET_ERROR);
1245 }
SOCKET sock
Definition: NetworkDevice.h:552
ReliableConduitRef G3D::NetListener::waitForConnection ( )

Block until a connection is received. Returns NULL if something went wrong.

1210  {
1212  // The address of the connecting host
1213  SOCKADDR_IN remote_addr;
1214  int iAddrLen = sizeof(remote_addr);
1215 
1216  Log::common()->println("Blocking in NetListener::waitForConnection().");
1217 
1218  SOCKET sClient = accept(sock, (struct sockaddr*) &remote_addr,
1219  (socklen_t*)&iAddrLen);
1220 
1221  if (sClient == SOCKET_ERROR) {
1222  Log::common()->println("Error in NetListener::acceptConnection.");
1224  nd->closesocket(sock);
1225  return ReliableConduitRef();
1226  }
1227 
1228  Log::common()->printf("%s connected, transferred to socket %d.\n",
1229  inet_ntoa(remote_addr.sin_addr), sClient);
1230 
1231  #ifndef G3D_WINDOWS
1232  return ReliableConduitRef(new ReliableConduit(sClient,
1233  NetAddress(htonl(remote_addr.sin_addr.s_addr),
1234  ntohs(remote_addr.sin_port))));
1235  #else
1236  return ReliableConduitRef(ReliableConduitRef(new ReliableConduit(sClient,
1237  NetAddress(ntohl(remote_addr.sin_addr.S_un.S_addr),
1238  ntohs(remote_addr.sin_port)))));
1239  #endif
1240 }
SOCKET sock
Definition: NetworkDevice.h:552
static std::string socketErrorCode(int code)
Definition: networkHelpers.h:64
#define SOCKADDR_IN
Definition: netheaders.h:17
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
friend class NetworkDevice
Definition: NetworkDevice.h:550
shared_ptr< class ReliableConduit > ReliableConduitRef
Definition: NetworkDevice.h:136
static Log * common()
Definition: Log.cpp:100
void println(const std::string &s)
Definition: Log.cpp:144
#define SOCKET
Definition: netheaders.h:20
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109

+ Here is the call graph for this function:

Friends And Related Function Documentation

friend class NetworkDevice
friend

Member Data Documentation

SOCKET G3D::NetListener::sock
private

The documentation for this class was generated from the following files: