libktorrent  2.2.0
socket.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef NETSOCKET_H
21 #define NETSOCKET_H
22 
23 #include <ktorrent_export.h>
24 #include <QSharedPointer>
25 #include <net/socketdevice.h>
26 #include "address.h"
27 
28 namespace net
29 {
30  const int SEND_FAILURE = 0;
31  const int SEND_WOULD_BLOCK = -1;
32 
36  class KTORRENT_EXPORT Socket : public SocketDevice
37  {
38  public:
39  explicit Socket(int fd,int ip_version);
40  explicit Socket(bool tcp,int ip_version);
41  ~Socket() override;
42 
43  void setBlocking(bool on) override;
44  bool connectTo(const Address & addr) override;
45  bool connectSuccesFull() override;
46  void close() override;
47  Uint32 bytesAvailable() const override;
48  int send(const bt::Uint8* buf,int len) override;
49  int recv(bt::Uint8* buf,int max_len) override;
50  bool ok() const override {return m_fd >= 0;}
51  int fd() const override {return m_fd;}
52  bool setTOS(unsigned char type_of_service) override;
53  const Address & getPeerName() const override {return addr;}
54  Address getSockName() const override;
55 
56  void reset() override;
57  void prepare(Poll* p,Poll::Mode mode) override;
58  bool ready(const Poll* p,Poll::Mode mode) const override;
59 
60  bool bind(const QString & ip,Uint16 port,bool also_listen);
61  bool bind(const Address & addr,bool also_listen);
62  int accept(Address & a);
63 
64  int sendTo(const bt::Uint8* buf,int size,const Address & addr);
65  int recvFrom(bt::Uint8* buf,int max_size,Address & addr);
66 
67  bool isIPv4() const {return m_ip_version == 4;}
68  bool isIPv6() const {return m_ip_version == 6;}
69 
71  int take();
72 
73  typedef QSharedPointer<Socket> Ptr;
74 
75  private:
76  void cacheAddress();
77 
78  private:
79  int m_fd;
80  int m_ip_version;
81  int r_poll_index;
82  int w_poll_index;
83  };
84 
85 }
86 
87 #endif
net::Address
Definition: address.h:59
net::Poll
Definition: poll.h:64