Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    421065

Pointless entry #3

Sign in to follow this  
Evil Steve

70 views

Yay, I'm "Done" with my network code. As in, it works and I don't think I need any more functionality immediately. So, for hilarity, have some headers.

ENetworkClient.h:

//============================================================================
// ENetworkClient.h - Network client. Handles one connection
//============================================================================

#ifndef __ENETWORKCLIENT_H__
#define __ENETWORKCLIENT_H__

#include "ESP/ESP.h"
#include "EIP.h"
#include

class ENetworkClient : public ERefCounted
{
public:
typedef ESP SP;

ENetworkClient() : m_nLocalPort(0), m_nRemotePort(0) {}
virtual ~ENetworkClient() {}

//========================================================================

// Return the number of bytes pending in the read buffer
virtual size_t GetBufferSize() const = 0;

// Returns if this client is [still] connected
virtual bool IsConnected() const = 0;

// Extract some bytes from the read buffer. Returns the number of bytes
// extracted.
virtual size_t ExtractBuffer(void* pBuffer, size_t nNumBytes) = 0;

// Send data to a client
virtual void Send(const void* pBuffer, size_t nNumBytes) = 0;
void Send(const std::string& str) { Send(str.c_str(), str.length()); }
void Send(const std::wstring& str) { Send(str.c_str(), str.length()*2); }

//========================================================================

// Return connection info
const EIP& GetRemoteIP() const { return m_ip; }
unsigned short GetRemotePort() const { return m_nRemotePort; }
unsigned short GetLocalPort() const { return m_nLocalPort; }

protected:
EIP m_ip;
unsigned short m_nLocalPort;
unsigned short m_nRemotePort;
};

#endif // __ENETWORKCLIENT_H__



ENetworkServer.h:

//============================================================================
// ENetworkServer.h - Network server class. Handles network connections on
// one port
//============================================================================

#ifndef __ENETWORKSERVER_H__
#define __ENETWORKSERVER_H__

#include "ESP/ESP.h"
#include "ENetworkClient.h"

class ENetworkServer : public ERefCounted
{
public:
typedef ESP SP;

ENetworkServer() {}
virtual ~ENetworkServer() {}

//========================================================================

// Get the next pending connection. Will return null if no connections pending
virtual ENetworkClient::SP GetNextConnection() = 0;
};

#endif // __ENETWORKSERVER_H__



ENetworkMgr.h:

//============================================================================
// ENetworkMgr.h - Network manager class
//============================================================================

#ifndef __ENETWORKMGR_H__
#define __ENETWORKMGR_H__

#include "ENetworkServer.h"

class ENetworkMgr
{
public:
// Protocol for network connections
enum Protocol
{
PROTO_TCP,
PROTO_UDP
};

ENetworkMgr() {}
virtual ~ENetworkMgr() {}

// Singleton access
static ENetworkMgr& Get() { return *ms_pInstance; }
static void Destroy() { delete ms_pInstance; ms_pInstance=0; }

//========================================================================

// Start a server listening on a specified port
virtual ENetworkServer::SP CreateServer(unsigned short nPort, Protocol eProto) = 0;

// Perform one update
virtual void Tick() = 0;

protected:
static ENetworkMgr* ms_pInstance;
};

#endif // __ENETWORKMGR_H__



And finally, EIP.h:

//============================================================================
// EIP.h - IP address wrapper class
//============================================================================

#ifndef __EIP_H__
#define __EIP_H__

#include

class EIP
{
public:
EIP() { m_ip.nIP=0; }
~EIP() {}

//========================================================================

// Get / set raw IP address
unsigned int GetIP() const { return m_ip.nIP; }
void SetIP(unsigned int nIP) { m_ip.nIP = nIP; }

// Get IP address as a XXX.XXX.XXX.XXX string
std::wstring ToString() const;

//========================================================================

private:
struct IPQuad
{
unsigned char a, b, c, d;
};

union {
IPQuad ip;
unsigned int nIP;
} m_ip;
};

#endif // __EIP_H__


As the title says, pointless. But it works anyway. The ESP and ERefCounted stuff is my smart pointer code before you ask. And now, I'm going for an early night.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now