123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #ifndef M_Networking
- #define M_Networking
-
- #include <stdexcept>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cstring>
-
- using namespace std;
-
-
-
- #if defined(WIN32) || defined(WIN64)
-
-
-
- #include <winsock2.h>
- typedef int socklen_t;
- typedef SOCKET hSocket;
-
- #else
-
-
-
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/file.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <cstdlib>
- #include <cstdio>
- #include <cerrno>
-
- typedef int hSocket;
- const hSocket INVALID_SOCKET = -1;
-
- #endif
-
-
-
- const unsigned long LOCALHOST = 0x7F000001;
-
- const int DefaultMaxPending = 5;
-
- const int TCPClientBufferSize = 4096;
- const int TCPHostBufferSize = 4096;
-
- const int NOFLAGS = 0;
-
-
-
-
-
-
- class IP4Address {
- private:
- unsigned long int IP;
-
- public:
- IP4Address();
- IP4Address(const unsigned long int newIP);
- IP4Address(const IP4Address&);
-
- IP4Address(const char* newIP);
- IP4Address(const string& newIP);
-
- IP4Address& operator=(const unsigned long int Right);
- IP4Address& operator=(const char* Right);
- IP4Address& operator=(const string& Right);
-
- operator unsigned long int() const;
- operator string() const;
-
- bool operator<(const IP4Address Right) const;
- bool operator>(const IP4Address Right) const;
- bool operator==(const IP4Address Right) const;
- bool operator!=(const IP4Address Right) const;
- bool operator<=(const IP4Address Right) const;
- bool operator>=(const IP4Address Right) const;
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Networking {
- private:
-
- public:
-
- class NotSupportedError : public runtime_error {
- public: NotSupportedError(const string& w):runtime_error(w) {}
- };
- class InitializationError : public runtime_error {
- public: InitializationError(const string& w):runtime_error(w) {}
- };
- class ControlError : public runtime_error {
- public: ControlError(const string& w):runtime_error(w) {}
- };
- class SocketCreationError : public runtime_error {
- public: SocketCreationError(const string& w):runtime_error(w) {}
- };
- class SocketSetSockOptError : public runtime_error {
- public: SocketSetSockOptError(const string& w):runtime_error(w) {}
- };
- class SocketBindError : public runtime_error {
- public: SocketBindError(const string& w):runtime_error(w) {}
- };
- class SocketListenError : public runtime_error {
- public: SocketListenError(const string& w):runtime_error(w) {}
- };
- class SocketConnectError : public runtime_error {
- public: SocketConnectError(const string& w):runtime_error(w) {}
- };
- class SocketAcceptError : public runtime_error {
- public: SocketAcceptError(const string& w):runtime_error(w) {}
- };
- class SocketReadError : public runtime_error {
- public: SocketReadError(const string& w):runtime_error(w) {}
- };
- class SocketWriteError : public runtime_error {
- public: SocketWriteError(const string& w):runtime_error(w) {}
- };
-
- static string DescriptiveError(string Msg, int Errno);
-
- Networking();
- ~Networking();
-
- int getLastError();
- int setNonBlocking(hSocket socket);
- int closeSocket(hSocket socket);
-
- bool WouldBlock(int ErrorCode);
- bool InProgress(int ErrorCode);
- bool IsConnected(int ErrorCode);
-
- };
-
- extern Networking Network;
-
-
-
-
-
-
-
-
-
-
-
-
- const int IPStringBufferSize = 40;
- const int PortStringBufferSize = 20;
-
- class SocketAddress {
- private:
- struct sockaddr_in Address;
-
- char IPStringBuffer[IPStringBufferSize];
- char PortStringBuffer[PortStringBufferSize];
-
- public:
-
- SocketAddress();
-
- struct sockaddr_in* getPtr_sockaddr_in();
- struct sockaddr* getPtr_sockaddr();
- socklen_t getAddressSize();
-
- void setAddress(unsigned long ipAddress);
- void setAddress(char* ipString);
- unsigned long getAddress();
- const char* getAddress(char* str);
- void getAddress(int& a0, int& a1, int& a2, int& a3);
-
- void setPort(unsigned short port);
- void setPort(char* port);
- unsigned short getPort();
- const char* getPort(char* str);
-
- void clear();
- };
-
-
-
-
-
-
-
-
-
-
-
-
- class Socket {
- protected:
- hSocket Handle;
- bool NonBlocking;
- bool ReuseAddress;
- bool OpenSucceeded;
-
- int LastError;
-
- SocketAddress LocalAddress;
- SocketAddress RemoteAddress;
-
- public:
- Socket();
- ~Socket();
-
- hSocket getHandle();
- bool isNonBlocking();
- void makeNonBlocking();
- bool isReuseAddress();
- bool isReuseAddress(bool set);
- bool isOpen();
- int getLastError();
-
- virtual void open() = 0;
- void close();
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class MessagePort {
-
- public:
-
- virtual bool isNonBlocking() = 0;
- virtual int transmit(const char* bfr, int size) = 0;
- virtual int receive(char* bfr, int size) = 0;
- virtual int delimited_receive(char* bfr, int size, char delimiter) = 0;
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Message {
-
- char* Data;
- int DataBufferSize;
- int DataSize;
- char* RWPointer;
- bool TransferInProgress;
- bool Delimited;
- char Delimiter;
-
- public:
-
- Message(const Message& M);
- Message(int Size);
- Message(int Size, char Delimiter);
- Message(char* NewData, int Size);
- Message(char* NewData, int Size, char Delimiter);
-
- void writeTo(MessagePort &P);
- void readFrom(MessagePort &P);
- bool isBusy();
- void transferMore();
- void abortTransfer();
-
- bool isDelimited();
- char getDelimiter();
- char* getData();
- int getDataSize();
-
- };
-
-
-
-
-
-
-
-
-
-
- class TCPClient;
-
- class TCPListener : public Socket {
- private:
-
- bool OpenStage1Complete;
- bool OpenStage2Complete;
-
- public:
-
- TCPListener(unsigned short Port);
- TCPListener(SocketAddress& WhereToBind);
- ~TCPListener();
-
- int MaxPending;
-
- virtual void open();
-
- TCPClient* acceptClient();
- };
-
-
-
-
-
-
-
-
-
-
- class TCPClient : public Socket, public MessagePort {
- private:
- TCPListener& MyListener;
-
- char ReadBuffer[TCPClientBufferSize];
-
- char* ReadPointer;
- int DataLength;
-
- bool ReadBufferIsEmpty();
- void fillReadBuffer();
-
- public:
-
- TCPClient(TCPListener& L, hSocket H, SocketAddress& A);
- ~TCPClient();
-
- TCPListener& getMyListener();
-
- bool isNonBlocking();
- virtual int transmit(const char* bfr, int size);
- virtual int receive(char* bfr, int size);
- virtual int delimited_receive(char* bfr, int size, char delimiter);
- virtual void open();
-
- unsigned long getRemoteIP();
- const char* getRemoteIP(char* str);
- unsigned short getRemotePort();
- const char* getRemotePort(char* str);
-
- };
-
-
-
-
-
-
-
-
-
- class TCPHost : public Socket, public MessagePort {
- private:
- char ReadBuffer[TCPHostBufferSize];
- char* ReadPointer;
- int DataLength;
-
- bool ReadBufferIsEmpty();
- void fillReadBuffer();
-
- bool OpenStage1Complete;
-
- public:
-
- TCPHost(unsigned short Port);
- TCPHost(SocketAddress& Remote);
-
- ~TCPHost();
-
- bool isNonBlocking();
- virtual int transmit(const char* bfr, int size);
- virtual int receive(char* bfr, int size);
- virtual int delimited_receive(char* bfr, int size, char delimiter);
- virtual void open();
-
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
- class UDPListener : public Socket {
-
- };
-
-
-
-
-
-
-
-
-
-
-
- class UDPRequest : public MessagePort {
-
- };
-
-
-
-
-
-
-
-
-
-
-
- class UDPHost : public Socket, public MessagePort {
-
- };
-
-
-
-
-
-
-
-
-
-
-
- class UDPReceiver : public Socket, public MessagePort {
-
- };
-
-
-
-
-
-
-
-
- class UDPBroadcaster : public Socket, public MessagePort {
-
- };
-
-
-
-
-
-
- #include "networking.inline.hpp"
-
- #endif
|