Socket.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "Socket.h"
  23. Socket::Socket() {
  24. core = new SocketCore();
  25. }
  26. Socket::Socket(const Socket& s) {
  27. core = s.core;
  28. core->use++;
  29. }
  30. Socket::Socket(SocketCore* core) {
  31. this->core = core;
  32. }
  33. Socket::~Socket() {
  34. core->use--;
  35. if(core->use == 0) {
  36. delete core;
  37. }
  38. }
  39. Socket& Socket::operator=(const Socket& s) {
  40. if(this != &s) {
  41. core->use--;
  42. if(core->use == 0) {
  43. delete core;
  44. }
  45. core = s.core;
  46. core->use++;
  47. }
  48. return *this;
  49. }
  50. void Socket::beginListen() const {
  51. core->beginListen();
  52. }
  53. void Socket::getAddrInfo(pair<string, int>& addrinfo) const {
  54. core->getAddrInfo(addrinfo);
  55. }
  56. Socket* Socket::acceptConnection() const {
  57. return new Socket(core->acceptConnection());
  58. }
  59. void Socket::establishConnection(string host, int port) const {
  60. core->establishConnection(host, port);
  61. }
  62. void Socket::setBlockingMode() const {
  63. core->setBlockingMode();
  64. }
  65. void Socket::closeConnection() const {
  66. core->closeConnection();
  67. }
  68. bool Socket::isWritable(int timeout) const {
  69. return core->isWritable(timeout);
  70. }
  71. bool Socket::isReadable(int timeout) const {
  72. return core->isReadable(timeout);
  73. }
  74. void Socket::writeData(const char* data, int len, int timeout) const {
  75. core->writeData(data, len, timeout);
  76. }
  77. void Socket::writeData(string str, int timeout) const {
  78. core->writeData(str.c_str(), str.size(), timeout);
  79. }
  80. void Socket::readData(char* data, int& len, int timeout) const {
  81. core->readData(data, len, timeout);
  82. }
  83. void Socket::peekData(char* data, int& len, int timeout) const {
  84. core->peekData(data, len, timeout);
  85. }
  86. void Socket::initiateSecureConnection() const {
  87. core->initiateSecureConnection();
  88. }