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