Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 131 lines (119 sloc) 4.182 kb
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
1 /* <!-- copyright */
2 /*
3 * aria2 - The high speed download utility
4 *
5 * Copyright (C) 2006 Tatsuhiro Tsujikawa
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * In addition, as a special exception, the copyright holders give
22 * permission to link the code of portions of this program with the
23 * OpenSSL library under certain conditions as described in each
24 * individual source file, and distribute linked combinations
25 * including the two.
26 * You must obey the GNU General Public License in all respects
27 * for all of the code used other than OpenSSL. If you modify
28 * file(s) with this exception, you may extend this exception to your
29 * version of the file(s), but you are not obligated to do so. If you
30 * do not wish to do so, delete this exception statement from your
31 * version. If you delete this exception statement from all source
32 * files in the program, then also delete it here.
33 */
34 /* copyright --> */
35 #include "BtExtendedMessage.h"
36 #include "BtRegistry.h"
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
37 #include "PeerObject.h"
38 #include "BtMessageFactory.h"
39 #include "BtMessageReceiver.h"
40 #include "BtMessageDispatcher.h"
41 #include "BtRequestFactory.h"
42 #include "PeerConnection.h"
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
43 #include "ExtensionMessage.h"
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
44 #include "ExtensionMessageFactory.h"
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
45 #include "PeerMessageUtil.h"
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
46 #include "Peer.h"
47 #include "BtContext.h"
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
48 #include "DlAbortEx.h"
49 #include "message.h"
50 #include "Util.h"
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
51 #include <cassert>
52 #include <cstring>
53
54 namespace aria2 {
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
55
56 BtExtendedMessage::BtExtendedMessage(const ExtensionMessageHandle& extensionMessage):_extensionMessage(extensionMessage), _msg(0), _msgLength(0)
57 {}
58
59 BtExtendedMessage::~BtExtendedMessage()
60 {
61 delete [] _msg;
62 }
63
64 const unsigned char* BtExtendedMessage::getMessage() {
65 if(!_msg) {
66 /**
67 * len --- 2+extpayload.length, 4bytes
68 * id --- 20, 1byte
69 * extmsgid --- extmsgid, 1byte
70 * extpayload --- extpayload, nbytes
71 * total: 6+extpayload.length bytes
72 */
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
73 std::string payload = _extensionMessage->getBencodedData();
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
74 _msgLength = 6+payload.size();
75 _msg = new unsigned char[_msgLength];
76 PeerMessageUtil::createPeerMessageString(_msg, _msgLength, 2+payload.size(), ID);
77 *(_msg+5) = _extensionMessage->getExtensionMessageID();
78 memcpy(_msg+6, payload.c_str(), payload.size());
79 }
80 return _msg;
81 }
82
032c7c2 @tatsuhiro-t 2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
83 size_t BtExtendedMessage::getMessageLength() {
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
84 getMessage();
85 return _msgLength;
86 }
87
88 bool BtExtendedMessage::sendPredicate() const
89 {
90 return peer->isExtendedMessagingEnabled();
91 }
92
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
93 std::string BtExtendedMessage::toString() const {
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
94 return "extended "+_extensionMessage->toString();
95 }
96
97 BtExtendedMessageHandle
98 BtExtendedMessage::create(const BtContextHandle& btContext,
99 const PeerHandle& peer,
fc0fa42 @tatsuhiro-t 2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
100 const unsigned char* data, size_t dataLength)
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
101 {
102 if(dataLength < 2) {
103 throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength);
104 }
032c7c2 @tatsuhiro-t 2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
105 uint8_t id = PeerMessageUtil::getId(data);
286f34c @tatsuhiro-t 2007-12-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
106 if(id != ID) {
107 throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID);
108 }
109 ExtensionMessageFactoryHandle factory = EXTENSION_MESSAGE_FACTORY(btContext,
110 peer);
111 assert(!factory.isNull());
112 ExtensionMessageHandle extmsg = factory->createMessage(data+1,
113 dataLength-1);
114 BtExtendedMessageHandle message = new BtExtendedMessage(extmsg);
115 return message;
116 }
117
118 void BtExtendedMessage::doReceivedAction()
119 {
120 if(!_extensionMessage.isNull()) {
121 _extensionMessage->doReceivedAction();
122 }
123 }
124
125 ExtensionMessageHandle BtExtendedMessage::getExtensionMessage() const
126 {
127 return _extensionMessage;
128 }
1b7c198 @tatsuhiro-t 2008-02-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
tatsuhiro-t authored
129
130 } // namespace aria2
Something went wrong with that request. Please try again.