diff options
| author | Adam- <Adam-@5417fbe8-f217-4b02-8779-1006273d7864> | 2010-04-08 20:23:00 +0000 |
|---|---|---|
| committer | Adam- <Adam-@5417fbe8-f217-4b02-8779-1006273d7864> | 2010-04-08 20:23:00 +0000 |
| commit | 973ecb7058325931d14a9537e5e3e57595347a10 (patch) | |
| tree | 43585f36085949a1b494374572fa4e3dc25b0960 /include/sockets.h | |
| parent | e1ff14e0c44d0a115372a821559b782c57c793f3 (diff) | |
Rewrote sockets. This adds support for IPv6 and makes Anope capable of reconnecting if it loses connection to the uplink.
git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@2862 5417fbe8-f217-4b02-8779-1006273d7864
Diffstat (limited to 'include/sockets.h')
| -rw-r--r-- | include/sockets.h | 191 |
1 files changed, 164 insertions, 27 deletions
diff --git a/include/sockets.h b/include/sockets.h index c722fbb1e..3277c1b9f 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -15,34 +15,171 @@ #define SOCKETS_H #ifdef _WIN32 -typedef SOCKET ano_socket_t; -#define ano_sockread(fd, buf, len) recv(fd, buf, len, 0) -#define ano_sockwrite(fd, buf, len) send(fd, buf, len, 0) -#define ano_sockclose(fd) closesocket(fd) -#define ano_sockgeterr() WSAGetLastError() -#define ano_sockseterr(err) WSASetLastError(err) -/* ano_sockstrerror in sockutil.c */ -extern char *ano_sockstrerror(int); -/* ano_socksetnonb in sockutil.c */ -#define ano_sockerrnonb(err) (err == WSAEINPROGRESS || err == WSAEWOULDBLOCK) -#define SOCKERR_EBADF WSAENOTSOCK -#define SOCKERR_EINTR WSAEINTR -#define SOCKERR_EINVAL WSAEINVAL -#define SOCKERR_EINPROGRESS WSAEINPROGRESS +#define CloseSocket closesocket #else -typedef int ano_socket_t; -#define ano_sockread(fd, buf, len) read(fd, buf, len) -#define ano_sockwrite(fd, buf, len) write(fd, buf, len) -#define ano_sockclose(fd) close(fd) -#define ano_sockgeterr() errno -#define ano_sockseterr(err) errno = err -#define ano_sockstrerror(err) strerror(err) -#define ano_socksetnonb(fd) fcntl(fd, F_SETFL, O_NONBLOCK) -#define ano_sockerrnonb(err) (err == EINPROGRESS) -#define SOCKERR_EBADF EBADF -#define SOCKERR_EINTR EINTR -#define SOCKERR_EINVAL EINVAL -#define SOCKERR_EINPROGRESS EINPROGRESS +#define CloseSocket close #endif +class SocketException : public CoreException +{ + public: + /** Default constructor for socket exceptions + * @param message Error message + */ + SocketException(const std::string &message) : CoreException(message) { } + + /** Default destructor + * @throws Nothing + */ + virtual ~SocketException() throw() { } +}; + +class CoreExport Socket +{ + private: + /** Read from the socket + * @param buf Buffer to read to + * @param sz How much to read + * @return Number of bytes recieved + */ + virtual int RecvInternal(char *buf, size_t sz) const; + + /** Write to the socket + * @param buf What to write + * @return Number of bytes sent, -1 on error + */ + virtual int SendInternal(const std::string &buf) const; + + protected: + /* Socket FD */ + int Sock; + /* Host this socket is connected to */ + std::string TargetHost; + /* Port we're connected to */ + int Port; + /* IP this socket is bound to */ + std::string BindHost; + /* Is this an IPv6 socket? */ + bool IPv6; + + /* Messages to be written to the socket */ + std::string WriteBuffer; + /* Part of a message not totally yet recieved */ + std::string extrabuf; + /* How much data was recieved from the socket */ + size_t RecvLen; + + public: + /** Default constructor + * @param nTargetHost Hostname to connect to + * @param nPort Port to connect to + * @param nBindHos Host to bind to when connecting + * @param nIPv6 true to use IPv6 + */ + Socket(const std::string &nTargetHost, int nPort, const std::string &nBindHost = "", bool nIPv6 = false); + + /** Default destructor + */ + virtual ~Socket(); + + /** Get the socket FD for this socket + * @return The fd + */ + virtual int GetSock() const; + + /** Check if this socket is IPv6 + * @return true or false + */ + bool IsIPv6() const; + + /** Called when there is something to be read from thie socket + * @return true on success, false to kill this socket + */ + virtual bool ProcessRead(); + + /** Called when this socket becomes writeable + * @return true on success, false to drop this socket + */ + virtual bool ProcessWrite(); + + /** Called when there is an error on this socket + */ + virtual void ProcessError(); + + /** Called with a message recieved from the socket + * @param buf The message + * @return true on success, false to kill this socket + */ + virtual bool Read(const std::string &buf); + + /** Write to the socket + * @param message The message to write + */ + void Write(const char *message, ...); + void Write(std::string &message); + + /** Get the length of the read buffer + * @return The length of the read buffer + */ + size_t ReadBufferLen() const; + + /** Get the length of the write buffer + * @return The length of the write buffer + */ + size_t WriteBufferLen() const; +}; + +class CoreExport SocketEngine +{ + private: + /* List of sockets that need to be deleted */ + std::set<Socket *> OldSockets; + /* FDs to read */ + fd_set ReadFDs; + /* FDs that want writing */ + fd_set WriteFDs; + /* Max FD */ + int MaxFD; + + /** Unmark a socket as writeable + * @param s The socket + */ + void ClearWriteable(Socket *s); + public: + /* Set of sockets */ + std::set<Socket *> Sockets; + + /** Constructor + */ + SocketEngine(); + + /** Destructor + */ + virtual ~SocketEngine(); + + /** Add a socket to the socket engine + * @param s The socket + */ + void AddSocket(Socket *s); + + /** Delete a socket from the socket engine + * @param s The socket + */ + void DelSocket(Socket *s); + + /** Mark a socket as wanting to be written to + * @param s The socket + */ + void MarkWriteable(Socket *s); + + /** Called to iterate through each socket and check for activity + */ + void Process(); + + /** Get the last socket error + * @return The error + */ + const std::string GetError() const; +}; + #endif |
