00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#ifndef GM_NETWORK_H
00025
#define GM_NETWORK_H
00026
00027
#include <string>
00028
00029
#include "cryptopp/misc.h"
00030
00031
#include "gm/exception.h"
00032
#include "gm/xmlnode.h"
00033
#include "gm/buffer.h"
00034
00035 namespace GNUMessenger
00036 {
00037
using namespace std;
00038
using namespace CryptoPP;
00039
00040
class Protocol;
00041
class PeerProtocol;
00042
00048 class Network
00049 {
00050
public:
00051
00053 class NetworkError:
public Exception
00054 {
00055
public:
00056
NetworkError(
const string& error):
Exception(error) {}
00057 };
00058
00066
template <
typename T>
00067 static T
NBO(
const T& in) {
00068
#ifdef IS_LITTLE_ENDIAN
00069
return ByteReverse(in);
00070
#else
00071
else return in;
00072
#endif
00073
}
00074
00075
00077 enum Error
00078 {
00079
ERROR_NETWORK_NOERROR = 101,
00080
ERROR_NETWORK_INVOP,
00081
ERROR_NETWORK_IOERR,
00082
ERROR_NETWORK_INVADDR,
00083
ERROR_NETWORK_INVSOCK,
00084
ERROR_NETWORK_NOHOST,
00085
ERROR_NETWORK_INVPORT,
00086
ERROR_NETWORK_WOULDBLOCK,
00087
ERROR_NETWORK_TIMEDOUT,
00088
ERROR_NETWORK_MEMERR,
00089
ERROR_NETWORK_UNKNOWNERR,
00090
ERROR_NETWORK_ALLOCERR
00091 };
00092
00094 enum Status {
00095 Connected,
00096 Closed,
00097 HostLookupFailure,
00098 ConnectionRefused,
00099 FailedRead,
00100 FailedWrite,
00101 NotConnected
00102 };
00103
00105
Network(
Protocol *owner);
00106
00111
virtual void connectTo(
const string& host,
unsigned int port)=0;
00112
00117
virtual string
getRemoteIP()=0;
00118
00121
virtual bool isConnected() =0;
00122
00124
virtual unsigned long availableData() =0;
00125
00127 virtual Status status() {
return m_status; }
00128
00131
virtual void sendData(
const char *data,
unsigned long len)=0;
00132
virtual void sendData(
const string &data) {
sendData(data.c_str(),(
unsigned long)data.length()); }
00133
virtual void sendData(
const XMLNode &data) { string tmp=data;
sendData(tmp.c_str(),(
unsigned long)tmp.length()); }
00134
virtual void sendData(
const VBuffer& data) = 0;
00135
00137
virtual void disconnect()=0;
00138
00140
virtual void socketData(byte*& data,
unsigned int& len)=0;
00141
virtual void socketData(string& data) = 0;
00142
virtual void socketData(VBuffer& data) = 0;
00143
00145
virtual void checkForData()=0;
00146
00148
virtual int getSock()=0;
00149
00151 virtual void setOwner(
Protocol* protocol) { m_owner=protocol; }
00152
00153
unsigned long getSentBytes()
const {
return m_sentBytes; }
00154
unsigned long getSentPackets()
const {
return m_sentPackets; }
00155
00156
unsigned long getRecvBytes()
const {
return m_recvBytes; }
00157
unsigned long getRecvPackets()
const {
return m_recvPackets; }
00158
00159
virtual string getHTTP(
const string& url) = 0;
00160
00161
protected:
00162
00163
unsigned long m_sentBytes;
00164
unsigned long m_recvBytes;
00165
unsigned long m_sentPackets;
00166
unsigned long m_recvPackets;
00167
00168 Protocol *m_owner;
00169
Status m_status;
00170 };
00171
00174 class NetworkServer
00175 {
00176
public:
00177
00180 class NetworkServerError:
public Exception
00181 {
00182
public:
00183
NetworkServerError(
const string& error):
Exception(error) {}
00184 };
00185
00187 enum Status {
00188 Listening,
00189 NotListening,
00190 Error };
00191
00193 NetworkServer(
PeerProtocol *owner,
unsigned int port=3000): m_owner(owner), m_port(port) {}
00194
00196
virtual void StartListen()=0;
00197
virtual void StopListen()=0;
00198
00200
virtual bool isIncomming()=0;
00201
00203
virtual Network *
getIncomming()=0;
00204
00206 virtual int getStatus() {
return m_status;}
00207
00209 virtual unsigned short getPort() {
return m_port; }
00210
00212 virtual void setOwner(
PeerProtocol *protocol) { m_owner=protocol; }
00213
00214
protected:
00215
00216
PeerProtocol *m_owner;
00217
int m_status;
00218
unsigned int m_port;
00219
00220 };
00221
00222 }
00223
00224
#endif // !GM_NETWORK_H