source: cheroxy/trunk/include/CRXSocket.h@ 14

Last change on this file since 14 was 14, checked in by cheese, 12 years ago

#1 add timeout to socket connection and fix test label in makefile

File size: 2.5 KB
Line 
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
18using namespace std;
19
20#define TCPSOCKET_NO_TIMEOUT -1
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
35#define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12
36
37class CRXSocket
38{
39private:
40 static bool mInitialized;
41 struct sockaddr_in mAddress;
42 int mSocket;
43
44private:
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 */
55 inline bool IsCreated (void) const;
56
57 inline bool IsReady (void) const;
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
69public:
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
87 /**
88 * for server
89 */
90 int CreateServer (const unsigned short aPort,
91 const int aBacklog = 5,
92 struct sockaddr_in * aAddress = NULL);
93
94 int Accept (struct sockaddr_in * aRemoteAddress = NULL,
95 int * aAddressLength = NULL);
96
97 /**
98 * for client
99 */
100 int Connect (const std::string aUrl,
101 const unsigned short aPort,
102 const int aTimeout = TCPSOCKET_NO_TIMEOUT);
103
104 /**
105 * for communication
106 */
107 int Send (char * aBuffer,
108 int aSize);
109
110 int Receive (char * aBuffer,
111 int aSize);
112};
113
114#endif // #ifndef __CRXSOCKET_H__
Note: See TracBrowser for help on using the repository browser.