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

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

#1 apply reuseaddr

File size: 2.9 KB
RevLine 
[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>
[38]11typedef int socklen_t;
[5]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
[14]21#define TCPSOCKET_NO_TIMEOUT -1
[5]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
[57]36#define ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT ERROR_TCPSOCKET - 12
37#define ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT ERROR_TCPSOCKET - 13
[61]38#define ERROR_TCPSOCKET_FAILED_TO_GET_SOCKOPT ERROR_TCPSOCKET - 14
[5]39
[18]40class CRXSocket : public CRXException
[5]41{
42private:
[14]43 struct sockaddr_in mAddress;
44 int mSocket;
[5]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 */
[24]57 bool IsCreated (void) const;
[5]58
[24]59 bool IsReady (void) const;
[5]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 */
[61]75 CRXSocket (int aSocket = -1);
[5]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
[61]89 int GetOption (const int aOptName,
90 void * aOptVal,
91 size_t * aOptLen);
92
[59]93 int SetOption (const int aOptName,
94 const void * aOptVal,
95 const size_t aOptLen);
96
[24]97 int SetTimeout (const int aTimeout);
98
[5]99 /**
100 * for server
101 */
[14]102 int CreateServer (const unsigned short aPort,
103 const int aBacklog = 5,
104 struct sockaddr_in * aAddress = NULL);
[5]105
[18]106 int Accept (struct sockaddr_in * aRemoteAddress = NULL,
107 int * aAddressLength = NULL);
[5]108
109 /**
110 * for client
111 */
[14]112 int Connect (const std::string aUrl,
113 const unsigned short aPort,
114 const int aTimeout = TCPSOCKET_NO_TIMEOUT);
[5]115
116 /**
117 * for communication
118 */
[24]119 int Send (const char * aBuffer,
120 int aSize);
[5]121
122 int Receive (char * aBuffer,
123 int aSize);
124};
125
126#endif // #ifndef __CRXSOCKET_H__
Note: See TracBrowser for help on using the repository browser.