PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Network/Socket.h

https://bitbucket.org/jiangzhengzheng/efonvnc
C Header | 147 lines | 75 code | 24 blank | 48 comment | 1 complexity | 8958f3b861269a9888dc0225f97c79a6 MD5 | raw file
  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 <limits.h>
  22. #include <rdr/FdInStream.h>
  23. #include <rdr/FdOutStream.h>
  24. #include <rdr/Exception.h>
  25. namespace network {
  26. class Socket {
  27. public:
  28. Socket(int fd)
  29. : instream(new rdr::FdInStream(fd)),
  30. outstream(new rdr::FdOutStream(fd)),
  31. ownStreams(true), isShutdown_(false),
  32. queryConnection(false) {}
  33. virtual ~Socket() {
  34. if (ownStreams) {
  35. delete instream;
  36. delete outstream;
  37. }
  38. }
  39. rdr::FdInStream &inStream() {return *instream;}
  40. rdr::FdOutStream &outStream() {return *outstream;}
  41. int getFd() {return outstream->getFd();}
  42. // if shutdown() is overridden then the override MUST call on to here
  43. virtual void shutdown() {isShutdown_ = true;}
  44. bool isShutdown() const {return isShutdown_;}
  45. // information about this end of the socket
  46. virtual char* getMyAddress() = 0; // a string e.g. "192.168.0.1"
  47. virtual int getMyPort() = 0;
  48. virtual char* getMyEndpoint() = 0; // <address>::<port>
  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. };
  74. class SocketListener {
  75. public:
  76. SocketListener() : fd(0), filter(0) {}
  77. virtual ~SocketListener() {}
  78. // shutdown() stops the socket from accepting further connections
  79. virtual void shutdown() = 0;
  80. // accept() returns a new Socket object if there is a connection
  81. // attempt in progress AND if the connection passes the filter
  82. // if one is installed. Otherwise, returns 0.
  83. virtual Socket* accept() = 0;
  84. // setFilter() applies the specified filter to all new connections
  85. void setFilter(ConnectionFilter* f) {filter = f;}
  86. int getFd() {return fd;}
  87. protected:
  88. int fd;
  89. ConnectionFilter* filter;
  90. };
  91. struct SocketException : public rdr::SystemException {
  92. SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
  93. };
  94. class SocketServer {
  95. public:
  96. virtual ~SocketServer() {}
  97. // addSocket() tells the server to serve the Socket. The caller
  98. // retains ownership of the Socket - the only way for the server
  99. // to discard a Socket is by calling shutdown() on it.
  100. // outgoing is set to true if the socket was created by connecting out
  101. // to another host, or false if the socket was created by accept()ing
  102. // an incoming connection.
  103. virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
  104. // removeSocket() tells the server to stop serving the Socket. The
  105. // caller retains ownership of the Socket - the server must NOT
  106. // delete the Socket! This call is used mainly to cause per-Socket
  107. // resources to be freed.
  108. virtual void removeSocket(network::Socket* sock) = 0;
  109. // processSocketEvent() tells the server there is a Socket read event.
  110. // The implementation can indicate that the Socket is no longer active
  111. // by calling shutdown() on it. The caller will then call removeSocket()
  112. // soon after processSocketEvent returns, to allow any pre-Socket
  113. // resources to be tidied up.
  114. virtual void processSocketEvent(network::Socket* sock) = 0;
  115. // checkTimeouts() allows the server to check socket timeouts, etc. The
  116. // return value is the number of milliseconds to wait before
  117. // checkTimeouts() should be called again. If this number is zero then
  118. // there is no timeout and checkTimeouts() should be called the next time
  119. // an event occurs.
  120. virtual int checkTimeouts() = 0;
  121. };
  122. }
  123. #endif // __NETWORK_SOCKET_H__