[5] | 1 | /**
|
---|
| 2 | * CRXSocket.h
|
---|
| 3 | */
|
---|
| 4 | #ifndef __CRXSOCKET_H__
|
---|
| 5 | #define __CRXSOCKET_H__
|
---|
| 6 |
|
---|
[18] | 7 | #include "CRXException.h"
|
---|
| 8 |
|
---|
[5] | 9 | #ifdef WIN32
|
---|
| 10 | # include <WinSock2.h>
|
---|
[38] | 11 | typedef int socklen_t;
|
---|
[5] | 12 | #else
|
---|
| 13 | # include <netinet/in.h>
|
---|
| 14 | # include <sys/socket.h>
|
---|
| 15 | # include <arpa/inet.h>
|
---|
| 16 | # include <netdb.h>
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #include <string>
|
---|
| 20 |
|
---|
[14] | 21 | #define TCPSOCKET_NO_TIMEOUT -1
|
---|
[5] | 22 |
|
---|
| 23 | #define ERROR_TCPSOCKET -1000
|
---|
| 24 |
|
---|
| 25 | #define ERROR_TCPSOCKET_FAILED_TO_INITIALIZE ERROR_TCPSOCKET - 1
|
---|
| 26 | #define ERROR_TCPSOCKET_ALREADY_IN_USE ERROR_TCPSOCKET - 2
|
---|
| 27 | #define ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET ERROR_TCPSOCKET - 3
|
---|
| 28 | #define ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME ERROR_TCPSOCKET - 4
|
---|
| 29 | #define ERROR_TCPSOCKET_FAILED_TO_CONNECT ERROR_TCPSOCKET - 5
|
---|
| 30 | #define ERROR_TCPSOCKET_FAILED_TO_BIND ERROR_TCPSOCKET - 6
|
---|
| 31 | #define ERROR_TCPSOCKET_FAILED_TO_LISTEN ERROR_TCPSOCKET - 7
|
---|
| 32 | #define ERROR_TCPSOCKET_FAILED_TO_ACCEPT ERROR_TCPSOCKET - 8
|
---|
| 33 | #define ERROR_TCPSOCKET_FAILED_TO_SEND ERROR_TCPSOCKET - 9
|
---|
| 34 | #define ERROR_TCPSOCKET_FAILED_TO_RECEIVE ERROR_TCPSOCKET - 10
|
---|
| 35 | #define ERROR_TCPSOCKET_NOT_READY ERROR_TCPSOCKET - 11
|
---|
[14] | 36 | #define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12
|
---|
[5] | 37 |
|
---|
[18] | 38 | class CRXSocket : public CRXException
|
---|
[5] | 39 | {
|
---|
| 40 | private:
|
---|
[14] | 41 | static bool mInitialized;
|
---|
| 42 | struct sockaddr_in mAddress;
|
---|
| 43 | int mSocket;
|
---|
[5] | 44 |
|
---|
| 45 | private:
|
---|
| 46 | /**
|
---|
| 47 | * create socket.
|
---|
| 48 | * @return success : socket id, failed : -1.
|
---|
| 49 | */
|
---|
| 50 | int CreateSocket (void);
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * check socket status.
|
---|
| 54 | * @return available : true, else : false.
|
---|
| 55 | */
|
---|
[24] | 56 | bool IsCreated (void) const;
|
---|
[5] | 57 |
|
---|
[24] | 58 | bool IsReady (void) const;
|
---|
[5] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * static functions
|
---|
| 62 | */
|
---|
| 63 | static int Initialize (void);
|
---|
| 64 |
|
---|
| 65 | static bool IsInitialized (void);
|
---|
| 66 |
|
---|
| 67 | static void Finalize (void);
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | public:
|
---|
| 71 | /**
|
---|
| 72 | * common functions
|
---|
| 73 | */
|
---|
| 74 | CRXSocket (int aSocket = 0);
|
---|
| 75 |
|
---|
| 76 | ~CRXSocket (void);
|
---|
| 77 |
|
---|
| 78 | void Attach (int aSocket);
|
---|
| 79 |
|
---|
| 80 | int Detach (void);
|
---|
| 81 |
|
---|
| 82 | void Close (void);
|
---|
| 83 |
|
---|
| 84 | operator int (void) const;
|
---|
| 85 |
|
---|
| 86 | CRXSocket & operator = (int aSocket);
|
---|
| 87 |
|
---|
[24] | 88 | int SetTimeout (const int aTimeout);
|
---|
| 89 |
|
---|
[5] | 90 | /**
|
---|
| 91 | * for server
|
---|
| 92 | */
|
---|
[14] | 93 | int CreateServer (const unsigned short aPort,
|
---|
| 94 | const int aBacklog = 5,
|
---|
| 95 | struct sockaddr_in * aAddress = NULL);
|
---|
[5] | 96 |
|
---|
[18] | 97 | int Accept (struct sockaddr_in * aRemoteAddress = NULL,
|
---|
| 98 | int * aAddressLength = NULL);
|
---|
[5] | 99 |
|
---|
| 100 | /**
|
---|
| 101 | * for client
|
---|
| 102 | */
|
---|
[14] | 103 | int Connect (const std::string aUrl,
|
---|
| 104 | const unsigned short aPort,
|
---|
| 105 | const int aTimeout = TCPSOCKET_NO_TIMEOUT);
|
---|
[5] | 106 |
|
---|
| 107 | /**
|
---|
| 108 | * for communication
|
---|
| 109 | */
|
---|
[24] | 110 | int Send (const char * aBuffer,
|
---|
| 111 | int aSize);
|
---|
[5] | 112 |
|
---|
| 113 | int Receive (char * aBuffer,
|
---|
| 114 | int aSize);
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | #endif // #ifndef __CRXSOCKET_H__
|
---|