~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/protocol/peer_connection_base.h

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#define LIBTORRENT_PROTOCOL_PEER_CONNECTION_BASE_H
39
39
 
40
40
#include "data/chunk_handle.h"
 
41
#include "download/choke_manager_node.h"
41
42
#include "net/socket_stream.h"
42
43
#include "torrent/poll.h"
43
44
 
 
45
#include "encryption_info.h"
44
46
#include "peer_chunks.h"
45
47
#include "protocol_base.h"
46
48
#include "request_list.h"
65
67
  typedef ProtocolBase           ProtocolRead;
66
68
  typedef ProtocolBase           ProtocolWrite;
67
69
 
 
70
#if USE_EXTRA_DEBUG == 666
 
71
  // For testing, use a really small buffer.
 
72
  typedef ProtocolBuffer<256>  EncryptBuffer;
 
73
#else
 
74
  typedef ProtocolBuffer<16384>  EncryptBuffer;
 
75
#endif
 
76
 
68
77
  // Find an optimal number for this.
69
78
  static const uint32_t read_size = 64;
70
79
 
71
80
  PeerConnectionBase();
72
81
  virtual ~PeerConnectionBase();
73
82
  
74
 
  void                initialize(DownloadMain* download, PeerInfo* p, SocketFd fd, Bitfield* bitfield);
 
83
  void                initialize(DownloadMain* download, PeerInfo* p, SocketFd fd, Bitfield* bitfield, EncryptionInfo* encryptionInfo);
75
84
  void                cleanup();
76
85
 
77
 
  bool                is_up_choked()                { return m_up->choked(); }
78
 
  bool                is_up_interested()            { return m_up->interested(); }
79
 
  bool                is_down_choked()              { return m_down->choked(); }
80
 
  bool                is_down_interested()          { return m_down->interested(); }
81
 
 
82
 
  bool                is_upload_wanted() const      { return m_down->interested() && !m_peerChunks.is_snubbed(); }
 
86
  bool                is_up_choked()                { return m_upChoke.choked(); }
 
87
  bool                is_up_interested()            { return m_upChoke.queued(); }
 
88
  bool                is_up_snubbed()               { return m_upChoke.snubbed(); }
 
89
 
 
90
  bool                is_down_queued()              { return m_downChoke.queued(); }
 
91
  bool                is_down_local_unchoked()      { return m_downChoke.unchoked(); }
 
92
  bool                is_down_remote_unchoked()     { return m_downUnchoked; }
 
93
  bool                is_down_interested()          { return m_downInterested; }
 
94
 
 
95
  void                set_upload_snubbed(bool v);
83
96
 
84
97
  bool                is_seeder() const             { return m_peerChunks.is_seeder(); }
85
98
 
 
99
  bool                is_encrypted() const          { return m_encryption.is_encrypted(); }
 
100
  bool                is_obfuscated() const         { return m_encryption.is_obfuscated(); }
 
101
 
86
102
  PeerInfo*           peer_info()                   { return m_peerInfo; }
87
103
  const PeerInfo*     c_peer_info() const           { return m_peerInfo; }
88
104
  PeerChunks*         peer_chunks()                 { return &m_peerChunks; }
89
105
 
 
106
  DownloadMain*       download()                    { return m_download; }
90
107
  RequestList*        download_queue()              { return &m_downloadQueue; }
91
108
 
92
 
  // Make sure you choke the peer when snubbing. Snubbing a peer will
93
 
  // only cause it not to be unchoked.
94
 
  //
95
 
  // Move this stuff to PeerChunks.
96
 
  void                set_snubbed(bool v);
97
 
 
98
 
  rak::timer          time_last_choked() const      { return m_timeLastChoked; }
99
 
 
100
109
  // These must be implemented by the child class.
101
110
  virtual void        initialize_custom() = 0;
102
 
 
103
111
  virtual void        update_interested() = 0;
104
 
 
105
 
  virtual void        receive_finished_chunk(int32_t i) = 0;
106
112
  virtual bool        receive_keepalive() = 0;
107
 
  void                receive_choke(bool v);
 
113
 
 
114
  bool                receive_upload_choke(bool choke);
 
115
  bool                receive_download_choke(bool choke);
108
116
 
109
117
  virtual void        event_error();
110
118
 
111
119
  void                push_unread(const void* data, uint32_t size);
112
120
 
 
121
  void                cancel_transfer(BlockTransfer* transfer);
 
122
 
113
123
protected:
114
124
  inline bool         read_remaining();
115
125
  inline bool         write_remaining();
122
132
  void                read_request_piece(const Piece& p);
123
133
  void                read_cancel_piece(const Piece& p);
124
134
 
125
 
  void                read_buffer_move_unused();
126
 
 
127
135
  void                write_prepare_piece();
128
136
 
129
137
  bool                down_chunk_start(const Piece& p);
138
146
  uint32_t            down_chunk_skip_process(const void* buffer, uint32_t length);
139
147
 
140
148
  bool                up_chunk();
 
149
  inline uint32_t     up_chunk_encrypt(uint32_t quota);
141
150
 
142
151
  void                down_chunk_release();
143
152
  void                up_chunk_release();
145
154
  bool                should_request();
146
155
  bool                try_request_pieces();
147
156
 
148
 
  void                set_remote_interested();
149
 
  void                set_remote_not_interested();
150
 
 
151
157
  // Insert into the poll unless we're blocking for throttling etc.
152
158
  void                read_insert_poll_safe();
153
159
  void                write_insert_poll_safe();
167
173
  Piece               m_upPiece;
168
174
  ChunkHandle         m_upChunk;
169
175
 
 
176
  // The interested state no longer follows the spec's wording as it
 
177
  // has been swapped.
 
178
  //
 
179
  // Thus the same ProtocolBase object now groups the same choke and
 
180
  // interested states togheter, thus for m_up 'interested' means the
 
181
  // remote peer wants upload and 'choke' means we've choked upload to
 
182
  // that peer.
 
183
  //
 
184
  // In the downlod object, 'queued' now means the same as the spec's
 
185
  // 'unchoked', while 'unchoked' means we start requesting pieces.
 
186
  ChokeManagerNode    m_upChoke;
 
187
  ChokeManagerNode    m_downChoke;
 
188
 
 
189
  bool                m_downInterested;
 
190
  bool                m_downUnchoked;
 
191
 
170
192
  bool                m_sendChoked;
171
193
  bool                m_sendInterested;
 
194
  bool                m_tryRequest;
172
195
 
173
 
  rak::timer          m_timeLastChoked;
174
196
  rak::timer          m_timeLastRead;
 
197
 
 
198
  EncryptBuffer*      m_encryptBuffer;
 
199
  EncryptionInfo      m_encryption;
175
200
};
176
201
 
177
202
inline void
180
205
  m_down->buffer()->move_end(size);
181
206
}
182
207
 
183
 
inline bool
184
 
PeerConnectionBase::read_remaining() {
185
 
  m_down->buffer()->move_position(read_stream_throws(m_down->buffer()->position(), m_down->buffer()->remaining()));
186
 
 
187
 
  return !m_down->buffer()->remaining();
188
 
}
189
 
 
190
 
inline bool
191
 
PeerConnectionBase::write_remaining() {
192
 
  m_up->buffer()->move_position(write_stream_throws(m_up->buffer()->position(), m_up->buffer()->remaining()));
193
 
 
194
 
  return !m_up->buffer()->remaining();
195
 
}
196
 
 
197
208
inline void
198
209
PeerConnectionBase::read_insert_poll_safe() {
199
210
  if (m_down->get_state() != ProtocolRead::IDLE)