Socket.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_SOCKET_H_
  23. #define _D_SOCKET_H_
  24. #include <string>
  25. #include "SocketCore.h"
  26. #include "common.h"
  27. using namespace std;
  28. class Socket {
  29. private:
  30. // socket endpoint descriptor
  31. SocketCore* core;
  32. public:
  33. Socket();
  34. Socket(const Socket& s);
  35. ~Socket();
  36. Socket& operator=(const Socket& s);
  37. int getSockfd() const { return core->sockfd; }
  38. /**
  39. * Connects to the server named host and the destination port is port.
  40. * This method make socket non-blocking mode.
  41. * To make the socket blocking mode, call setNonBlockingMode() after
  42. * the connection is established.
  43. */
  44. void establishConnection(string host, int port);
  45. void setNonBlockingMode();
  46. // Closes the connection which this socket object has
  47. void closeConnection();
  48. // examines whether the socket of this Socket object is available for writing.
  49. // returns true if the socket is available for writing, otherwise returns false.
  50. bool isWritable(int timeout);
  51. // examines whether the socket of this Socket object is available for reading.
  52. // returns true if the socket is available for reading, otherwise returns false.
  53. bool isReadable(int timeout);
  54. // writes characters into the socket. data is a pointer pointing the first
  55. // byte of the data and len is the length of the data.
  56. void writeData(const char* data, int len, int timeout = 5);
  57. // Reads up to len bytes from this socket.
  58. // data is a pointer pointing the first
  59. // byte of the data, which must be allocated before this method is called.
  60. // len is the size of the allocated memory. When this method returns
  61. // successfully, len is replaced by the size of the read data.
  62. void readData(char* data, int& len, int timeout = 5);
  63. // Reads up to len bytes from this socket, but bytes are not removed from
  64. // this socket.
  65. void peekData(char* data, int& len, int timeout = 5);
  66. #ifdef HAVE_LIBSSL
  67. // for SSL
  68. void initiateSecureConnection();
  69. #endif // HAVE_LIBSSL
  70. };
  71. #endif // _D_SOCKET_H_