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

Last change on this file since 38 was 38, checked in by cheese, 11 years ago

#1 more mutex interface and add debugging code to check connection port number

File size: 2.6 KB
Line 
1/**
2 * CRXSocket.h
3 */
4#ifndef __CRXSOCKET_H__
5#define __CRXSOCKET_H__
6
7#include "CRXException.h"
8
9#ifdef WIN32
10# include <WinSock2.h>
11typedef int socklen_t;
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
21#define TCPSOCKET_NO_TIMEOUT -1
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
36#define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12
37
38class CRXSocket : public CRXException
39{
40private:
41 static bool mInitialized;
42 struct sockaddr_in mAddress;
43 int mSocket;
44
45private:
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 */
56 bool IsCreated (void) const;
57
58 bool IsReady (void) const;
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
70public:
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
88 int SetTimeout (const int aTimeout);
89
90 /**
91 * for server
92 */
93 int CreateServer (const unsigned short aPort,
94 const int aBacklog = 5,
95 struct sockaddr_in * aAddress = NULL);
96
97 int Accept (struct sockaddr_in * aRemoteAddress = NULL,
98 int * aAddressLength = NULL);
99
100 /**
101 * for client
102 */
103 int Connect (const std::string aUrl,
104 const unsigned short aPort,
105 const int aTimeout = TCPSOCKET_NO_TIMEOUT);
106
107 /**
108 * for communication
109 */
110 int Send (const char * aBuffer,
111 int aSize);
112
113 int Receive (char * aBuffer,
114 int aSize);
115};
116
117#endif // #ifndef __CRXSOCKET_H__
Note: See TracBrowser for help on using the repository browser.