summaryrefslogtreecommitdiff
path: root/src/PeerConnection.h
blob: e96f37e961d0b14cddd420ac68cd55aec318bd00 (plain)
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
/* <!-- copyright */
/*
 * aria2 - a simple utility for downloading files faster
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* copyright --> */
#ifndef _D_PEER_CONNECTION_H_
#define _D_PEER_CONNECTION_H_

#include "Option.h"
#include "Socket.h"
#include "Logger.h"
#include "TorrentMan.h"
#include "PeerMessage.h"
#include "HandshakeMessage.h"
#include "common.h"

// we assume maximum length of incoming message is "piece" message with 16KB
// data. Messages beyond that size are dropped.
#define MAX_PAYLOAD_LEN (9+16*1024)

class PeerConnection {
private:
  int cuid;
  const Socket* socket;
  const Option* option;
  const Logger* logger;

  char resbuf[MAX_PAYLOAD_LEN];
  int resbufLength;
  int currentPayloadLength;
  char lenbuf[4];
  int lenbufLength;

public:
  PeerConnection(int cuid, const Socket* socket, const Option* op);
  ~PeerConnection();
  
  // Returns the number of bytes written
  int sendMessage(const char* msg, int length);

  bool receiveMessage(char* msg, int& length);
  /**
   * Returns true if a handshake message is fully received, otherwise returns
   * false.
   * In both cases, 'msg' is filled with received bytes and the filled length
   * is assigned to 'length'.
   */
  bool receiveHandshake(char* msg, int& length);
};

#endif // _D_PEER_CONNECTION_H_