Socket.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Socket(SocketCore* core);
  33. public:
  34. Socket();
  35. Socket(const Socket& s);
  36. ~Socket();
  37. Socket& operator=(const Socket& s);
  38. int getSockfd() const { return core->sockfd; }
  39. void beginListen() const;
  40. void getAddrInfo(pair<string, int>& addrinfo) const;
  41. Socket* acceptConnection() const;
  42. /**
  43. * Connects to the server named host and the destination port is port.
  44. * This method make socket non-blocking mode.
  45. * To make the socket blocking mode, call setBlockingMode() after
  46. * the connection is established.
  47. */
  48. void establishConnection(string host, int port) const;
  49. void setBlockingMode() const;
  50. // Closes the connection which this socket object has
  51. void closeConnection() const;
  52. // examines whether the socket of this Socket object is available for writing.
  53. // returns true if the socket is available for writing, otherwise returns false.
  54. bool isWritable(int timeout) const;
  55. // examines whether the socket of this Socket object is available for reading.
  56. // returns true if the socket is available for reading, otherwise returns false.
  57. bool isReadable(int timeout) const;
  58. // writes characters into the socket. data is a pointer pointing the first
  59. // byte of the data and len is the length of the data.
  60. void writeData(const char* data, int len, int timeout = 5) const;
  61. void writeData(string str, int timeout = 5) const;
  62. // Reads up to len bytes from this socket.
  63. // data is a pointer pointing the first
  64. // byte of the data, which must be allocated before this method is called.
  65. // len is the size of the allocated memory. When this method returns
  66. // successfully, len is replaced by the size of the read data.
  67. void readData(char* data, int& len, int timeout = 5) const;
  68. // Reads up to len bytes from this socket, but bytes are not removed from
  69. // this socket.
  70. void peekData(char* data, int& len, int timeout = 5) const;
  71. /**
  72. * Makes this socket secure.
  73. * If the system has not OpenSSL, then this method do nothing.
  74. */
  75. void initiateSecureConnection() const;
  76. };
  77. #endif // _D_SOCKET_H_