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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2002-2004 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 <rdr/FdInStream.h>
  22. #include <rdr/FdOutStream.h>
  23. #include <rdr/Exception.h>
  24. namespace network {
  25. class Socket {
  26. public:
  27. Socket(int fd)
  28. : instream(new rdr::FdInStream(fd)),
  29. outstream(new rdr::FdOutStream(fd)),
  30. own_streams(true) {}
  31. virtual ~Socket() {
  32. if (own_streams) {
  33. delete instream;
  34. delete outstream;
  35. }
  36. }
  37. rdr::FdInStream &inStream() {return *instream;}
  38. rdr::FdOutStream &outStream() {return *outstream;}
  39. int getFd() {return outstream->getFd();}
  40. virtual void shutdown() = 0;
  41. // information about this end of the socket
  42. virtual char* getMyAddress() = 0; // a string e.g. "192.168.0.1"
  43. virtual int getMyPort() = 0;
  44. virtual char* getMyEndpoint() = 0; // <address>::<port>
  45. // information about the remote end of the socket
  46. virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
  47. virtual int getPeerPort() = 0;
  48. virtual char* getPeerEndpoint() = 0; // <address>::<port>
  49. // Is the remote end on the same machine?
  50. virtual bool sameMachine() = 0;
  51. protected:
  52. Socket() : instream(0), outstream(0), own_streams(false) {}
  53. Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own)
  54. : instream(i), outstream(o), own_streams(own) {}
  55. rdr::FdInStream* instream;
  56. rdr::FdOutStream* outstream;
  57. bool own_streams;
  58. };
  59. class ConnectionFilter {
  60. public:
  61. virtual bool verifyConnection(Socket* s) = 0;
  62. virtual bool queryUserAcceptConnection(Socket*) {return false;}
  63. };
  64. class SocketListener {
  65. public:
  66. SocketListener() : fd(0), filter(0) {}
  67. virtual ~SocketListener() {}
  68. // shutdown() stops the socket from accepting further connections
  69. virtual void shutdown() = 0;
  70. // accept() returns a new Socket object if there is a connection
  71. // attempt in progress AND if the connection passes the filter
  72. // if one is installed. Otherwise, returns 0.
  73. virtual Socket* accept() = 0;
  74. void setFilter(ConnectionFilter* f) {filter = f;}
  75. int getFd() {return fd;}
  76. protected:
  77. int fd;
  78. ConnectionFilter* filter;
  79. };
  80. struct SocketException : public rdr::SystemException {
  81. SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
  82. };
  83. class SocketServer {
  84. public:
  85. virtual ~SocketServer() {}
  86. // addClient() tells the server to manage the socket.
  87. // If the server can't manage the socket, it must shutdown() it.
  88. virtual void addClient(network::Socket* sock) = 0;
  89. // processSocketEvent() tells the server there is a socket read event.
  90. // If there is an error, or the socket has been closed/shutdown then
  91. // the server MUST delete the socket AND return false.
  92. virtual bool processSocketEvent(network::Socket* sock) = 0;
  93. // checkTimeouts() allows the server to check socket timeouts, etc. The
  94. // return value is the number of milliseconds to wait before
  95. // checkTimeouts() should be called again. If this number is zero then
  96. // there is no timeout and checkTimeouts() should be called the next time
  97. // an event occurs.
  98. virtual int checkTimeouts() = 0;
  99. // soonestTimeout() is a function to help work out the soonest of several
  100. // timeouts.
  101. static void soonestTimeout(int* timeout, int newTimeout) {
  102. if (newTimeout && (!*timeout || newTimeout < *timeout))
  103. *timeout = newTimeout;
  104. }
  105. virtual bool getDisable() {return false;};
  106. };
  107. }
  108. #endif // __NETWORK_SOCKET_H__