DCL 4.0
Loading...
Searching...
No Matches
Socket.h
Go to the documentation of this file.
1#ifndef __DCL_SOCKET_H__
2#define __DCL_SOCKET_H__ 20030501
3
4#ifndef __DCL_CONFIG_H__
5#include <dcl/Config.h>
6#endif
7
8#if __DCL_WINDOWS
9 #ifndef _WINSOCK2API_
10 #error "Required winsock2.h, See dcl/_windows.h"
11 #endif
12 #include <ws2tcpip.h>
13 #ifdef _MSC_VER
14 #pragma comment(lib, "ws2_32.lib")
15 #endif
16#else
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21#endif
22
23#ifndef __DCL_POLL_ABLE_H__
24 #include <dcl/PollAble.h>
25#endif
26
27__DCL_BEGIN_NAMESPACE
28
29#if __DCL_WINDOWS
30 typedef int socklen_t;
31 class SocketPollThread;
32#endif
33
41class DCLCAPI Socket : public PollAble
42{
43 DECLARE_CLASSINFO(Socket)
44public:
45 // Object overrides
46 virtual String toString() const;
47
48 // File overrides
49 void open(const String& _addr, uint16_t _port)
51
52 virtual size_t available() const
54
55 virtual size_t read(void* _buf, size_t _n)
57
58 virtual size_t write(const void* _buf, size_t _n)
60
61 struct DCLCAPI Addr
62 {
63 union
64 {
65 short sa_family;
66 struct sockaddr_in sa_in; // AF_INET
67 struct sockaddr_in6 sa_in6; // AF_INET6
68#if !__DCL_WINDOWS
69 struct sockaddr_un sa_un; // AF_UNIX
70#endif
71 };
72
73 Addr();
74
81 Addr(const char* _addr, uint16_t _port)
83
84 Addr(const String& _addr, uint16_t _port)
86
87#if !__DCL_WINDOWS
91 Addr(const char* _path)
93
94 Addr(const String& _path)
96
97#endif
98 String toString() const;
99 };
100
110 void bind(const Addr& _my_addr,
111 int _type = SOCK_STREAM, int _protocol = 0, bool _reuse = true
113
114 void accept(Socket& _r)
116
117 void connect(const Addr& _serv_addr)
119
120 socklen_t getsockname(Addr& _addr)
122
123 socklen_t getpeername(Addr& _addr)
125
126 // API implements
127
128 Socket();
129
130 Socket(const String& _addr, uint16_t _port)
132
133 virtual ~Socket();
134
135
136 virtual void close()
138
139 void create(int _domain = AF_INET, int _type = SOCK_STREAM, int _protocol = 0)
141
142 void setNonblock()
144
145 void bind(const struct sockaddr* _my_addr, socklen_t _addrlen)
147
148 void listen(unsigned _backlog = 5)
150
151 void accept(Socket& r, struct sockaddr* _addr, socklen_t* _addrlen)
153
154 virtual void connect(const sockaddr* _serv_addr, socklen_t _addrlen)
156
160 virtual size_t send(const void* _buf, size_t _n, int _flags = 0)
162
166 virtual size_t recv(void* _buf, size_t _n, int _flags = 0)
168
169protected:
174 virtual bool onEvent(short _revents, PollThread* _pPollThread)
176
177#if __DCL_WINDOWS
178private:
179 // SocketPollThread에 의해 사용된다.
180 WSAEVENT __waitEvent;
181
182 friend class SocketPollThread;
183#endif
184};
185
186__DCL_END_NAMESPACE
187
188#endif // __DCL_SOCKET_H__
#define DCLCAPI
Definition Config.h:100
#define __DCL_THROWS1(e)
Definition Config.h:167
#define DECLARE_CLASSINFO(class_name)
Definition Object.h:210
ByteString r
_r
Definition SQLField.cpp:260
__DCL_BEGIN_NAMESPACE typedef PollThread SocketPollThread
void open(const String &_path, int _oflags=READONLY, int _mode=0666) __DCL_THROWS1(IOException *)
Definition File.cpp:77
virtual size_t read(void *_buf, size_t _n) __DCL_THROWS1(IOException *)
Definition File.cpp:476
virtual String toString() const
virtual size_t write(const void *_buf, size_t _n) __DCL_THROWS1(IOException *)
Definition File.cpp:535
virtual size_t available() const __DCL_THROWS1(IOException *)
Definition File.cpp:401
friend class PollThread
Definition PollAble.h:38
virtual bool onEvent(short _revents, PollThread *_pPollThread) __DCL_THROWS1(IOException *)
Definition Socket.cpp:591