You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Socket.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // -=- Socket.h - abstract base-class for any kind of network stream/socket
  19. #ifndef __NETWORK_SOCKET_H__
  20. #define __NETWORK_SOCKET_H__
  21. #include <list>
  22. #include <limits.h>
  23. #include <rdr/FdInStream.h>
  24. #include <rdr/FdOutStream.h>
  25. #include <rdr/Exception.h>
  26. namespace network {
  27. void initSockets();
  28. bool isSocketListening(int sock);
  29. class Socket {
  30. public:
  31. Socket(int fd);
  32. virtual ~Socket();
  33. rdr::FdInStream &inStream() {return *instream;}
  34. rdr::FdOutStream &outStream() {return *outstream;}
  35. int getFd() {return outstream->getFd();}
  36. void shutdown();
  37. bool isShutdown() const;
  38. virtual bool cork(bool enable) = 0;
  39. // information about the remote end of the socket
  40. virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
  41. virtual char* getPeerEndpoint() = 0; // <address>::<port>
  42. // Was there a "?" in the ConnectionFilter used to accept this Socket?
  43. void setRequiresQuery();
  44. bool requiresQuery() const;
  45. protected:
  46. Socket();
  47. void setFd(int fd);
  48. private:
  49. rdr::FdInStream* instream;
  50. rdr::FdOutStream* outstream;
  51. bool isShutdown_;
  52. bool queryConnection;
  53. };
  54. class ConnectionFilter {
  55. public:
  56. virtual bool verifyConnection(Socket* s) = 0;
  57. virtual ~ConnectionFilter() {}
  58. };
  59. class SocketListener {
  60. public:
  61. SocketListener(int fd);
  62. virtual ~SocketListener();
  63. // shutdown() stops the socket from accepting further connections
  64. void shutdown();
  65. // accept() returns a new Socket object if there is a connection
  66. // attempt in progress AND if the connection passes the filter
  67. // if one is installed. Otherwise, returns 0.
  68. Socket* accept();
  69. virtual int getMyPort() = 0;
  70. // setFilter() applies the specified filter to all new connections
  71. void setFilter(ConnectionFilter* f) {filter = f;}
  72. int getFd() {return fd;}
  73. protected:
  74. SocketListener();
  75. void listen(int fd);
  76. // createSocket() should create a new socket of the correct class
  77. // for the given file descriptor
  78. virtual Socket* createSocket(int fd) = 0;
  79. protected:
  80. int fd;
  81. ConnectionFilter* filter;
  82. };
  83. struct SocketException : public rdr::SystemException {
  84. SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
  85. };
  86. class SocketServer {
  87. public:
  88. virtual ~SocketServer() {}
  89. // addSocket() tells the server to serve the Socket. The caller
  90. // retains ownership of the Socket - the only way for the server
  91. // to discard a Socket is by calling shutdown() on it.
  92. // outgoing is set to true if the socket was created by connecting out
  93. // to another host, or false if the socket was created by accept()ing
  94. // an incoming connection.
  95. virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
  96. // removeSocket() tells the server to stop serving the Socket. The
  97. // caller retains ownership of the Socket - the server must NOT
  98. // delete the Socket! This call is used mainly to cause per-Socket
  99. // resources to be freed.
  100. virtual void removeSocket(network::Socket* sock) = 0;
  101. // getSockets() gets a list of sockets. This can be used to generate an
  102. // fd_set for calling select().
  103. virtual void getSockets(std::list<network::Socket*>* sockets) = 0;
  104. // processSocketReadEvent() tells the server there is a Socket read event.
  105. // The implementation can indicate that the Socket is no longer active
  106. // by calling shutdown() on it. The caller will then call removeSocket()
  107. // soon after processSocketEvent returns, to allow any pre-Socket
  108. // resources to be tidied up.
  109. virtual void processSocketReadEvent(network::Socket* sock) = 0;
  110. // processSocketReadEvent() tells the server there is a Socket write event.
  111. // This is only necessary if the Socket has been put in non-blocking
  112. // mode and needs this callback to flush the buffer.
  113. virtual void processSocketWriteEvent(network::Socket* sock) = 0;
  114. };
  115. }
  116. #endif // __NETWORK_SOCKET_H__