[go: up one dir, main page]

Menu

[r5]: / dtorrent / trunk / peer.h  Maximize  Restore  History

Download this file

151 lines (115 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef PEER_H
#define PEER_H
#include "./def.h"
#ifdef WINDOWS
#include <Winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#endif
#include <time.h>
#include "btrequest.h"
#include "btstream.h"
#include "bitfield.h"
#include "rate.h"
#define P_CONNECTING (unsigned char) 0 // connecting
#define P_HANDSHAKE (unsigned char) 1 // handshaking
#define P_SUCCESS (unsigned char) 2 // successful
#define P_FAILED (unsigned char) 3 // failed
typedef struct _btstatus{
unsigned char remote_choked:1;
unsigned char remote_interested:1;
unsigned char local_choked:1;
unsigned char local_interested:1;
unsigned char reserved:4; /* unused */
}BTSTATUS;
size_t get_nl(char *from);
void set_nl(char *to, size_t from);
class btBasic
{
private:
Rate rate_dl;
Rate rate_ul;
public:
struct sockaddr_in m_sin;
//IPµØÖ·Ïà¹Øº¯Êý
int IpEquiv(struct sockaddr_in addr);
void SetIp(struct sockaddr_in addr);
void SetAddress(struct sockaddr_in addr);
void GetAddress(struct sockaddr_in *psin) const {
memcpy(psin,&m_sin,sizeof(struct sockaddr_in));
}
// ËÙÂÊÏà¹Øº¯Êý
Rate GetDLRate() const { return rate_dl; }
Rate GetULRate() const { return rate_ul; }
u_int64_t TotalDL() const { return rate_dl.Count(); }
u_int64_t TotalUL() const { return rate_ul.Count(); }
void DataRecved(size_t nby) { rate_dl.CountAdd(nby); }
void DataSended(size_t nby) { rate_ul.CountAdd(nby); }
size_t RateDL() const { return rate_dl.RateMeasure(); }
size_t RateUL() const { return rate_ul.RateMeasure();}
void StartDLTimer() { rate_dl.StartTimer(); }
void StartULTimer() { rate_ul.StartTimer(); }
void StopDLTimer() { rate_dl.StopTimer(); }
void StopULTimer() { rate_ul.StopTimer(); }
void ResetDLTimer() { rate_dl.Reset(); }
void ResetULTimer() { rate_ul.Reset(); }
};
class btPeer:public btBasic
{
private:
time_t m_last_timestamp, m_unchoke_timestamp;
unsigned char m_f_keepalive:1;
unsigned char m_status:4;
unsigned char m_reserved:3;
BTSTATUS m_state;
size_t m_cached_idx;
size_t m_err_count;
int m_standby;
int PieceDeliver(size_t mlen);
int ReportComplete(size_t idx);
int RequestCheck();
int SendRequest();
int CancelRequest(PSLICE ps);
int ReponseSlice();
int RequestPiece();
int MsgDeliver();
int CouldReponseSlice();
int BandWidthLimit();
int BandWidthLimitUp();
int BandWidthLimitDown();
public:
BitField bitfield;
btStream stream;
RequestQueue request_q;
RequestQueue reponse_q;
btPeer();
int RecvModule();
int SendModule();
time_t SetLastTimestamp() { return time(&m_last_timestamp); }
time_t GetLastTimestamp() const { return m_last_timestamp; }
time_t SetLastUnchokeTime() { return time(&m_unchoke_timestamp); }
time_t GetLastUnchokeTime() const { return m_unchoke_timestamp; }
int Is_Remote_Interested() const { return m_state.remote_interested ? 1 : 0; }
int Is_Remote_UnChoked() const { return m_state.remote_choked ? 0 : 1; }
int Is_Local_Interested() const { return m_state.local_interested ? 1 : 0;}
int Is_Local_UnChoked() const { return m_state.local_choked ? 0 : 1; }
int SetLocal(unsigned char s);
int CancelSliceRequest(size_t idx, size_t off, size_t len);
void SetStatus(unsigned char s){ m_status = s; }
unsigned char GetStatus() const { return m_status; }
int NeedWrite();
int NeedRead();
void CloseConnection();
int AreYouOK();
int Send_ShakeInfo();
int HandShake();
int Need_Remote_Data();
int Need_Local_Data();
void dump();
};
extern btBasic Self;
#endif