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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. class Socket {
  28. public:
  29. Socket(int fd)
  30. : instream(new rdr::FdInStream(fd)),
  31. outstream(new rdr::FdOutStream(fd)),
  32. ownStreams(true), isShutdown_(false),
  33. queryConnection(false) {}
  34. virtual ~Socket() {
  35. if (ownStreams) {
  36. delete instream;
  37. delete outstream;
  38. }
  39. }
  40. rdr::FdInStream &inStream() {return *instream;}
  41. rdr::FdOutStream &outStream() {return *outstream;}
  42. int getFd() {return outstream->getFd();}
  43. // if shutdown() is overridden then the override MUST call on to here
  44. virtual void shutdown() {isShutdown_ = true;}
  45. bool isShutdown() const {return isShutdown_;}
  46. virtual bool cork(bool enable) = 0;
  47. // information about this end of the socket
  48. virtual int getMyPort() = 0;
  49. // information about the remote end of the socket
  50. virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
  51. virtual int getPeerPort() = 0;
  52. virtual char* getPeerEndpoint() = 0; // <address>::<port>
  53. // Is the remote end on the same machine?
  54. virtual bool sameMachine() = 0;
  55. // Was there a "?" in the ConnectionFilter used to accept this Socket?
  56. void setRequiresQuery() {queryConnection = true;}
  57. bool requiresQuery() const {return queryConnection;}
  58. protected:
  59. Socket() : instream(0), outstream(0), ownStreams(false),
  60. isShutdown_(false), queryConnection(false) {}
  61. Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own)
  62. : instream(i), outstream(o), ownStreams(own),
  63. isShutdown_(false), queryConnection(false) {}
  64. rdr::FdInStream* instream;
  65. rdr::FdOutStream* outstream;
  66. bool ownStreams;
  67. bool isShutdown_;
  68. bool queryConnection;
  69. };
  70. class ConnectionFilter {
  71. public:
  72. virtual bool verifyConnection(Socket* s) = 0;
  73. virtual ~ConnectionFilter() {}
  74. };
  75. class SocketListener {
  76. public:
  77. SocketListener() : fd(0), filter(0) {}
  78. virtual ~SocketListener() {}
  79. // shutdown() stops the socket from accepting further connections
  80. virtual void shutdown() = 0;
  81. // accept() returns a new Socket object if there is a connection
  82. // attempt in progress AND if the connection passes the filter
  83. // if one is installed. Otherwise, returns 0.
  84. virtual Socket* accept() = 0;
  85. virtual int getMyPort() = 0;
  86. // setFilter() applies the specified filter to all new connections
  87. void setFilter(ConnectionFilter* f) {filter = f;}
  88. int getFd() {return fd;}
  89. protected:
  90. int fd;
  91. ConnectionFilter* filter;
  92. };
  93. struct SocketException : public rdr::SystemException {
  94. SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
  95. };
  96. class SocketServer {
  97. public:
  98. virtual ~SocketServer() {}
  99. // addSocket() tells the server to serve the Socket. The caller
  100. // retains ownership of the Socket - the only way for the server
  101. // to discard a Socket is by calling shutdown() on it.
  102. // outgoing is set to true if the socket was created by connecting out
  103. // to another host, or false if the socket was created by accept()ing
  104. // an incoming connection.
  105. virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
  106. // removeSocket() tells the server to stop serving the Socket. The
  107. // caller retains ownership of the Socket - the server must NOT
  108. // delete the Socket! This call is used mainly to cause per-Socket
  109. // resources to be freed.
  110. virtual void removeSocket(network::Socket* sock) = 0;
  111. // getSockets() gets a list of sockets. This can be used to generate an
  112. // fd_set for calling select().
  113. virtual void getSockets(std::list<network::Socket*>* sockets) = 0;
  114. // processSocketReadEvent() tells the server there is a Socket read event.
  115. // The implementation can indicate that the Socket is no longer active
  116. // by calling shutdown() on it. The caller will then call removeSocket()
  117. // soon after processSocketEvent returns, to allow any pre-Socket
  118. // resources to be tidied up.
  119. virtual void processSocketReadEvent(network::Socket* sock) = 0;
  120. // processSocketReadEvent() tells the server there is a Socket write event.
  121. // This is only necessary if the Socket has been put in non-blocking
  122. // mode and needs this callback to flush the buffer.
  123. virtual void processSocketWriteEvent(network::Socket* sock) = 0;
  124. // checkTimeouts() allows the server to check socket timeouts, etc. The
  125. // return value is the number of milliseconds to wait before
  126. // checkTimeouts() should be called again. If this number is zero then
  127. // there is no timeout and checkTimeouts() should be called the next time
  128. // an event occurs.
  129. virtual int checkTimeouts() = 0;
  130. virtual bool getDisable() {return false;};
  131. };
  132. }
  133. #endif // __NETWORK_SOCKET_H__