21 #ifndef UTP_CONNECTION_H
22 #define UTP_CONNECTION_H
26 #include <QWaitCondition>
27 #include <QBasicTimer>
28 #include <QSharedPointer>
29 #include <ktorrent_export.h>
30 #include <net/address.h>
31 #include <utp/utpprotocol.h>
32 #include <util/circularbuffer.h>
33 #include <util/timer.h>
34 #include <utp/remotewindow.h>
35 #include <boost/concept_check.hpp>
49 class KTORRENT_EXPORT Connection :
public QObject,
public Retransmitter
60 class TransmissionError
63 TransmissionError(
const char* file,
int line);
72 ConnectionState state;
73 bt::Uint16 send_connection_id;
74 bt::Uint32 reply_micro;
75 bt::Uint16 recv_connection_id;
82 bt::Uint32 packet_size;
83 bt::Uint32 last_window_size_transmitted;
85 bt::Uint64 bytes_received;
86 bt::Uint64 bytes_sent;
87 bt::Uint32 packets_received;
88 bt::Uint32 packets_sent;
89 bt::Uint64 bytes_lost;
90 bt::Uint32 packets_lost;
100 void setBlocking(
bool on) {blocking = on;}
106 void startConnecting();
109 const Stats & connectionStats()
const {
return stats;}
112 ConnectionState handlePacket(
const PacketParser & parser, bt::Buffer::Ptr packet);
115 const net::Address & remoteAddress()
const {
return stats.remote;}
118 bt::Uint16 receiveConnectionID()
const {
return stats.recv_connection_id;}
121 int send(
const bt::Uint8* data, bt::Uint32 len);
124 int recv(bt::Uint8* buf, bt::Uint32 max_len);
127 ConnectionState connectionState()
const {
return stats.state;}
130 Type connectionType()
const {
return stats.type;}
133 bt::Uint32 bytesAvailable()
const;
136 bool isWriteable()
const;
139 bool waitUntilConnected();
142 bool waitForData(bt::Uint32 timeout = 0);
151 void updateRTT(
const Header* hdr, bt::Uint32 packet_rtt, bt::Uint32 packet_size)
override;
157 bool allDataSent()
const;
160 bt::Uint32 currentTimeout()
const override {
return stats.timeout;}
162 typedef QSharedPointer<Connection> Ptr;
163 typedef QWeakPointer<Connection> WPtr;
166 void setWeakPointer(WPtr ptr) {
self = ptr;}
176 void updateDelayMeasurement(
const Header* hdr);
177 void sendStateOrData();
179 void sendPacket(bt::Uint32 type, bt::Uint16 p_ack_nr);
180 void checkIfClosed();
184 bt::Uint32 extensionLength()
const;
185 void handleTimeout();
193 mutable QMutex mutex;
194 QWaitCondition connected;
195 QWaitCondition data_ready;
200 Connection::WPtr
self;
215 virtual bool sendTo(Connection::Ptr conn,
const PacketBuffer & packet) = 0;
218 virtual void stateChanged(Connection::Ptr conn,
bool readable,
bool writeable) = 0;
221 virtual void closed(Connection::Ptr conn) = 0;
226 #endif // UTP_CONNECTION_H