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