root / trunk / Windows / addons / ofxNetwork / src / ofxTCPManager.h @ 3
View | Annotate | Download (5.6 KB)
| 1 | #ifndef ___ofxTCPManager__H__
|
|---|---|
| 2 | #define ___ofxTCPManager__H__
|
| 3 | |
| 4 | |
| 5 | //////////////////////////////////////////////////////////////////////////////////////
|
| 6 | // Original author: ???????? we think Christian Naglhofer
|
| 7 | // Crossplatform port by: Theodore Watson May 2007 - update Jan 2008
|
| 8 | // Changes: Mac (and should be nix) equivilant functions and data types for
|
| 9 | // win32 calls, artificial nix version of GetTickCount() used for timestamp
|
| 10 | //
|
| 11 | //////////////////////////////////////////////////////////////////////////////////////
|
| 12 | |
| 13 | |
| 14 | /***************************************************************
|
| 15 | USAGE |
| 16 | **************************************************************** |
| 17 | |
| 18 | TCP Socket Client: |
| 19 | ------------------ |
| 20 | |
| 21 | 1) create() |
| 22 | 2) connect() |
| 23 | 3) send() |
| 24 | ... |
| 25 | x) close() |
| 26 | |
| 27 | optional: |
| 28 | SetTimeoutSend() |
| 29 | |
| 30 | |
| 31 | TCP Socket Server: |
| 32 | ------------------ |
| 33 | |
| 34 | 1) create() |
| 35 | 2) bind() |
| 36 | 3) listen() |
| 37 | 4) accept() |
| 38 | 5) receive() |
| 39 | ... |
| 40 | x) close() |
| 41 | |
| 42 | optional: |
| 43 | SetTimeoutAccept() |
| 44 | SetTimeoutReceive() |
| 45 | |
| 46 | ****************************************************************/ |
| 47 | #include "ofConstants.h" |
| 48 | #include <string.h> |
| 49 | #include <wchar.h> |
| 50 | #include <stdio.h> |
| 51 | |
| 52 | #ifndef TARGET_WIN32
|
| 53 | |
| 54 | //unix includes - works for osx should be same for *nix
|
| 55 | #include <ctype.h> |
| 56 | #include <netdb.h> |
| 57 | #include <string.h> |
| 58 | #include <fcntl.h> |
| 59 | #include <time.h> |
| 60 | #include <errno.h> |
| 61 | #include <unistd.h> |
| 62 | #include <netinet/in.h> |
| 63 | #include <arpa/inet.h> |
| 64 | #include <sys/timeb.h> |
| 65 | #include <sys/types.h> |
| 66 | #include <sys/socket.h> |
| 67 | #include <sys/time.h> |
| 68 | #include <sys/ioctl.h> |
| 69 | |
| 70 | #include <sys/signal.h> |
| 71 | |
| 72 | //other types
|
| 73 | #define INVALID_SOCKET -1 |
| 74 | #define SOCKET_ERROR -1 |
| 75 | #define FAR
|
| 76 | #define SO_MAX_MSG_SIZE TCP_MAXSEG
|
| 77 | #else
|
| 78 | //windows includes
|
| 79 | #include <winsock2.h> |
| 80 | #include <ws2tcpip.h> // TCP/IP annex needed for multicasting |
| 81 | #endif
|
| 82 | |
| 83 | //--------------------------------------------------------------------------------
|
| 84 | class InetAddr : public sockaddr_in |
| 85 | {
|
| 86 | public:
|
| 87 | // constructors
|
| 88 | InetAddr() {
|
| 89 | memset(this, 0, sizeof(InetAddr)); |
| 90 | sin_family= AF_INET; |
| 91 | sin_port= 0;
|
| 92 | sin_addr.s_addr= 0;
|
| 93 | }; |
| 94 | |
| 95 | InetAddr(const sockaddr& sockAddr) {
|
| 96 | memcpy(this, &sockAddr, sizeof(sockaddr));
|
| 97 | }; |
| 98 | |
| 99 | InetAddr(const sockaddr_in& sin) {
|
| 100 | memcpy(this, &sin, sizeof(sockaddr_in));
|
| 101 | }; |
| 102 | |
| 103 | InetAddr(const unsigned long ulAddr, const unsigned short ushPort= 0) { |
| 104 | memset(this, 0, sizeof(InetAddr)); |
| 105 | sin_family= AF_INET; |
| 106 | sin_port= htons(ushPort); |
| 107 | sin_addr.s_addr= htonl(ulAddr); |
| 108 | }; |
| 109 | |
| 110 | InetAddr(const wchar_t* pStrIP, const unsigned short usPort= 0) { |
| 111 | char szStrIP[32]; |
| 112 | |
| 113 | #ifdef TARGET_WIN32
|
| 114 | WideCharToMultiByte(CP_ACP, 0, pStrIP, (int)wcslen(pStrIP) + 1, szStrIP, 32, 0, 0); |
| 115 | #else
|
| 116 | //theo note:
|
| 117 | //do we need to set the codepage here first?
|
| 118 | //or is the default one okay?
|
| 119 | wcstombs(szStrIP, pStrIP, 32);
|
| 120 | #endif
|
| 121 | |
| 122 | memset(this, 0, sizeof(InetAddr)); |
| 123 | sin_family= AF_INET; |
| 124 | sin_port= htons(usPort); |
| 125 | sin_addr.s_addr= inet_addr(szStrIP); |
| 126 | } |
| 127 | |
| 128 | InetAddr(const char* pStrIP, const unsigned short usPort= 0) { |
| 129 | memset(this, 0, sizeof(InetAddr)); |
| 130 | sin_family= AF_INET; |
| 131 | sin_port= htons(usPort); |
| 132 | sin_addr.s_addr= inet_addr(pStrIP); |
| 133 | } |
| 134 | /// returns the address in dotted-decimal format
|
| 135 | char* DottedDecimal() { return inet_ntoa(sin_addr); } |
| 136 | unsigned short GetPort() const { return ntohs(sin_port); } |
| 137 | unsigned long GetIpAddr() const { return ntohl(sin_addr.s_addr); } |
| 138 | /// operators added for efficiency
|
| 139 | const InetAddr& operator=(const sockaddr& sa) { |
| 140 | memcpy(this, &sa, sizeof(sockaddr));
|
| 141 | return *this;
|
| 142 | } |
| 143 | const InetAddr& operator=(const sockaddr_in& sin) { |
| 144 | memcpy(this, &sin, sizeof(sockaddr_in));
|
| 145 | return *this;
|
| 146 | } |
| 147 | operator sockaddr() { return *((sockaddr *)this); }
|
| 148 | operator sockaddr *() { return (sockaddr *)this; }
|
| 149 | operator sockaddr_in*() { return (sockaddr_in*) this; }
|
| 150 | }; |
| 151 | |
| 152 | typedef const InetAddr* LPCINETADDR; |
| 153 | typedef InetAddr* LPINETADDR;
|
| 154 | |
| 155 | |
| 156 | //--------------------------------------------------------------------------------
|
| 157 | /// Socket constants.
|
| 158 | #define SOCKET_TIMEOUT SOCKET_ERROR - 1 |
| 159 | #define NO_TIMEOUT 0xFFFF |
| 160 | #define OF_TCP_DEFAULT_TIMEOUT NO_TIMEOUT
|
| 161 | |
| 162 | |
| 163 | //--------------------------------------------------------------------------------
|
| 164 | /// Implementation of a TCP socket.
|
| 165 | class ofxTCPManager |
| 166 | {
|
| 167 | public:
|
| 168 | ofxTCPManager(); |
| 169 | virtual ~ofxTCPManager() {
|
| 170 | if ((m_hSocket)&&(m_hSocket != INVALID_SOCKET)) Close();
|
| 171 | }; |
| 172 | |
| 173 | bool Close();
|
| 174 | bool Create();
|
| 175 | bool Listen(int iMaxConnections); |
| 176 | bool Connect(char *pAddrStr, unsigned short usPort); |
| 177 | bool Bind(unsigned short usPort); |
| 178 | bool Accept(ofxTCPManager& sock);
|
| 179 | //sends the data, but it is not guaranteed that really all data will be sent
|
| 180 | int Send(const char* pBuff, const int iSize); |
| 181 | //all data will be sent guaranteed.
|
| 182 | int SendAll(const char* pBuff, const int iSize); |
| 183 | int Receive(char* pBuff, const int iSize); |
| 184 | int ReceiveAll(char* pBuff, const int iSize); |
| 185 | int Write(const char* pBuff, const int iSize); |
| 186 | bool GetRemoteAddr(LPINETADDR pIntAddr);
|
| 187 | bool GetInetAddr(LPINETADDR pInetAddr);
|
| 188 | void SetTimeoutSend(int timeoutInSeconds); |
| 189 | void SetTimeoutReceive(int timeoutInSeconds); |
| 190 | void SetTimeoutAccept(int timeoutInSeconds); |
| 191 | int GetTimeoutSend();
|
| 192 | int GetTimeoutReceive();
|
| 193 | int GetTimeoutAccept();
|
| 194 | bool SetReceiveBufferSize(int sizeInByte); |
| 195 | bool SetSendBufferSize(int sizeInByte); |
| 196 | int GetReceiveBufferSize();
|
| 197 | int GetSendBufferSize();
|
| 198 | int GetMaxConnections();
|
| 199 | bool SetNonBlocking(bool useNonBlocking); |
| 200 | bool CheckHost(const char *pAddrStr); |
| 201 | void CleanUp();
|
| 202 | |
| 203 | protected:
|
| 204 | int m_iListenPort;
|
| 205 | int m_iMaxConnections;
|
| 206 | |
| 207 | #ifdef TARGET_WIN32
|
| 208 | SOCKET m_hSocket; |
| 209 | #else
|
| 210 | int m_hSocket;
|
| 211 | #endif
|
| 212 | |
| 213 | unsigned long m_dwTimeoutSend; |
| 214 | unsigned long m_dwTimeoutReceive; |
| 215 | unsigned long m_dwTimeoutAccept; |
| 216 | bool nonBlocking;
|
| 217 | static bool m_bWinsockInit; |
| 218 | }; |
| 219 | |
| 220 | #endif // ___ofxTCPManager__H__ |
