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

Abstraction of network (socket) functionality. More...

#include <NetworkDevice.h>

Classes

class  EthernetAdapter
 Description of an ethernet or wireless ethernet adapter. More...
 

Public Member Functions

 ~NetworkDevice ()
 
const Array< EthernetAdapter > & adapterArray () const
 
const Array< uint32 > & broadcastAddressArray () const
 
void describeSystem (TextOutput &t)
 
void describeSystem (std::string &s)
 
std::string localHostName () const
 
void localHostAddresses (Array< NetAddress > &array) const
 

Static Public Member Functions

static std::string formatIP (uint32 ip)
 
static std::string formatMAC (const uint8 mac[6])
 
static NetworkDeviceinstance ()
 
static void cleanup ()
 

Private Member Functions

void closesocket (SOCKET &sock) const
 
bool bind (SOCKET sock, const NetAddress &addr) const
 
 NetworkDevice ()
 
bool init ()
 
void _cleanup ()
 
void addAdapter (const EthernetAdapter &a)
 

Private Attributes

bool initialized
 
Array< EthernetAdapterm_adapterArray
 
Array< uint32m_broadcastAddresses
 

Static Private Attributes

static NetworkDevices_instance = NULL
 

Friends

class Conduit
 
class LightweightConduit
 
class ReliableConduit
 
class NetListener
 

Detailed Description

Abstraction of network (socket) functionality.

An abstraction over sockets that provides a message-based network infrastructure optimized for sending many small (~500 bytes) messages. All functions always return immediately.

Create only one NetworkDevice per process (a WinSock restriction).

NetworkDevice is technically not thread safe. However, as long as you use different conduits on different threads (or lock conduits before sending), you will encounter no problems sharing the single NetworkDevice across multiple threads. That is, do not invoke the same Conduit's send or receive method on two threads at once.

