1997-06-03 13:54:55 +00:00
|
|
|
#ifndef __NETUTILS_H__
|
|
|
|
#define __NETUTILS_H__
|
|
|
|
|
|
|
|
#include "assoc.h"
|
|
|
|
|
|
|
|
class TConnection;
|
2002-09-13 14:56:23 +00:00
|
|
|
typedef unsigned long CONNID;
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
typedef int (*ConnectionFunction)(TConnection& conn, void* pJolly);
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
class TLanManager : public TObject
|
1997-06-03 13:54:55 +00:00
|
|
|
{
|
|
|
|
TAssoc_array m_mapConn;
|
|
|
|
|
|
|
|
protected:
|
2002-09-13 14:56:23 +00:00
|
|
|
CONNID AddConnection(TConnection* pConn);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual TConnection* OnCreateConnection(CONNID id);
|
|
|
|
virtual bool OnRemoveConnection(CONNID id);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
public:
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool IsServer() const { return FALSE; }
|
|
|
|
virtual bool IsClient() const { return FALSE; }
|
|
|
|
virtual bool IsOk() const { return FALSE; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool Execute(CONNID id, const char* cmd);
|
|
|
|
virtual bool Request(CONNID id, const char* cmd);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
int ForEachConnection(ConnectionFunction f, void* pJolly);
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
bool HasConnections() const;
|
|
|
|
TConnection* GetConnection(CONNID id) const;
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
bool RemoveConnection(CONNID id);
|
1997-06-03 13:54:55 +00:00
|
|
|
void RemoveAllConnections();
|
|
|
|
|
|
|
|
TLanManager();
|
|
|
|
virtual ~TLanManager();
|
|
|
|
};
|
|
|
|
|
|
|
|
class TLanServer : public TLanManager
|
|
|
|
{
|
2002-09-13 14:56:23 +00:00
|
|
|
TString m_strName;
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
public:
|
2002-09-13 14:56:23 +00:00
|
|
|
const TString& Name() const { return m_strName; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool IsServer() const { return TRUE; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual byte* GetBuffer(unsigned int dwSize) pure;
|
|
|
|
byte* GetBufferSetString(const char* str);
|
|
|
|
byte* GetBufferSetBool(bool ok);
|
|
|
|
byte* GetBufferSetInteger(long n);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
TLanServer(const char* name) : m_strName(name) { }
|
|
|
|
virtual ~TLanServer() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
class TLanClient : public TLanManager
|
|
|
|
{
|
|
|
|
protected:
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual TConnection* OnQueryConnection(const char* service, const char* server) pure;
|
|
|
|
|
1997-06-03 13:54:55 +00:00
|
|
|
public:
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool IsClient() const { return TRUE; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
CONNID QueryConnection(const char* service, const char* server = NULL);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool Execute(CONNID id, const char* cmd);
|
|
|
|
virtual bool Request(CONNID id, const char* cmd);
|
|
|
|
virtual byte* GetBuffer(unsigned int& dwSize) = 0;
|
1997-06-03 13:54:55 +00:00
|
|
|
virtual void ReleaseBuffer() = 0;
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
bool RequestString(CONNID id, const char* cmd, TString& res);
|
|
|
|
bool RequestInteger(CONNID id, const char* cmd, long& num);
|
|
|
|
bool RequestBool(CONNID id, const char* cmd, bool& ok);
|
1997-06-03 13:54:55 +00:00
|
|
|
};
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
class TConnection : public TObject
|
1997-06-03 13:54:55 +00:00
|
|
|
{
|
|
|
|
TLanManager* m_pManager;
|
2002-09-13 14:56:23 +00:00
|
|
|
CONNID m_dwId;
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
protected:
|
2002-09-13 14:56:23 +00:00
|
|
|
int ParseCommand(const char* cmd, TString_array& argv);
|
|
|
|
void SetId(CONNID id) { m_dwId = id; } // Internal use only
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
public:
|
2002-09-13 14:56:23 +00:00
|
|
|
CONNID Id() const { return m_dwId; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
|
|
|
TLanManager& Manager() const { return *m_pManager; }
|
|
|
|
TLanServer& Server() const;
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
virtual bool Execute(const char* cmd);
|
|
|
|
virtual bool Request(const char* cmd);
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
bool ReturnBool(bool b) const { return Server().GetBufferSetBool(b) != NULL; }
|
|
|
|
bool ReturnInteger(long n) const { return Server().GetBufferSetInteger(n) != NULL; }
|
|
|
|
bool ReturnString(const TString& s) const { return Server().GetBufferSetString(s) != NULL; }
|
1997-06-03 13:54:55 +00:00
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
TConnection(TLanManager* lm, CONNID id);
|
1997-06-03 13:54:55 +00:00
|
|
|
virtual ~TConnection();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|