source: filezilla/trunk/fuentes/src/include/socket.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 5.4 KB
Line 
1#ifndef __SOCKET_H__
2#define __SOCKET_H__
3
4#include "event_handler.h"
5
6#include <errno.h>
7
8// IPv6 capable, non-blocking socket class for use with wxWidgets.
9// Error codes are the same as used by the POSIX socket functions,
10// see 'man 2 socket', 'man 2 connect', ...
11
12enum class SocketEventType
13{
14        // This is a nonfatal condition. It
15        // means there are additional addresses to try.
16        connection_next,
17
18        connection,
19        read,
20        write,
21        close
22};
23
24class CSocketEventSource
25{
26public:
27        virtual ~CSocketEventSource() {}
28};
29
30struct socket_event_type;
31typedef CEvent<socket_event_type, CSocketEventSource*, SocketEventType, int> CSocketEvent;
32
33struct hostaddress_event_type;
34typedef CEvent<hostaddress_event_type, CSocketEventSource*, wxString> CHostAddressEvent;
35
36class CCallback
37{
38public:
39        virtual void cb() {}
40};
41
42void RemoveSocketEvents(CEventHandler * handler, CSocketEventSource const* const source);
43
44class CSocketThread;
45class CSocket final : public CSocketEventSource
46{
47        friend class CSocketThread;
48public:
49        CSocket(CEventHandler* pEvtHandler);
50        virtual ~CSocket();
51
52        CSocket(CSocket const&) = delete;
53        CSocket& operator=(CSocket const&) = delete;
54
55        enum SocketState
56        {
57                // How the socket is initially
58                none,
59
60                // Only in listening and connecting states you can get a connection event.
61                // After sending the event, socket is in connected state
62                listening,
63                connecting,
64
65                // Only in this state you can get send or receive events
66                connected,
67
68                // Graceful shutdown, you get close event once done
69                closing,
70                closed
71        };
72        SocketState GetState();
73
74        enum address_family
75        {
76                unspec, // AF_UNSPEC
77                ipv4,   // AF_INET
78                ipv6    // AF_INET6
79        };
80
81        // Connects to the given host, given as name, IPv4 or IPv6 address.
82        // Returns 0 on success, else an error code. Note: EINPROGRESS is
83        // not really an error. On success, you should still wait for the
84        // connection event.
85        // If host is a name that can be resolved, a hostaddress socket event gets sent.
86        // Once connections got established, a connection event gets sent. If
87        // connection could not be established, a close event gets sent.
88        int Connect(wxString const& host, unsigned int port, address_family family = unspec, wxString const& bind = wxString());
89
90        // After receiving a send or receive event, you can call these functions
91        // as long as their return value is positive.
92        int Read(void *buffer, unsigned int size, int& error);
93        int Peek(void *buffer, unsigned int size, int& error);
94        int Write(const void *buffer, unsigned int size, int& error);
95
96        int Close();
97
98        // Returns empty string on error
99        wxString GetLocalIP(bool strip_zone_index = false) const;
100        wxString GetPeerIP(bool strip_zone_index = false) const;
101
102        // Returns the hostname passed to Connect()
103        wxString GetPeerHost() const;
104
105        // -1 on error
106        int GetLocalPort(int& error);
107        int GetRemotePort(int& error);
108
109        // If connected, either ipv4 or ipv6, unspec otherwise
110        address_family GetAddressFamily() const;
111
112        static wxString GetErrorString(int error);
113        static wxString GetErrorDescription(int error);
114
115        // Can only be called if the state is none
116        void SetEventHandler(CEventHandler* pEvtHandler);
117        CEventHandler* GetEventHandler() { return m_pEvtHandler; }
118
119        static void Cleanup(bool force);
120
121        static wxString AddressToString(const struct sockaddr* addr, int addr_len, bool with_port = true, bool strip_zone_index = false);
122        static wxString AddressToString(char const* buf, int buf_len);
123
124        int Listen(address_family family, int port = 0);
125        CSocket* Accept(int& error);
126
127        enum Flags
128        {
129                flag_nodelay = 0x01,
130                flag_keepalive = 0x02
131        };
132
133        int GetFlags() const { return m_flags; }
134        void SetFlags(int flags);
135
136        // If called on listen socket, sizes will be inherited by
137        // accepted sockets
138        int SetBufferSizes(int size_read, int size_write);
139
140        void SetSynchronousReadCallback(CCallback* cb);
141
142protected:
143        static int DoSetFlags(int fd, int flags, int flags_mask);
144        static int DoSetBufferSizes(int fd, int size_read, int size_write);
145        static int SetNonblocking(int fd);
146
147        void DetachThread();
148
149        CEventHandler* m_pEvtHandler;
150
151        int m_fd;
152
153        SocketState m_state;
154
155        CSocketThread* m_pSocketThread;
156
157        wxString m_host;
158        unsigned int m_port;
159        int m_family;
160
161        int m_flags;
162
163        int m_buffer_sizes[2];
164
165        CCallback* m_synchronous_read_cb{};
166};
167
168#ifdef __WXMSW__
169
170#ifndef EISCONN
171#define EISCONN WSAEISCONN
172#endif
173#ifndef EINPROGRESS
174#define EINPROGRESS WSAEINPROGRESS
175#endif
176#ifndef EAFNOSUPPORT
177#define EAFNOSUPPORT WSAEAFNOSUPPORT
178#endif
179#ifndef EADDRINUSE
180#define EADDRINUSE WSAEADDRINUSE
181#endif
182#ifndef ENOBUFS
183#define ENOBUFS WSAENOBUFS
184#endif
185#ifndef EPROTONOSUPPORT
186#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
187#endif
188#ifndef EALREADY
189#define EALREADY WSAEALREADY
190#endif
191#ifndef ECONNREFUSED
192#define ECONNREFUSED WSAECONNREFUSED
193#endif
194#ifndef ENOTSOCK
195#define ENOTSOCK WSAENOTSOCK
196#endif
197#ifndef ETIMEDOUT
198#define ETIMEDOUT WSAETIMEDOUT
199#endif
200#ifndef ENETUNREACH
201#define ENETUNREACH WSAENETUNREACH
202#endif
203#ifndef EHOSTUNREACH
204#define EHOSTUNREACH WSAEHOSTUNREACH
205#endif
206#ifndef ENOTCONN
207#define ENOTCONN WSAENOTCONN
208#endif
209#ifndef ENETRESET
210#define ENETRESET WSAENETRESET
211#endif
212#ifndef EOPNOTSUPP
213#define EOPNOTSUPP WSAEOPNOTSUPP
214#endif
215#ifndef ESHUTDOWN
216#define ESHUTDOWN WSAESHUTDOWN
217#endif
218#ifndef EMSGSIZE
219#define EMSGSIZE WSAEMSGSIZE
220#endif
221#ifndef ECONNABORTED
222#define ECONNABORTED WSAECONNABORTED
223#endif
224#ifndef ECONNRESET
225#define ECONNRESET WSAECONNRESET
226#endif
227#ifndef EHOSTDOWN
228#define EHOSTDOWN WSAEHOSTDOWN
229#endif
230
231// For the future:
232// Handle ERROR_NETNAME_DELETED=64
233#endif //__WXMSW__
234
235#endif //__SOCKET_H__
Note: See TracBrowser for help on using the repository browser.