This assumes that the underlying WinSock/BSD sockets implementation is thread safe. That is not guaranteed, but in practice seems to always be true (see http://tangentsoft.net/wskfaq/intermediate.html#threadsafety)


IP networks use "network byte order" (big-endian) for communicating integers. "Host byte order" is the endian-ness of the local machine (typically little-endian; see System::endian). The C functions htonl() and ntohl() convert 32-bit values between these formats. G3D only ever exposes host byte order, so programmers rarely need to be aware of the distinction.

Deprecated:

Constructor & Destructor Documentation

G3D::NetworkDevice::NetworkDevice ( )
private
130  {
131  initialized = false;
132 }
bool initialized
Definition: NetworkDevice.h:645

+ Here is the caller graph for this function:

G3D::NetworkDevice::~NetworkDevice ( )
135  {
136 }

Member Function Documentation

void G3D::NetworkDevice::_cleanup ( )
private
475  {
477 
478 # ifdef G3D_WINDOWS
479  // Now handled through enet
480 // WSACleanup();
481 # endif
482 }
bool initialized
Definition: NetworkDevice.h:645
#define debugAssert(exp)
Definition: debugAssert.h:160

+ Here is the caller graph for this function:

const Array<EthernetAdapter>& G3D::NetworkDevice::adapterArray ( ) const
inline

Returns the available ethernet adapters for the current machine that are online. Does not include the loopback adapter for localhost.

686  {
687  return m_adapterArray;
688  }
Array< EthernetAdapter > m_adapterArray
Definition: NetworkDevice.h:647
void G3D::NetworkDevice::addAdapter ( const EthernetAdapter a)
private

Called from init to update m_adapterArray and m_broadcastAddresses.

223  {
224  m_adapterArray.append(a);
225  if (a.broadcast != 0) {
226  int i = m_broadcastAddresses.findIndex(a.broadcast);
227  if (i == -1) {
228  m_broadcastAddresses.append(a.broadcast);
229  }
230  }
231 }
Array< EthernetAdapter > m_adapterArray
Definition: NetworkDevice.h:647
Array< uint32 > m_broadcastAddresses
Definition: NetworkDevice.h:651
int findIndex(const T &value) const
Definition: Array.h:1009
void append(const T &value)
Definition: Array.h:583

+ Here is the call graph for this function:

bool G3D::NetworkDevice::bind ( SOCKET  sock,
const NetAddress addr 
) const
private

Utility method. Returns true on success.

484  {
485  Log::common()->printf("Binding socket %d on port %d ",
486  sock, htons(addr.addr.sin_port));
487  if (::bind(sock, (struct sockaddr*)&(addr.addr), sizeof(addr.addr)) ==
488  SOCKET_ERROR) {
489 
490  Log::common()->println("FAIL");
492  closesocket(sock);
493  return false;
494  }
495 
496  Log::common()->println("Ok");
497  return true;
498 }
bool bind(SOCKET sock, const NetAddress &addr) const
Definition: NetworkDevice.cpp:484
void closesocket(SOCKET &sock) const
Definition: NetworkDevice.cpp:501
static std::string socketErrorCode(int code)
Definition: networkHelpers.h:64
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
static Log * common()
Definition: Log.cpp:100
void println(const std::string &s)
Definition: Log.cpp:144

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const Array<uint32>& G3D::NetworkDevice::broadcastAddressArray ( ) const
inline

Returns the (unique) IP addresses for UDP broadcasting extracted from adapterArray(). All are in host byte order.

692  {
693  return m_broadcastAddresses;
694  }
Array< uint32 > m_broadcastAddresses
Definition: NetworkDevice.h:651
void G3D::NetworkDevice::cleanup ( )
static

Shuts down the network device (destroying the global instance).

121  {
122  if (s_instance) {
123  s_instance->_cleanup();
124  delete s_instance;
125  s_instance = NULL;
126  }
127 }
arena_t NULL
Definition: jemalloc_internal.h:624
static NetworkDevice * s_instance
Definition: NetworkDevice.h:660
void _cleanup()
Definition: NetworkDevice.cpp:475

+ Here is the call graph for this function:

void G3D::NetworkDevice::closesocket ( SOCKET sock) const
private

Utility method.

501  {
502  if (sock != 0) {
503  #ifdef G3D_WINDOWS
504  ::closesocket(sock);
505  #else
506  close(sock);
507  #endif
508 
509  Log::common()->printf("Closed socket %d\n", sock);
510  sock = 0;
511  }
512 }
void closesocket(SOCKET &sock) const
Definition: NetworkDevice.cpp:501
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
static Log * common()
Definition: Log.cpp:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::NetworkDevice::describeSystem ( TextOutput t)

Prints a human-readable description of this machine to the text output stream.

1255  {
1256 
1257  t.writeSymbols("Network", "=", "{");
1258  t.writeNewline();
1259  t.pushIndent();
1260 
1261  for (int i = 0; i < m_adapterArray.size(); ++i) {
1262  t.printf("Adapter%d =", i);
1263  m_adapterArray[i].describe(t);
1264  }
1265 
1266  t.popIndent();
1267  t.writeSymbols("};");
1268  t.writeNewline();
1269  t.writeNewline();
1270 }
Array< EthernetAdapter > m_adapterArray
Definition: NetworkDevice.h:647

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::NetworkDevice::describeSystem ( std::string &  s)
1274  {
1275 
1276  TextOutput t;
1277  describeSystem(t);
1278  t.commitString(s);
1279 }
void describeSystem(TextOutput &t)
Definition: NetworkDevice.cpp:1254

+ Here is the call graph for this function:

std::string G3D::NetworkDevice::formatIP ( uint32  ip)
static

Prints an IP address to a string.

Parameters
ipIn host byte order.
234  {
235  return format("%3d.%3d.%3d.%3d", (addr >> 24) & 0xFF, (addr >> 16) & 0xFF,
236  (addr >> 8) & 0xFF, addr & 0xFF);
237 }
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string G3D::NetworkDevice::formatMAC ( const uint8  mac[6])
static

Prints a MAC address to a string.

240  {
241  return format("%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
242 }
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::NetworkDevice::init ( )
private

+ Here is the caller graph for this function:

NetworkDevice * G3D::NetworkDevice::instance ( )
static

Returns NULL if there was a problem initializing the network.

109  {
110  if (s_instance == NULL) {
111  s_instance = new NetworkDevice();
112  if (! s_instance->init()) {
113  delete s_instance;
114  s_instance = NULL;
115  }
116  }
117  return s_instance;
118 }
arena_t NULL
Definition: jemalloc_internal.h:624
static NetworkDevice * s_instance
Definition: NetworkDevice.h:660
NetworkDevice()
Definition: NetworkDevice.cpp:130

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::NetworkDevice::localHostAddresses ( Array< NetAddress > &  array) const

There is often more than one address for the local host. This returns all of them.

Deprecated:
Use adapterArray()
515  {
516  array.resize(0);
517 
518  char ac[256];
519 
520  if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
521  Log::common()->printf("Error while getting local host name\n");
522  return;
523  }
524 
525  struct hostent* phe = gethostbyname(ac);
526  if (phe == 0) {
527  Log::common()->printf("Error while getting local host address\n");
528  return;
529  }
530 
531  for (int i = 0; (phe->h_addr_list[i] != 0); ++i) {
532  struct in_addr addr;
533  memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
534  array.append(NetAddress(addr));
535  }
536 }
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
static Log * common()
Definition: Log.cpp:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string G3D::NetworkDevice::localHostName ( ) const

Returns the name (or one of the names) of this computer

139  {
140  char ac[128];
141  if (gethostname(ac, sizeof(ac)) == -1) {
142  Log::common()->printf("Error while getting local host name\n");
143  return "localhost";
144  }
145  return gethostbyname(ac)->h_name;
146 }
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
static Log * common()
Definition: Log.cpp:100

+ Here is the call graph for this function:

Friends And Related Function Documentation

friend class Conduit
friend
friend class LightweightConduit
friend
friend class NetListener
friend
friend class ReliableConduit
friend

Member Data Documentation

bool G3D::NetworkDevice::initialized
private
Array<EthernetAdapter> G3D::NetworkDevice::m_adapterArray
private
Array<uint32> G3D::NetworkDevice::m_broadcastAddresses
private

Broadcast addresses available on this machine, extracted from m_adapterArray.

NetworkDevice * G3D::NetworkDevice::s_instance = NULL
staticprivate

The global instance


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