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

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

#1 work for CRXProxy and CRXException

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>
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
20using namespace std;
21
22#define TCPSOCKET_NO_TIMEOUT -1
23
24#define ERROR_TCPSOCKET -1000
25
26#define ERROR_TCPSOCKET_FAILED_TO_INITIALIZE ERROR_TCPSOCKET - 1
27#define ERROR_TCPSOCKET_ALREADY_IN_USE ERROR_TCPSOCKET - 2
28#define ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET ERROR_TCPSOCKET - 3
29#define ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME ERROR_TCPSOCKET - 4
30#define ERROR_TCPSOCKET_FAILED_TO_CONNECT ERROR_TCPSOCKET - 5
31#define ERROR_TCPSOCKET_FAILED_TO_BIND ERROR_TCPSOCKET - 6
32#define ERROR_TCPSOCKET_FAILED_TO_LISTEN ERROR_TCPSOCKET - 7
33#define ERROR_TCPSOCKET_FAILED_TO_ACCEPT ERROR_TCPSOCKET - 8
34#define ERROR_TCPSOCKET_FAILED_TO_SEND ERROR_TCPSOCKET - 9
35#define ERROR_TCPSOCKET_FAILED_TO_RECEIVE ERROR_TCPSOCKET - 10
36#define ERROR_TCPSOCKET_NOT_READY ERROR_TCPSOCKET - 11
37#define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12
38
39class CRXSocket : public CRXException
40{
41private:
42 static bool mInitialized;
43 struct sockaddr_in mAddress;
44 int mSocket;
45
46private:
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
71public:
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 (const unsigned short aPort,
93 const int aBacklog = 5,
94 struct sockaddr_in * aAddress = NULL);
95
96 int Accept (struct sockaddr_in * aRemoteAddress = NULL,
97 int * aAddressLength = NULL);
98
99 /**
100 * for client
101 */
102 int Connect (const std::string aUrl,
103 const unsigned short aPort,
104 const int aTimeout = TCPSOCKET_NO_TIMEOUT);
105
106 /**
107 * for communication
108 */
109 int Send (char * aBuffer,
110 int aSize);
111
112 int Receive (char * aBuffer,
113 int aSize);
114};
115
116#endif // #ifndef __CRXSOCKET_H__
Note: See TracBrowser for help on using the repository browser.