TCP.h

Go to the documentation of this file.
00001 
00002 //written by Luke Lenhart, 2008
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 #include "../base/Setup.h"
00008 
00009 #ifdef MPMA_COMPILE_NET
00010 
00011 #include "Common.h"
00012 #include <vector>
00013 
00014 class MPMAMemoryManager; //FD
00015 
00016 namespace NET
00017 {
00019     class TCPClient
00020     {
00021     public:
00023         static TCPClient* Connect(const Address &addr, uint16 localPort=0);
00024 
00026         static void FreeClient(TCPClient *client);
00027 
00029         bool IsConnected() const;
00030 
00032         bool Send(const void *data, nuint dataLen);
00033 
00035         nuint Receive(std::vector<uint8> &data, nuint exactBytesToRetrieve=0);
00036 
00038         inline uint16 GetLocalPort() const { return port; }
00039 
00041         inline const Address& GetRemoteAddress() const { return remoteAddr; }
00042 
00044         void EnableSendCoalescing(bool allow);
00045 
00046         //TODO: Some sort of callback mechanism for notifications.
00047 
00048     private:
00049         //we are the only allocator
00050         TCPClient();
00051         inline ~TCPClient() {}
00052         inline TCPClient(const TCPClient&) {}
00053         inline void operator=(const TCPClient&) {}
00054         friend class ::MPMAMemoryManager;
00055         friend class TCPServer;
00056 
00057         //
00058         Address remoteAddr;
00059         uint16 port;
00060         nsint sock;
00061         mutable bool connected;
00062         std::vector<uint8> bufferedBytes;
00063     };
00064 
00066     class TCPServer
00067     {
00068     public:
00070         static TCPServer* Listen(uint16 port, bool openNatAutomatically=true);
00071 
00073         static void FreeServer(TCPServer *server);
00074 
00076         TCPClient* GetNextConnection();
00077 
00079         inline uint16 GetListenPort() const { return port; }
00080 
00081         //TODO: Some sort of callback mechanism for notifications.
00082 
00083     private:
00084         //we are the only allocator
00085         TCPServer();
00086         inline ~TCPServer() {}
00087         inline TCPServer(const TCPServer&) {}
00088         inline void operator=(const TCPServer&) {}
00089         friend class ::MPMAMemoryManager;
00090 
00091         //
00092         nsint sock;
00093         uint16 port;
00094         bool autoUPNP;
00095     };
00096 }
00097 
00098 #endif //#ifdef MPMA_COMPILE_NET
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends