3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "connection.h"
23 #include "serialization.h"
26 #include "network/networkpacket.h"
27 #include "network/peerhandler.h"
28 #include "util/serialize.h"
29 #include "util/numeric.h"
30 #include "util/string.h"
37 /******************************************************************************/
38 /* defines used for debugging and profiling */
39 /******************************************************************************/
43 #undef DEBUG_CONNECTION_KBPS
45 /* this mutex is used to achieve log message consistency */
46 std::mutex log_message_mutex;
49 MutexAutoLock loglock(log_message_mutex); \
53 //#define DEBUG_CONNECTION_KBPS
54 #undef DEBUG_CONNECTION_KBPS
58 static inline float CALC_DTIME(u64 lasttime, u64 curtime)
60 float value = ( curtime - lasttime) / 1000.0;
61 return MYMAX(MYMIN(value,0.1),0.0);
64 #define MAX_UDP_PEERS 65535
66 #define PING_TIMEOUT 5.0
68 /* maximum number of retries for reliable packets */
69 #define MAX_RELIABLE_RETRY 5
71 static u16 readPeerId(u8 *packetdata)
73 return readU16(&packetdata[4]);
75 static u8 readChannel(u8 *packetdata)
77 return readU8(&packetdata[6]);
80 BufferedPacket makePacket(Address &address, u8 *data, u32 datasize,
81 u32 protocol_id, u16 sender_peer_id, u8 channel)
83 u32 packet_size = datasize + BASE_HEADER_SIZE;
84 BufferedPacket p(packet_size);
87 writeU32(&p.data[0], protocol_id);
88 writeU16(&p.data[4], sender_peer_id);
89 writeU8(&p.data[6], channel);
91 memcpy(&p.data[BASE_HEADER_SIZE], data, datasize);
96 BufferedPacket makePacket(Address &address, SharedBuffer<u8> &data,
97 u32 protocol_id, u16 sender_peer_id, u8 channel)
99 return makePacket(address, *data, data.getSize(),
100 protocol_id, sender_peer_id, channel);
103 SharedBuffer<u8> makeOriginalPacket(
104 SharedBuffer<u8> data)
107 u32 packet_size = data.getSize() + header_size;
108 SharedBuffer<u8> b(packet_size);
110 writeU8(&(b[0]), TYPE_ORIGINAL);
111 if (data.getSize() > 0) {
112 memcpy(&(b[header_size]), *data, data.getSize());
117 std::list<SharedBuffer<u8> > makeSplitPacket(
118 SharedBuffer<u8> data,
122 // Chunk packets, containing the TYPE_SPLIT header
123 std::list<SharedBuffer<u8> > chunks;
125 u32 chunk_header_size = 7;
126 u32 maximum_data_size = chunksize_max - chunk_header_size;
132 end = start + maximum_data_size - 1;
133 if (end > data.getSize() - 1)
134 end = data.getSize() - 1;
136 u32 payload_size = end - start + 1;
137 u32 packet_size = chunk_header_size + payload_size;
139 SharedBuffer<u8> chunk(packet_size);
141 writeU8(&chunk[0], TYPE_SPLIT);
142 writeU16(&chunk[1], seqnum);
143 // [3] u16 chunk_count is written at next stage
144 writeU16(&chunk[5], chunk_num);
145 memcpy(&chunk[chunk_header_size], &data[start], payload_size);
147 chunks.push_back(chunk);
153 while(end != data.getSize() - 1);
155 for (SharedBuffer<u8> &chunk : chunks) {
157 writeU16(&(chunk[3]), chunk_count);
163 std::list<SharedBuffer<u8> > makeAutoSplitPacket(
164 SharedBuffer<u8> data,
168 u32 original_header_size = 1;
169 std::list<SharedBuffer<u8> > list;
170 if (data.getSize() + original_header_size > chunksize_max)
172 list = makeSplitPacket(data, chunksize_max, split_seqnum);
177 list.push_back(makeOriginalPacket(data));
181 SharedBuffer<u8> makeReliablePacket(
182 const SharedBuffer<u8> &data,
186 u32 packet_size = data.getSize() + header_size;
187 SharedBuffer<u8> b(packet_size);
189 writeU8(&b[0], TYPE_RELIABLE);
190 writeU16(&b[1], seqnum);
192 memcpy(&b[header_size], *data, data.getSize());
201 void ReliablePacketBuffer::print()
203 MutexAutoLock listlock(m_list_mutex);
204 LOG(dout_con<<"Dump of ReliablePacketBuffer:" << std::endl);
205 unsigned int index = 0;
206 for (BufferedPacket &bufferedPacket : m_list) {
207 u16 s = readU16(&(bufferedPacket.data[BASE_HEADER_SIZE+1]));
208 LOG(dout_con<<index<< ":" << s << std::endl);
212 bool ReliablePacketBuffer::empty()
214 MutexAutoLock listlock(m_list_mutex);
215 return m_list.empty();
218 u32 ReliablePacketBuffer::size()
223 bool ReliablePacketBuffer::containsPacket(u16 seqnum)
225 return !(findPacket(seqnum) == m_list.end());
228 RPBSearchResult ReliablePacketBuffer::findPacket(u16 seqnum)
230 std::list<BufferedPacket>::iterator i = m_list.begin();
231 for(; i != m_list.end(); ++i)
233 u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
234 /*dout_con<<"findPacket(): finding seqnum="<<seqnum
235 <<", comparing to s="<<s<<std::endl;*/
241 RPBSearchResult ReliablePacketBuffer::notFound()
245 bool ReliablePacketBuffer::getFirstSeqnum(u16& result)
247 MutexAutoLock listlock(m_list_mutex);
250 BufferedPacket p = *m_list.begin();
251 result = readU16(&p.data[BASE_HEADER_SIZE+1]);
255 BufferedPacket ReliablePacketBuffer::popFirst()
257 MutexAutoLock listlock(m_list_mutex);
259 throw NotFoundException("Buffer is empty");
260 BufferedPacket p = *m_list.begin();
261 m_list.erase(m_list.begin());
264 if (m_list_size == 0) {
265 m_oldest_non_answered_ack = 0;
267 m_oldest_non_answered_ack =
268 readU16(&(*m_list.begin()).data[BASE_HEADER_SIZE+1]);
272 BufferedPacket ReliablePacketBuffer::popSeqnum(u16 seqnum)
274 MutexAutoLock listlock(m_list_mutex);
275 RPBSearchResult r = findPacket(seqnum);
276 if (r == notFound()) {
277 LOG(dout_con<<"Sequence number: " << seqnum
278 << " not found in reliable buffer"<<std::endl);
279 throw NotFoundException("seqnum not found in buffer");
281 BufferedPacket p = *r;
284 RPBSearchResult next = r;
286 if (next != notFound()) {
287 u16 s = readU16(&(next->data[BASE_HEADER_SIZE+1]));
288 m_oldest_non_answered_ack = s;
294 if (m_list_size == 0)
295 { m_oldest_non_answered_ack = 0; }
297 { m_oldest_non_answered_ack = readU16(&(*m_list.begin()).data[BASE_HEADER_SIZE+1]); }
300 void ReliablePacketBuffer::insert(BufferedPacket &p,u16 next_expected)
302 MutexAutoLock listlock(m_list_mutex);
303 if (p.data.getSize() < BASE_HEADER_SIZE + 3) {
304 errorstream << "ReliablePacketBuffer::insert(): Invalid data size for "
305 "reliable packet" << std::endl;
308 u8 type = readU8(&p.data[BASE_HEADER_SIZE + 0]);
309 if (type != TYPE_RELIABLE) {
310 errorstream << "ReliablePacketBuffer::insert(): type is not reliable"
314 u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE + 1]);
316 if (!seqnum_in_window(seqnum, next_expected, MAX_RELIABLE_WINDOW_SIZE)) {
317 errorstream << "ReliablePacketBuffer::insert(): seqnum is outside of "
318 "expected window " << std::endl;
321 if (seqnum == next_expected) {
322 errorstream << "ReliablePacketBuffer::insert(): seqnum is next expected"
328 sanity_check(m_list_size <= SEQNUM_MAX+1); // FIXME: Handle the error?
330 // Find the right place for the packet and insert it there
331 // If list is empty, just add it
335 m_oldest_non_answered_ack = seqnum;
340 // Otherwise find the right place
341 std::list<BufferedPacket>::iterator i = m_list.begin();
342 // Find the first packet in the list which has a higher seqnum
343 u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
345 /* case seqnum is smaller then next_expected seqnum */
346 /* this is true e.g. on wrap around */
347 if (seqnum < next_expected) {
348 while(((s < seqnum) || (s >= next_expected)) && (i != m_list.end())) {
350 if (i != m_list.end())
351 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
354 /* non wrap around case (at least for incoming and next_expected */
357 while(((s < seqnum) && (s >= next_expected)) && (i != m_list.end())) {
359 if (i != m_list.end())
360 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
366 (readU16(&(i->data[BASE_HEADER_SIZE+1])) != seqnum) ||
367 (i->data.getSize() != p.data.getSize()) ||
368 (i->address != p.address)
371 /* if this happens your maximum transfer window may be to big */
373 "Duplicated seqnum %d non matching packet detected:\n",
375 fprintf(stderr, "Old: seqnum: %05d size: %04d, address: %s\n",
376 readU16(&(i->data[BASE_HEADER_SIZE+1])),i->data.getSize(),
377 i->address.serializeString().c_str());
378 fprintf(stderr, "New: seqnum: %05d size: %04u, address: %s\n",
379 readU16(&(p.data[BASE_HEADER_SIZE+1])),p.data.getSize(),
380 p.address.serializeString().c_str());
381 throw IncomingDataCorruption("duplicated packet isn't same as original one");
384 /* nothing to do this seems to be a resent packet */
385 /* for paranoia reason data should be compared */
388 /* insert or push back */
389 else if (i != m_list.end()) {
396 /* update last packet number */
397 m_oldest_non_answered_ack = readU16(&(*m_list.begin()).data[BASE_HEADER_SIZE+1]);
400 void ReliablePacketBuffer::incrementTimeouts(float dtime)
402 MutexAutoLock listlock(m_list_mutex);
403 for (BufferedPacket &bufferedPacket : m_list) {
404 bufferedPacket.time += dtime;
405 bufferedPacket.totaltime += dtime;
409 std::list<BufferedPacket> ReliablePacketBuffer::getTimedOuts(float timeout,
410 unsigned int max_packets)
412 MutexAutoLock listlock(m_list_mutex);
413 std::list<BufferedPacket> timed_outs;
414 for (BufferedPacket &bufferedPacket : m_list) {
415 if (bufferedPacket.time >= timeout) {
416 timed_outs.push_back(bufferedPacket);
418 //this packet will be sent right afterwards reset timeout here
419 bufferedPacket.time = 0.0f;
420 if (timed_outs.size() >= max_packets)
431 IncomingSplitBuffer::~IncomingSplitBuffer()
433 MutexAutoLock listlock(m_map_mutex);
434 for (auto &i : m_buf) {
439 This will throw a GotSplitPacketException when a full
440 split packet is constructed.
442 SharedBuffer<u8> IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
444 MutexAutoLock listlock(m_map_mutex);
445 u32 headersize = BASE_HEADER_SIZE + 7;
446 if (p.data.getSize() < headersize) {
447 errorstream << "Invalid data size for split packet" << std::endl;
448 return SharedBuffer<u8>();
450 u8 type = readU8(&p.data[BASE_HEADER_SIZE+0]);
451 u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE+1]);
452 u16 chunk_count = readU16(&p.data[BASE_HEADER_SIZE+3]);
453 u16 chunk_num = readU16(&p.data[BASE_HEADER_SIZE+5]);
455 if (type != TYPE_SPLIT) {
456 errorstream << "IncomingSplitBuffer::insert(): type is not split"
458 return SharedBuffer<u8>();
461 // Add if doesn't exist
462 if (m_buf.find(seqnum) == m_buf.end())
464 IncomingSplitPacket *sp = new IncomingSplitPacket();
465 sp->chunk_count = chunk_count;
466 sp->reliable = reliable;
470 IncomingSplitPacket *sp = m_buf[seqnum];
472 // TODO: These errors should be thrown or something? Dunno.
473 if (chunk_count != sp->chunk_count)
474 LOG(derr_con<<"Connection: WARNING: chunk_count="<<chunk_count
475 <<" != sp->chunk_count="<<sp->chunk_count
477 if (reliable != sp->reliable)
478 LOG(derr_con<<"Connection: WARNING: reliable="<<reliable
479 <<" != sp->reliable="<<sp->reliable
482 // If chunk already exists, ignore it.
483 // Sometimes two identical packets may arrive when there is network
484 // lag and the server re-sends stuff.
485 if (sp->chunks.find(chunk_num) != sp->chunks.end())
486 return SharedBuffer<u8>();
488 // Cut chunk data out of packet
489 u32 chunkdatasize = p.data.getSize() - headersize;
490 SharedBuffer<u8> chunkdata(chunkdatasize);
491 memcpy(*chunkdata, &(p.data[headersize]), chunkdatasize);
493 // Set chunk data in buffer
494 sp->chunks[chunk_num] = chunkdata;
496 // If not all chunks are received, return empty buffer
497 if (!sp->allReceived())
498 return SharedBuffer<u8>();
500 // Calculate total size
502 for (const auto &chunk : sp->chunks) {
503 totalsize += chunk.second.getSize();
506 SharedBuffer<u8> fulldata(totalsize);
508 // Copy chunks to data buffer
510 for(u32 chunk_i=0; chunk_i<sp->chunk_count;
513 SharedBuffer<u8> buf = sp->chunks[chunk_i];
514 u16 chunkdatasize = buf.getSize();
515 memcpy(&fulldata[start], *buf, chunkdatasize);
516 start += chunkdatasize;;
519 // Remove sp from buffer
525 void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
527 std::list<u16> remove_queue;
529 MutexAutoLock listlock(m_map_mutex);
530 for (auto &i : m_buf) {
531 IncomingSplitPacket *p = i.second;
532 // Reliable ones are not removed by timeout
536 if (p->time >= timeout)
537 remove_queue.push_back(i.first);
540 for (u16 j : remove_queue) {
541 MutexAutoLock listlock(m_map_mutex);
542 LOG(dout_con<<"NOTE: Removing timed out unreliable split packet"<<std::endl);
552 void ConnectionCommand::send(u16 peer_id_, u8 channelnum_, NetworkPacket *pkt,
557 channelnum = channelnum_;
558 data = pkt->oldForgePacket();
559 reliable = reliable_;
566 u16 Channel::readNextIncomingSeqNum()
568 MutexAutoLock internal(m_internal_mutex);
569 return next_incoming_seqnum;
572 u16 Channel::incNextIncomingSeqNum()
574 MutexAutoLock internal(m_internal_mutex);
575 u16 retval = next_incoming_seqnum;
576 next_incoming_seqnum++;
580 u16 Channel::readNextSplitSeqNum()
582 MutexAutoLock internal(m_internal_mutex);
583 return next_outgoing_split_seqnum;
585 void Channel::setNextSplitSeqNum(u16 seqnum)
587 MutexAutoLock internal(m_internal_mutex);
588 next_outgoing_split_seqnum = seqnum;
591 u16 Channel::getOutgoingSequenceNumber(bool& successful)
593 MutexAutoLock internal(m_internal_mutex);
594 u16 retval = next_outgoing_seqnum;
595 u16 lowest_unacked_seqnumber;
597 /* shortcut if there ain't any packet in outgoing list */
598 if (outgoing_reliables_sent.empty())
600 next_outgoing_seqnum++;
604 if (outgoing_reliables_sent.getFirstSeqnum(lowest_unacked_seqnumber))
606 if (lowest_unacked_seqnumber < next_outgoing_seqnum) {
607 // ugly cast but this one is required in order to tell compiler we
608 // know about difference of two unsigned may be negative in general
609 // but we already made sure it won't happen in this case
610 if (((u16)(next_outgoing_seqnum - lowest_unacked_seqnumber)) > window_size) {
616 // ugly cast but this one is required in order to tell compiler we
617 // know about difference of two unsigned may be negative in general
618 // but we already made sure it won't happen in this case
619 if ((next_outgoing_seqnum + (u16)(SEQNUM_MAX - lowest_unacked_seqnumber)) >
627 next_outgoing_seqnum++;
631 u16 Channel::readOutgoingSequenceNumber()
633 MutexAutoLock internal(m_internal_mutex);
634 return next_outgoing_seqnum;
637 bool Channel::putBackSequenceNumber(u16 seqnum)
639 if (((seqnum + 1) % (SEQNUM_MAX+1)) == next_outgoing_seqnum) {
641 next_outgoing_seqnum = seqnum;
647 void Channel::UpdateBytesSent(unsigned int bytes, unsigned int packets)
649 MutexAutoLock internal(m_internal_mutex);
650 current_bytes_transfered += bytes;
651 current_packet_successfull += packets;
654 void Channel::UpdateBytesReceived(unsigned int bytes) {
655 MutexAutoLock internal(m_internal_mutex);
656 current_bytes_received += bytes;
659 void Channel::UpdateBytesLost(unsigned int bytes)
661 MutexAutoLock internal(m_internal_mutex);
662 current_bytes_lost += bytes;
666 void Channel::UpdatePacketLossCounter(unsigned int count)
668 MutexAutoLock internal(m_internal_mutex);
669 current_packet_loss += count;
672 void Channel::UpdatePacketTooLateCounter()
674 MutexAutoLock internal(m_internal_mutex);
675 current_packet_too_late++;
678 void Channel::UpdateTimers(float dtime,bool legacy_peer)
680 bpm_counter += dtime;
681 packet_loss_counter += dtime;
683 if (packet_loss_counter > 1.0)
685 packet_loss_counter -= 1.0;
687 unsigned int packet_loss = 11; /* use a neutral value for initialization */
688 unsigned int packets_successfull = 0;
689 //unsigned int packet_too_late = 0;
691 bool reasonable_amount_of_data_transmitted = false;
694 MutexAutoLock internal(m_internal_mutex);
695 packet_loss = current_packet_loss;
696 //packet_too_late = current_packet_too_late;
697 packets_successfull = current_packet_successfull;
699 if (current_bytes_transfered > (unsigned int) (window_size*512/2))
701 reasonable_amount_of_data_transmitted = true;
703 current_packet_loss = 0;
704 current_packet_too_late = 0;
705 current_packet_successfull = 0;
708 /* dynamic window size is only available for non legacy peers */
710 float successfull_to_lost_ratio = 0.0;
713 if (packets_successfull > 0) {
714 successfull_to_lost_ratio = packet_loss/packets_successfull;
716 else if (packet_loss > 0)
720 MIN_RELIABLE_WINDOW_SIZE);
726 if ((successfull_to_lost_ratio < 0.01) &&
727 (window_size < MAX_RELIABLE_WINDOW_SIZE))
729 /* don't even think about increasing if we didn't even
730 * use major parts of our window */
731 if (reasonable_amount_of_data_transmitted)
734 MAX_RELIABLE_WINDOW_SIZE);
736 else if ((successfull_to_lost_ratio < 0.05) &&
737 (window_size < MAX_RELIABLE_WINDOW_SIZE))
739 /* don't even think about increasing if we didn't even
740 * use major parts of our window */
741 if (reasonable_amount_of_data_transmitted)
744 MAX_RELIABLE_WINDOW_SIZE);
746 else if (successfull_to_lost_ratio > 0.15)
750 MIN_RELIABLE_WINDOW_SIZE);
752 else if (successfull_to_lost_ratio > 0.1)
756 MIN_RELIABLE_WINDOW_SIZE);
762 if (bpm_counter > 10.0)
765 MutexAutoLock internal(m_internal_mutex);
767 (((float) current_bytes_transfered)/bpm_counter)/1024.0;
768 current_bytes_transfered = 0;
770 (((float) current_bytes_lost)/bpm_counter)/1024.0;
771 current_bytes_lost = 0;
773 (((float) current_bytes_received)/bpm_counter)/1024.0;
774 current_bytes_received = 0;
778 if (cur_kbps > max_kbps)
783 if (cur_kbps_lost > max_kbps_lost)
785 max_kbps_lost = cur_kbps_lost;
788 if (cur_incoming_kbps > max_incoming_kbps) {
789 max_incoming_kbps = cur_incoming_kbps;
792 rate_samples = MYMIN(rate_samples+1,10);
793 float old_fraction = ((float) (rate_samples-1) )/( (float) rate_samples);
794 avg_kbps = avg_kbps * old_fraction +
795 cur_kbps * (1.0 - old_fraction);
796 avg_kbps_lost = avg_kbps_lost * old_fraction +
797 cur_kbps_lost * (1.0 - old_fraction);
798 avg_incoming_kbps = avg_incoming_kbps * old_fraction +
799 cur_incoming_kbps * (1.0 - old_fraction);
808 PeerHelper::PeerHelper(Peer* peer) :
811 if (peer && !peer->IncUseCount())
815 PeerHelper::~PeerHelper()
818 m_peer->DecUseCount();
823 PeerHelper& PeerHelper::operator=(Peer* peer)
826 if (peer && !peer->IncUseCount())
831 Peer* PeerHelper::operator->() const
836 Peer* PeerHelper::operator&() const
841 bool PeerHelper::operator!() {
845 bool PeerHelper::operator!=(void* ptr)
847 return ((void*) m_peer != ptr);
850 bool Peer::IncUseCount()
852 MutexAutoLock lock(m_exclusive_access_mutex);
854 if (!m_pending_deletion) {
862 void Peer::DecUseCount()
865 MutexAutoLock lock(m_exclusive_access_mutex);
866 sanity_check(m_usage > 0);
869 if (!((m_pending_deletion) && (m_usage == 0)))
875 void Peer::RTTStatistics(float rtt, const std::string &profiler_id,
876 unsigned int num_samples) {
878 if (m_last_rtt > 0) {
879 /* set min max values */
880 if (rtt < m_rtt.min_rtt)
882 if (rtt >= m_rtt.max_rtt)
885 /* do average calculation */
886 if (m_rtt.avg_rtt < 0.0)
889 m_rtt.avg_rtt = m_rtt.avg_rtt * (num_samples/(num_samples-1)) +
890 rtt * (1/num_samples);
892 /* do jitter calculation */
894 //just use some neutral value at beginning
895 float jitter = m_rtt.jitter_min;
897 if (rtt > m_last_rtt)
898 jitter = rtt-m_last_rtt;
900 if (rtt <= m_last_rtt)
901 jitter = m_last_rtt - rtt;
903 if (jitter < m_rtt.jitter_min)
904 m_rtt.jitter_min = jitter;
905 if (jitter >= m_rtt.jitter_max)
906 m_rtt.jitter_max = jitter;
908 if (m_rtt.jitter_avg < 0.0)
909 m_rtt.jitter_avg = jitter;
911 m_rtt.jitter_avg = m_rtt.jitter_avg * (num_samples/(num_samples-1)) +
912 jitter * (1/num_samples);
914 if (!profiler_id.empty()) {
915 g_profiler->graphAdd(profiler_id + "_rtt", rtt);
916 g_profiler->graphAdd(profiler_id + "_jitter", jitter);
919 /* save values required for next loop */
923 bool Peer::isTimedOut(float timeout)
925 MutexAutoLock lock(m_exclusive_access_mutex);
926 u64 current_time = porting::getTimeMs();
928 float dtime = CALC_DTIME(m_last_timeout_check,current_time);
929 m_last_timeout_check = current_time;
931 m_timeout_counter += dtime;
933 return m_timeout_counter > timeout;
939 MutexAutoLock usage_lock(m_exclusive_access_mutex);
940 m_pending_deletion = true;
945 PROFILE(std::stringstream peerIdentifier1);
946 PROFILE(peerIdentifier1 << "runTimeouts[" << m_connection->getDesc()
947 << ";" << id << ";RELIABLE]");
948 PROFILE(g_profiler->remove(peerIdentifier1.str()));
949 PROFILE(std::stringstream peerIdentifier2);
950 PROFILE(peerIdentifier2 << "sendPackets[" << m_connection->getDesc()
951 << ";" << id << ";RELIABLE]");
952 PROFILE(ScopeProfiler peerprofiler(g_profiler, peerIdentifier2.str(), SPT_AVG));
957 UDPPeer::UDPPeer(u16 a_id, Address a_address, Connection* connection) :
958 Peer(a_address,a_id,connection)
962 bool UDPPeer::getAddress(MTProtocols type,Address& toset)
964 if ((type == MTP_UDP) || (type == MTP_MINETEST_RELIABLE_UDP) || (type == MTP_PRIMARY))
973 void UDPPeer::setNonLegacyPeer()
975 m_legacy_peer = false;
976 for(unsigned int i=0; i< CHANNEL_COUNT; i++)
978 channels->setWindowSize(g_settings->getU16("max_packets_per_iteration"));
982 void UDPPeer::reportRTT(float rtt)
987 RTTStatistics(rtt,"rudp",MAX_RELIABLE_WINDOW_SIZE*10);
989 float timeout = getStat(AVG_RTT) * RESEND_TIMEOUT_FACTOR;
990 if (timeout < RESEND_TIMEOUT_MIN)
991 timeout = RESEND_TIMEOUT_MIN;
992 if (timeout > RESEND_TIMEOUT_MAX)
993 timeout = RESEND_TIMEOUT_MAX;
995 MutexAutoLock usage_lock(m_exclusive_access_mutex);
996 resend_timeout = timeout;
999 bool UDPPeer::Ping(float dtime,SharedBuffer<u8>& data)
1001 m_ping_timer += dtime;
1002 if (m_ping_timer >= PING_TIMEOUT)
1004 // Create and send PING packet
1005 writeU8(&data[0], TYPE_CONTROL);
1006 writeU8(&data[1], CONTROLTYPE_PING);
1013 void UDPPeer::PutReliableSendCommand(ConnectionCommand &c,
1014 unsigned int max_packet_size)
1016 if (m_pending_disconnect)
1019 if ( channels[c.channelnum].queued_commands.empty() &&
1020 /* don't queue more packets then window size */
1021 (channels[c.channelnum].queued_reliables.size()
1022 < (channels[c.channelnum].getWindowSize()/2))) {
1023 LOG(dout_con<<m_connection->getDesc()
1024 <<" processing reliable command for peer id: " << c.peer_id
1025 <<" data size: " << c.data.getSize() << std::endl);
1026 if (!processReliableSendCommand(c,max_packet_size)) {
1027 channels[c.channelnum].queued_commands.push_back(c);
1031 LOG(dout_con<<m_connection->getDesc()
1032 <<" Queueing reliable command for peer id: " << c.peer_id
1033 <<" data size: " << c.data.getSize() <<std::endl);
1034 channels[c.channelnum].queued_commands.push_back(c);
1038 bool UDPPeer::processReliableSendCommand(
1039 ConnectionCommand &c,
1040 unsigned int max_packet_size)
1042 if (m_pending_disconnect)
1045 u32 chunksize_max = max_packet_size
1047 - RELIABLE_HEADER_SIZE;
1049 sanity_check(c.data.getSize() < MAX_RELIABLE_WINDOW_SIZE*512);
1051 std::list<SharedBuffer<u8> > originals;
1052 u16 split_sequence_number = channels[c.channelnum].readNextSplitSeqNum();
1056 originals.emplace_back(c.data);
1059 originals = makeAutoSplitPacket(c.data, chunksize_max,split_sequence_number);
1060 channels[c.channelnum].setNextSplitSeqNum(split_sequence_number);
1063 bool have_sequence_number = true;
1064 bool have_initial_sequence_number = false;
1065 std::queue<BufferedPacket> toadd;
1066 volatile u16 initial_sequence_number = 0;
1068 for (SharedBuffer<u8> &original : originals) {
1069 u16 seqnum = channels[c.channelnum].getOutgoingSequenceNumber(have_sequence_number);
1071 /* oops, we don't have enough sequence numbers to send this packet */
1072 if (!have_sequence_number)
1075 if (!have_initial_sequence_number)
1077 initial_sequence_number = seqnum;
1078 have_initial_sequence_number = true;
1081 SharedBuffer<u8> reliable = makeReliablePacket(original, seqnum);
1083 // Add base headers and make a packet
1084 BufferedPacket p = con::makePacket(address, reliable,
1085 m_connection->GetProtocolID(), m_connection->GetPeerID(),
1091 if (have_sequence_number) {
1092 volatile u16 pcount = 0;
1093 while (!toadd.empty()) {
1094 BufferedPacket p = toadd.front();
1096 // LOG(dout_con<<connection->getDesc()
1097 // << " queuing reliable packet for peer_id: " << c.peer_id
1098 // << " channel: " << (c.channelnum&0xFF)
1099 // << " seqnum: " << readU16(&p.data[BASE_HEADER_SIZE+1])
1101 channels[c.channelnum].queued_reliables.push(p);
1104 sanity_check(channels[c.channelnum].queued_reliables.size() < 0xFFFF);
1108 volatile u16 packets_available = toadd.size();
1109 /* we didn't get a single sequence number no need to fill queue */
1110 if (!have_initial_sequence_number) {
1114 while (!toadd.empty()) {
1118 bool successfully_put_back_sequence_number
1119 = channels[c.channelnum].putBackSequenceNumber(
1120 (initial_sequence_number+toadd.size() % (SEQNUM_MAX+1)));
1122 FATAL_ERROR_IF(!successfully_put_back_sequence_number, "error");
1125 LOG(dout_con<<m_connection->getDesc()
1126 << " Windowsize exceeded on reliable sending "
1127 << c.data.getSize() << " bytes"
1128 << std::endl << "\t\tinitial_sequence_number: "
1129 << initial_sequence_number
1130 << std::endl << "\t\tgot at most : "
1131 << packets_available << " packets"
1132 << std::endl << "\t\tpackets queued : "
1133 << channels[c.channelnum].outgoing_reliables_sent.size()
1139 void UDPPeer::RunCommandQueues(
1140 unsigned int max_packet_size,
1141 unsigned int maxcommands,
1142 unsigned int maxtransfer)
1145 for (Channel &channel : channels) {
1146 unsigned int commands_processed = 0;
1148 if ((!channel.queued_commands.empty()) &&
1149 (channel.queued_reliables.size() < maxtransfer) &&
1150 (commands_processed < maxcommands)) {
1152 ConnectionCommand c = channel.queued_commands.front();
1154 LOG(dout_con << m_connection->getDesc()
1155 << " processing queued reliable command " << std::endl);
1157 // Packet is processed, remove it from queue
1158 if (processReliableSendCommand(c,max_packet_size)) {
1159 channel.queued_commands.pop_front();
1161 LOG(dout_con << m_connection->getDesc()
1162 << " Failed to queue packets for peer_id: " << c.peer_id
1163 << ", delaying sending of " << c.data.getSize()
1164 << " bytes" << std::endl);
1167 catch (ItemNotFoundException &e) {
1168 // intentionally empty
1174 u16 UDPPeer::getNextSplitSequenceNumber(u8 channel)
1176 assert(channel < CHANNEL_COUNT); // Pre-condition
1177 return channels[channel].readNextSplitSeqNum();
1180 void UDPPeer::setNextSplitSequenceNumber(u8 channel, u16 seqnum)
1182 assert(channel < CHANNEL_COUNT); // Pre-condition
1183 channels[channel].setNextSplitSeqNum(seqnum);
1186 SharedBuffer<u8> UDPPeer::addSpiltPacket(u8 channel,
1187 BufferedPacket toadd,
1190 assert(channel < CHANNEL_COUNT); // Pre-condition
1191 return channels[channel].incoming_splits.insert(toadd,reliable);
1194 /******************************************************************************/
1195 /* Connection Threads */
1196 /******************************************************************************/
1198 ConnectionSendThread::ConnectionSendThread(unsigned int max_packet_size,
1200 Thread("ConnectionSend"),
1201 m_max_packet_size(max_packet_size),
1203 m_max_data_packets_per_iteration(g_settings->getU16("max_packets_per_iteration"))
1207 void * ConnectionSendThread::run()
1209 assert(m_connection);
1211 LOG(dout_con<<m_connection->getDesc()
1212 <<"ConnectionSend thread started"<<std::endl);
1214 u64 curtime = porting::getTimeMs();
1215 u64 lasttime = curtime;
1217 PROFILE(std::stringstream ThreadIdentifier);
1218 PROFILE(ThreadIdentifier << "ConnectionSend: [" << m_connection->getDesc() << "]");
1220 /* if stop is requested don't stop immediately but try to send all */
1222 while(!stopRequested() || packetsQueued()) {
1223 BEGIN_DEBUG_EXCEPTION_HANDLER
1224 PROFILE(ScopeProfiler sp(g_profiler, ThreadIdentifier.str(), SPT_AVG));
1226 m_iteration_packets_avaialble = m_max_data_packets_per_iteration;
1228 /* wait for trigger or timeout */
1229 m_send_sleep_semaphore.wait(50);
1231 /* remove all triggers */
1232 while(m_send_sleep_semaphore.wait(0)) {}
1235 curtime = porting::getTimeMs();
1236 float dtime = CALC_DTIME(lasttime,curtime);
1238 /* first do all the reliable stuff */
1241 /* translate commands to packets */
1242 ConnectionCommand c = m_connection->m_command_queue.pop_frontNoEx(0);
1243 while(c.type != CONNCMD_NONE)
1246 processReliableCommand(c);
1248 processNonReliableCommand(c);
1250 c = m_connection->m_command_queue.pop_frontNoEx(0);
1253 /* send non reliable packets */
1256 END_DEBUG_EXCEPTION_HANDLER
1259 PROFILE(g_profiler->remove(ThreadIdentifier.str()));
1263 void ConnectionSendThread::Trigger()
1265 m_send_sleep_semaphore.post();
1268 bool ConnectionSendThread::packetsQueued()
1270 std::list<u16> peerIds = m_connection->getPeerIDs();
1272 if (!m_outgoing_queue.empty() && !peerIds.empty())
1275 for (u16 peerId : peerIds) {
1276 PeerHelper peer = m_connection->getPeerNoEx(peerId);
1281 if (dynamic_cast<UDPPeer*>(&peer) == 0)
1284 for (Channel &channel : (dynamic_cast<UDPPeer *>(&peer))->channels) {
1285 if (!channel.queued_commands.empty()) {
1295 void ConnectionSendThread::runTimeouts(float dtime)
1297 std::list<u16> timeouted_peers;
1298 std::list<u16> peerIds = m_connection->getPeerIDs();
1300 for (u16 &peerId : peerIds) {
1301 PeerHelper peer = m_connection->getPeerNoEx(peerId);
1306 if (dynamic_cast<UDPPeer*>(&peer) == 0)
1309 PROFILE(std::stringstream peerIdentifier);
1310 PROFILE(peerIdentifier << "runTimeouts[" << m_connection->getDesc()
1311 << ";" << peerId << ";RELIABLE]");
1312 PROFILE(ScopeProfiler peerprofiler(g_profiler, peerIdentifier.str(), SPT_AVG));
1314 SharedBuffer<u8> data(2); // data for sending ping, required here because of goto
1319 if (peer->isTimedOut(m_timeout))
1321 infostream<<m_connection->getDesc()
1322 <<"RunTimeouts(): Peer "<<peer->id
1324 <<" (source=peer->timeout_counter)"
1326 // Add peer to the list
1327 timeouted_peers.push_back(peer->id);
1328 // Don't bother going through the buffers of this one
1332 float resend_timeout = dynamic_cast<UDPPeer*>(&peer)->getResendTimeout();
1333 bool retry_count_exceeded = false;
1334 for (Channel &channel : (dynamic_cast<UDPPeer *>(&peer))->channels) {
1335 std::list<BufferedPacket> timed_outs;
1337 if (dynamic_cast<UDPPeer*>(&peer)->getLegacyPeer())
1338 channel.setWindowSize(g_settings->getU16("workaround_window_size"));
1340 // Remove timed out incomplete unreliable split packets
1341 channel.incoming_splits.removeUnreliableTimedOuts(dtime, m_timeout);
1343 // Increment reliable packet times
1344 channel.outgoing_reliables_sent.incrementTimeouts(dtime);
1346 unsigned int numpeers = m_connection->m_peers.size();
1351 // Re-send timed out outgoing reliables
1352 timed_outs = channel.outgoing_reliables_sent.getTimedOuts(resend_timeout,
1353 (m_max_data_packets_per_iteration/numpeers));
1355 channel.UpdatePacketLossCounter(timed_outs.size());
1356 g_profiler->graphAdd("packets_lost", timed_outs.size());
1358 m_iteration_packets_avaialble -= timed_outs.size();
1360 for(std::list<BufferedPacket>::iterator k = timed_outs.begin();
1361 k != timed_outs.end(); ++k)
1363 u16 peer_id = readPeerId(*(k->data));
1364 u8 channelnum = readChannel(*(k->data));
1365 u16 seqnum = readU16(&(k->data[BASE_HEADER_SIZE+1]));
1367 channel.UpdateBytesLost(k->data.getSize());
1370 if (k-> resend_count > MAX_RELIABLE_RETRY) {
1371 retry_count_exceeded = true;
1372 timeouted_peers.push_back(peer->id);
1373 /* no need to check additional packets if a single one did timeout*/
1377 LOG(derr_con<<m_connection->getDesc()
1378 <<"RE-SENDING timed-out RELIABLE to "
1379 << k->address.serializeString()
1380 << "(t/o="<<resend_timeout<<"): "
1381 <<"from_peer_id="<<peer_id
1382 <<", channel="<<((int)channelnum&0xff)
1383 <<", seqnum="<<seqnum
1388 // do not handle rtt here as we can't decide if this packet was
1389 // lost or really takes more time to transmit
1392 if (retry_count_exceeded) {
1393 break; /* no need to check other channels if we already did timeout */
1396 channel.UpdateTimers(dtime,dynamic_cast<UDPPeer*>(&peer)->getLegacyPeer());
1399 /* skip to next peer if we did timeout */
1400 if (retry_count_exceeded)
1403 /* send ping if necessary */
1404 if (dynamic_cast<UDPPeer*>(&peer)->Ping(dtime,data)) {
1405 LOG(dout_con<<m_connection->getDesc()
1406 <<"Sending ping for peer_id: "
1407 << dynamic_cast<UDPPeer*>(&peer)->id <<std::endl);
1408 /* this may fail if there ain't a sequence number left */
1409 if (!rawSendAsPacket(dynamic_cast<UDPPeer*>(&peer)->id, 0, data, true))
1411 //retrigger with reduced ping interval
1412 dynamic_cast<UDPPeer*>(&peer)->Ping(4.0,data);
1416 dynamic_cast<UDPPeer*>(&peer)->RunCommandQueues(m_max_packet_size,
1417 m_max_commands_per_iteration,
1418 m_max_packets_requeued);
1421 // Remove timed out peers
1422 for (u16 timeouted_peer : timeouted_peers) {
1423 LOG(derr_con << m_connection->getDesc()
1424 << "RunTimeouts(): Removing peer "<< timeouted_peer <<std::endl);
1425 m_connection->deletePeer(timeouted_peer, true);
1429 void ConnectionSendThread::rawSend(const BufferedPacket &packet)
1432 m_connection->m_udpSocket.Send(packet.address, *packet.data,
1433 packet.data.getSize());
1434 LOG(dout_con <<m_connection->getDesc()
1435 << " rawSend: " << packet.data.getSize()
1436 << " bytes sent" << std::endl);
1437 } catch(SendFailedException &e) {
1438 LOG(derr_con<<m_connection->getDesc()
1439 <<"Connection::rawSend(): SendFailedException: "
1440 <<packet.address.serializeString()<<std::endl);
1444 void ConnectionSendThread::sendAsPacketReliable(BufferedPacket& p, Channel* channel)
1447 p.absolute_send_time = porting::getTimeMs();
1448 // Buffer the packet
1449 channel->outgoing_reliables_sent.insert(p,
1450 (channel->readOutgoingSequenceNumber() - MAX_RELIABLE_WINDOW_SIZE)
1451 % (MAX_RELIABLE_WINDOW_SIZE+1));
1453 catch(AlreadyExistsException &e)
1455 LOG(derr_con<<m_connection->getDesc()
1456 <<"WARNING: Going to send a reliable packet"
1457 <<" in outgoing buffer" <<std::endl);
1464 bool ConnectionSendThread::rawSendAsPacket(u16 peer_id, u8 channelnum,
1465 SharedBuffer<u8> data, bool reliable)
1467 PeerHelper peer = m_connection->getPeerNoEx(peer_id);
1469 LOG(dout_con<<m_connection->getDesc()
1470 <<" INFO: dropped packet for non existent peer_id: "
1471 << peer_id << std::endl);
1472 FATAL_ERROR_IF(!reliable, "Trying to send raw packet reliable but no peer found!");
1475 Channel *channel = &(dynamic_cast<UDPPeer*>(&peer)->channels[channelnum]);
1479 bool have_sequence_number_for_raw_packet = true;
1481 channel->getOutgoingSequenceNumber(have_sequence_number_for_raw_packet);
1483 if (!have_sequence_number_for_raw_packet)
1486 SharedBuffer<u8> reliable = makeReliablePacket(data, seqnum);
1487 Address peer_address;
1488 peer->getAddress(MTP_MINETEST_RELIABLE_UDP, peer_address);
1490 // Add base headers and make a packet
1491 BufferedPacket p = con::makePacket(peer_address, reliable,
1492 m_connection->GetProtocolID(), m_connection->GetPeerID(),
1495 // first check if our send window is already maxed out
1496 if (channel->outgoing_reliables_sent.size()
1497 < channel->getWindowSize()) {
1498 LOG(dout_con<<m_connection->getDesc()
1499 <<" INFO: sending a reliable packet to peer_id " << peer_id
1500 <<" channel: " << channelnum
1501 <<" seqnum: " << seqnum << std::endl);
1502 sendAsPacketReliable(p,channel);
1506 LOG(dout_con<<m_connection->getDesc()
1507 <<" INFO: queueing reliable packet for peer_id: " << peer_id
1508 <<" channel: " << channelnum
1509 <<" seqnum: " << seqnum << std::endl);
1510 channel->queued_reliables.push(p);
1514 Address peer_address;
1515 if (peer->getAddress(MTP_UDP, peer_address)) {
1516 // Add base headers and make a packet
1517 BufferedPacket p = con::makePacket(peer_address, data,
1518 m_connection->GetProtocolID(), m_connection->GetPeerID(),
1526 LOG(dout_con << m_connection->getDesc()
1527 << " INFO: dropped unreliable packet for peer_id: " << peer_id
1528 << " because of (yet) missing udp address" << std::endl);
1532 void ConnectionSendThread::processReliableCommand(ConnectionCommand &c)
1534 assert(c.reliable); // Pre-condition
1538 LOG(dout_con<<m_connection->getDesc()
1539 <<"UDP processing reliable CONNCMD_NONE"<<std::endl);
1543 LOG(dout_con<<m_connection->getDesc()
1544 <<"UDP processing reliable CONNCMD_SEND"<<std::endl);
1548 case CONNCMD_SEND_TO_ALL:
1549 LOG(dout_con<<m_connection->getDesc()
1550 <<"UDP processing CONNCMD_SEND_TO_ALL"<<std::endl);
1551 sendToAllReliable(c);
1554 case CONCMD_CREATE_PEER:
1555 LOG(dout_con<<m_connection->getDesc()
1556 <<"UDP processing reliable CONCMD_CREATE_PEER"<<std::endl);
1557 if (!rawSendAsPacket(c.peer_id,c.channelnum,c.data,c.reliable))
1559 /* put to queue if we couldn't send it immediately */
1564 case CONCMD_DISABLE_LEGACY:
1565 LOG(dout_con<<m_connection->getDesc()
1566 <<"UDP processing reliable CONCMD_DISABLE_LEGACY"<<std::endl);
1567 if (!rawSendAsPacket(c.peer_id,c.channelnum,c.data,c.reliable))
1569 /* put to queue if we couldn't send it immediately */
1575 case CONNCMD_CONNECT:
1576 case CONNCMD_DISCONNECT:
1578 FATAL_ERROR("Got command that shouldn't be reliable as reliable command");
1580 LOG(dout_con<<m_connection->getDesc()
1581 <<" Invalid reliable command type: " << c.type <<std::endl);
1586 void ConnectionSendThread::processNonReliableCommand(ConnectionCommand &c)
1588 assert(!c.reliable); // Pre-condition
1592 LOG(dout_con<<m_connection->getDesc()
1593 <<" UDP processing CONNCMD_NONE"<<std::endl);
1596 LOG(dout_con<<m_connection->getDesc()
1597 <<" UDP processing CONNCMD_SERVE port="
1598 <<c.address.serializeString()<<std::endl);
1601 case CONNCMD_CONNECT:
1602 LOG(dout_con<<m_connection->getDesc()
1603 <<" UDP processing CONNCMD_CONNECT"<<std::endl);
1606 case CONNCMD_DISCONNECT:
1607 LOG(dout_con<<m_connection->getDesc()
1608 <<" UDP processing CONNCMD_DISCONNECT"<<std::endl);
1611 case CONNCMD_DISCONNECT_PEER:
1612 LOG(dout_con<<m_connection->getDesc()
1613 <<" UDP processing CONNCMD_DISCONNECT_PEER"<<std::endl);
1614 disconnect_peer(c.peer_id);
1617 LOG(dout_con<<m_connection->getDesc()
1618 <<" UDP processing CONNCMD_SEND"<<std::endl);
1619 send(c.peer_id, c.channelnum, c.data);
1621 case CONNCMD_SEND_TO_ALL:
1622 LOG(dout_con<<m_connection->getDesc()
1623 <<" UDP processing CONNCMD_SEND_TO_ALL"<<std::endl);
1624 sendToAll(c.channelnum, c.data);
1627 LOG(dout_con<<m_connection->getDesc()
1628 <<" UDP processing CONCMD_ACK"<<std::endl);
1629 sendAsPacket(c.peer_id,c.channelnum,c.data,true);
1631 case CONCMD_CREATE_PEER:
1632 FATAL_ERROR("Got command that should be reliable as unreliable command");
1634 LOG(dout_con<<m_connection->getDesc()
1635 <<" Invalid command type: " << c.type <<std::endl);
1639 void ConnectionSendThread::serve(Address bind_address)
1641 LOG(dout_con<<m_connection->getDesc()
1642 <<"UDP serving at port " << bind_address.serializeString() <<std::endl);
1644 m_connection->m_udpSocket.Bind(bind_address);
1645 m_connection->SetPeerID(PEER_ID_SERVER);
1647 catch(SocketException &e) {
1651 m_connection->putEvent(ce);
1655 void ConnectionSendThread::connect(Address address)
1657 LOG(dout_con<<m_connection->getDesc()<<" connecting to "<<address.serializeString()
1658 <<":"<<address.getPort()<<std::endl);
1660 UDPPeer *peer = m_connection->createServerPeer(address);
1664 e.peerAdded(peer->id, peer->address);
1665 m_connection->putEvent(e);
1669 if (address.isIPv6())
1670 bind_addr.setAddress((IPv6AddressBytes*) NULL);
1672 bind_addr.setAddress(0,0,0,0);
1674 m_connection->m_udpSocket.Bind(bind_addr);
1676 // Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT
1677 m_connection->SetPeerID(PEER_ID_INEXISTENT);
1678 NetworkPacket pkt(0,0);
1679 m_connection->Send(PEER_ID_SERVER, 0, &pkt, true);
1682 void ConnectionSendThread::disconnect()
1684 LOG(dout_con<<m_connection->getDesc()<<" disconnecting"<<std::endl);
1686 // Create and send DISCO packet
1687 SharedBuffer<u8> data(2);
1688 writeU8(&data[0], TYPE_CONTROL);
1689 writeU8(&data[1], CONTROLTYPE_DISCO);
1693 std::list<u16> peerids = m_connection->getPeerIDs();
1695 for (u16 peerid : peerids) {
1696 sendAsPacket(peerid, 0,data,false);
1700 void ConnectionSendThread::disconnect_peer(u16 peer_id)
1702 LOG(dout_con<<m_connection->getDesc()<<" disconnecting peer"<<std::endl);
1704 // Create and send DISCO packet
1705 SharedBuffer<u8> data(2);
1706 writeU8(&data[0], TYPE_CONTROL);
1707 writeU8(&data[1], CONTROLTYPE_DISCO);
1708 sendAsPacket(peer_id, 0,data,false);
1710 PeerHelper peer = m_connection->getPeerNoEx(peer_id);
1715 if (dynamic_cast<UDPPeer*>(&peer) == 0)
1720 dynamic_cast<UDPPeer*>(&peer)->m_pending_disconnect = true;
1723 void ConnectionSendThread::send(u16 peer_id, u8 channelnum,
1724 SharedBuffer<u8> data)
1726 assert(channelnum < CHANNEL_COUNT); // Pre-condition
1728 PeerHelper peer = m_connection->getPeerNoEx(peer_id);
1731 LOG(dout_con<<m_connection->getDesc()<<" peer: peer_id="<<peer_id
1732 << ">>>NOT<<< found on sending packet"
1733 << ", channel " << (channelnum % 0xFF)
1734 << ", size: " << data.getSize() <<std::endl);
1738 LOG(dout_con<<m_connection->getDesc()<<" sending to peer_id="<<peer_id
1739 << ", channel " << (channelnum % 0xFF)
1740 << ", size: " << data.getSize() <<std::endl);
1742 u16 split_sequence_number = peer->getNextSplitSequenceNumber(channelnum);
1744 u32 chunksize_max = m_max_packet_size - BASE_HEADER_SIZE;
1745 std::list<SharedBuffer<u8> > originals;
1747 originals = makeAutoSplitPacket(data, chunksize_max,split_sequence_number);
1749 peer->setNextSplitSequenceNumber(channelnum,split_sequence_number);
1751 for (const SharedBuffer<u8> &original : originals) {
1752 sendAsPacket(peer_id, channelnum, original);
1756 void ConnectionSendThread::sendReliable(ConnectionCommand &c)
1758 PeerHelper peer = m_connection->getPeerNoEx(c.peer_id);
1762 peer->PutReliableSendCommand(c,m_max_packet_size);
1765 void ConnectionSendThread::sendToAll(u8 channelnum, SharedBuffer<u8> data)
1767 std::list<u16> peerids = m_connection->getPeerIDs();
1769 for (u16 peerid : peerids) {
1770 send(peerid, channelnum, data);
1774 void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)
1776 std::list<u16> peerids = m_connection->getPeerIDs();
1778 for (u16 peerid : peerids) {
1779 PeerHelper peer = m_connection->getPeerNoEx(peerid);
1784 peer->PutReliableSendCommand(c,m_max_packet_size);
1788 void ConnectionSendThread::sendPackets(float dtime)
1790 std::list<u16> peerIds = m_connection->getPeerIDs();
1791 std::list<u16> pendingDisconnect;
1792 std::map<u16,bool> pending_unreliable;
1794 for (u16 peerId : peerIds) {
1795 PeerHelper peer = m_connection->getPeerNoEx(peerId);
1796 //peer may have been removed
1798 LOG(dout_con<<m_connection->getDesc()<< " Peer not found: peer_id=" << peerId
1802 peer->m_increment_packets_remaining = m_iteration_packets_avaialble/m_connection->m_peers.size();
1804 UDPPeer *udpPeer = dynamic_cast<UDPPeer*>(&peer);
1810 if (udpPeer->m_pending_disconnect) {
1811 pendingDisconnect.push_back(peerId);
1814 PROFILE(std::stringstream peerIdentifier);
1815 PROFILE(peerIdentifier << "sendPackets[" << m_connection->getDesc() << ";" << peerId
1817 PROFILE(ScopeProfiler peerprofiler(g_profiler, peerIdentifier.str(), SPT_AVG));
1819 LOG(dout_con<<m_connection->getDesc()
1820 << " Handle per peer queues: peer_id=" << peerId
1821 << " packet quota: " << peer->m_increment_packets_remaining << std::endl);
1823 // first send queued reliable packets for all peers (if possible)
1824 for (unsigned int i=0; i < CHANNEL_COUNT; i++) {
1825 Channel &channel = udpPeer->channels[i];
1826 u16 next_to_ack = 0;
1828 channel.outgoing_reliables_sent.getFirstSeqnum(next_to_ack);
1829 u16 next_to_receive = 0;
1830 channel.incoming_reliables.getFirstSeqnum(next_to_receive);
1832 LOG(dout_con<<m_connection->getDesc()<< "\t channel: "
1833 << i << ", peer quota:"
1834 << peer->m_increment_packets_remaining
1836 << "\t\t\treliables on wire: "
1837 << channel.outgoing_reliables_sent.size()
1838 << ", waiting for ack for " << next_to_ack
1840 << "\t\t\tincoming_reliables: "
1841 << channel.incoming_reliables.size()
1842 << ", next reliable packet: "
1843 << channel.readNextIncomingSeqNum()
1844 << ", next queued: " << next_to_receive
1846 << "\t\t\treliables queued : "
1847 << channel.queued_reliables.size()
1849 << "\t\t\tqueued commands : "
1850 << channel.queued_commands.size()
1853 while ((!channel.queued_reliables.empty()) &&
1854 (channel.outgoing_reliables_sent.size()
1855 < channel.getWindowSize())&&
1856 (peer->m_increment_packets_remaining > 0))
1858 BufferedPacket p = channel.queued_reliables.front();
1859 channel.queued_reliables.pop();
1860 LOG(dout_con<<m_connection->getDesc()
1861 <<" INFO: sending a queued reliable packet "
1863 <<", seqnum: " << readU16(&p.data[BASE_HEADER_SIZE+1])
1865 sendAsPacketReliable(p, &channel);
1866 peer->m_increment_packets_remaining--;
1871 if (!m_outgoing_queue.empty()) {
1872 LOG(dout_con<<m_connection->getDesc()
1873 << " Handle non reliable queue ("
1874 << m_outgoing_queue.size() << " pkts)" << std::endl);
1877 unsigned int initial_queuesize = m_outgoing_queue.size();
1878 /* send non reliable packets*/
1879 for(unsigned int i=0;i < initial_queuesize;i++) {
1880 OutgoingPacket packet = m_outgoing_queue.front();
1881 m_outgoing_queue.pop();
1883 if (packet.reliable)
1886 PeerHelper peer = m_connection->getPeerNoEx(packet.peer_id);
1888 LOG(dout_con<<m_connection->getDesc()
1889 <<" Outgoing queue: peer_id="<<packet.peer_id
1890 << ">>>NOT<<< found on sending packet"
1891 << ", channel " << (packet.channelnum % 0xFF)
1892 << ", size: " << packet.data.getSize() <<std::endl);
1896 /* send acks immediately */
1898 rawSendAsPacket(packet.peer_id, packet.channelnum,
1899 packet.data, packet.reliable);
1900 peer->m_increment_packets_remaining =
1901 MYMIN(0,peer->m_increment_packets_remaining--);
1904 ( peer->m_increment_packets_remaining > 0) ||
1905 (stopRequested())) {
1906 rawSendAsPacket(packet.peer_id, packet.channelnum,
1907 packet.data, packet.reliable);
1908 peer->m_increment_packets_remaining--;
1911 m_outgoing_queue.push(packet);
1912 pending_unreliable[packet.peer_id] = true;
1916 for (u16 peerId : pendingDisconnect) {
1917 if (!pending_unreliable[peerId])
1919 m_connection->deletePeer(peerId,false);
1924 void ConnectionSendThread::sendAsPacket(u16 peer_id, u8 channelnum,
1925 SharedBuffer<u8> data, bool ack)
1927 OutgoingPacket packet(peer_id, channelnum, data, false, ack);
1928 m_outgoing_queue.push(packet);
1931 ConnectionReceiveThread::ConnectionReceiveThread(unsigned int max_packet_size) :
1932 Thread("ConnectionReceive")
1936 void * ConnectionReceiveThread::run()
1938 assert(m_connection);
1940 LOG(dout_con<<m_connection->getDesc()
1941 <<"ConnectionReceive thread started"<<std::endl);
1943 PROFILE(std::stringstream ThreadIdentifier);
1944 PROFILE(ThreadIdentifier << "ConnectionReceive: [" << m_connection->getDesc() << "]");
1946 #ifdef DEBUG_CONNECTION_KBPS
1947 u64 curtime = porting::getTimeMs();
1948 u64 lasttime = curtime;
1949 float debug_print_timer = 0.0;
1952 while(!stopRequested()) {
1953 BEGIN_DEBUG_EXCEPTION_HANDLER
1954 PROFILE(ScopeProfiler sp(g_profiler, ThreadIdentifier.str(), SPT_AVG));
1956 #ifdef DEBUG_CONNECTION_KBPS
1958 curtime = porting::getTimeMs();
1959 float dtime = CALC_DTIME(lasttime,curtime);
1962 /* receive packets */
1965 #ifdef DEBUG_CONNECTION_KBPS
1966 debug_print_timer += dtime;
1967 if (debug_print_timer > 20.0) {
1968 debug_print_timer -= 20.0;
1970 std::list<u16> peerids = m_connection->getPeerIDs();
1972 for (std::list<u16>::iterator i = peerids.begin();
1976 PeerHelper peer = m_connection->getPeerNoEx(*i);
1980 float peer_current = 0.0;
1981 float peer_loss = 0.0;
1982 float avg_rate = 0.0;
1983 float avg_loss = 0.0;
1985 for(u16 j=0; j<CHANNEL_COUNT; j++)
1987 peer_current +=peer->channels[j].getCurrentDownloadRateKB();
1988 peer_loss += peer->channels[j].getCurrentLossRateKB();
1989 avg_rate += peer->channels[j].getAvgDownloadRateKB();
1990 avg_loss += peer->channels[j].getAvgLossRateKB();
1993 std::stringstream output;
1994 output << std::fixed << std::setprecision(1);
1995 output << "OUT to Peer " << *i << " RATES (good / loss) " << std::endl;
1996 output << "\tcurrent (sum): " << peer_current << "kb/s "<< peer_loss << "kb/s" << std::endl;
1997 output << "\taverage (sum): " << avg_rate << "kb/s "<< avg_loss << "kb/s" << std::endl;
1998 output << std::setfill(' ');
1999 for(u16 j=0; j<CHANNEL_COUNT; j++)
2001 output << "\tcha " << j << ":"
2002 << " CUR: " << std::setw(6) << peer->channels[j].getCurrentDownloadRateKB() <<"kb/s"
2003 << " AVG: " << std::setw(6) << peer->channels[j].getAvgDownloadRateKB() <<"kb/s"
2004 << " MAX: " << std::setw(6) << peer->channels[j].getMaxDownloadRateKB() <<"kb/s"
2006 << " CUR: " << std::setw(6) << peer->channels[j].getCurrentLossRateKB() <<"kb/s"
2007 << " AVG: " << std::setw(6) << peer->channels[j].getAvgLossRateKB() <<"kb/s"
2008 << " MAX: " << std::setw(6) << peer->channels[j].getMaxLossRateKB() <<"kb/s"
2009 << " / WS: " << peer->channels[j].getWindowSize()
2013 fprintf(stderr,"%s\n",output.str().c_str());
2017 END_DEBUG_EXCEPTION_HANDLER
2020 PROFILE(g_profiler->remove(ThreadIdentifier.str()));
2024 // Receive packets from the network and buffers and create ConnectionEvents
2025 void ConnectionReceiveThread::receive()
2027 // use IPv6 minimum allowed MTU as receive buffer size as this is
2028 // theoretical reliable upper boundary of a udp packet for all IPv6 enabled
2030 unsigned int packet_maxsize = 1500;
2031 SharedBuffer<u8> packetdata(packet_maxsize);
2033 bool packet_queued = true;
2035 unsigned int loop_count = 0;
2037 /* first of all read packets from socket */
2038 /* check for incoming data available */
2039 while( (loop_count < 10) &&
2040 (m_connection->m_udpSocket.WaitData(50))) {
2043 if (packet_queued) {
2044 bool data_left = true;
2046 SharedBuffer<u8> resultdata;
2049 data_left = getFromBuffers(peer_id, resultdata);
2052 e.dataReceived(peer_id, resultdata);
2053 m_connection->putEvent(e);
2056 catch(ProcessedSilentlyException &e) {
2057 /* try reading again */
2060 packet_queued = false;
2064 s32 received_size = m_connection->m_udpSocket.Receive(sender, *packetdata, packet_maxsize);
2066 if ((received_size < BASE_HEADER_SIZE) ||
2067 (readU32(&packetdata[0]) != m_connection->GetProtocolID()))
2069 LOG(derr_con<<m_connection->getDesc()
2070 <<"Receive(): Invalid incoming packet, "
2071 <<"size: " << received_size
2073 << ((received_size >= 4) ? readU32(&packetdata[0]) : -1)
2078 u16 peer_id = readPeerId(*packetdata);
2079 u8 channelnum = readChannel(*packetdata);
2081 if (channelnum > CHANNEL_COUNT-1) {
2082 LOG(derr_con<<m_connection->getDesc()
2083 <<"Receive(): Invalid channel "<<channelnum<<std::endl);
2084 throw InvalidIncomingDataException("Channel doesn't exist");
2087 /* Try to identify peer by sender address (may happen on join) */
2088 if (peer_id == PEER_ID_INEXISTENT) {
2089 peer_id = m_connection->lookupPeer(sender);
2090 // We do not have to remind the peer of its
2091 // peer id as the CONTROLTYPE_SET_PEER_ID
2092 // command was sent reliably.
2095 /* The peer was not found in our lists. Add it. */
2096 if (peer_id == PEER_ID_INEXISTENT) {
2097 peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
2100 PeerHelper peer = m_connection->getPeerNoEx(peer_id);
2103 LOG(dout_con<<m_connection->getDesc()
2104 <<" got packet from unknown peer_id: "
2105 <<peer_id<<" Ignoring."<<std::endl);
2109 // Validate peer address
2111 Address peer_address;
2113 if (peer->getAddress(MTP_UDP, peer_address)) {
2114 if (peer_address != sender) {
2115 LOG(derr_con<<m_connection->getDesc()
2116 <<m_connection->getDesc()
2117 <<" Peer "<<peer_id<<" sending from different address."
2118 " Ignoring."<<std::endl);
2124 bool invalid_address = true;
2125 if (invalid_address) {
2126 LOG(derr_con<<m_connection->getDesc()
2127 <<m_connection->getDesc()
2128 <<" Peer "<<peer_id<<" unknown."
2129 " Ignoring."<<std::endl);
2134 peer->ResetTimeout();
2136 Channel *channel = 0;
2138 if (dynamic_cast<UDPPeer*>(&peer) != 0)
2140 channel = &(dynamic_cast<UDPPeer*>(&peer)->channels[channelnum]);
2144 channel->UpdateBytesReceived(received_size);
2147 // Throw the received packet to channel->processPacket()
2149 // Make a new SharedBuffer from the data without the base headers
2150 SharedBuffer<u8> strippeddata(received_size - BASE_HEADER_SIZE);
2151 memcpy(*strippeddata, &packetdata[BASE_HEADER_SIZE],
2152 strippeddata.getSize());
2155 // Process it (the result is some data with no headers made by us)
2156 SharedBuffer<u8> resultdata = processPacket
2157 (channel, strippeddata, peer_id, channelnum, false);
2159 LOG(dout_con<<m_connection->getDesc()
2160 <<" ProcessPacket from peer_id: " << peer_id
2161 << ",channel: " << (channelnum & 0xFF) << ", returned "
2162 << resultdata.getSize() << " bytes" <<std::endl);
2165 e.dataReceived(peer_id, resultdata);
2166 m_connection->putEvent(e);
2168 catch(ProcessedSilentlyException &e) {
2170 catch(ProcessedQueued &e) {
2171 packet_queued = true;
2174 catch(InvalidIncomingDataException &e) {
2176 catch(ProcessedSilentlyException &e) {
2181 bool ConnectionReceiveThread::getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst)
2183 std::list<u16> peerids = m_connection->getPeerIDs();
2185 for (u16 peerid : peerids) {
2186 PeerHelper peer = m_connection->getPeerNoEx(peerid);
2190 if (dynamic_cast<UDPPeer*>(&peer) == 0)
2193 for (Channel &channel : (dynamic_cast<UDPPeer *>(&peer))->channels) {
2194 if (checkIncomingBuffers(&channel, peer_id, dst)) {
2202 bool ConnectionReceiveThread::checkIncomingBuffers(Channel *channel,
2203 u16 &peer_id, SharedBuffer<u8> &dst)
2205 u16 firstseqnum = 0;
2206 if (channel->incoming_reliables.getFirstSeqnum(firstseqnum))
2208 if (firstseqnum == channel->readNextIncomingSeqNum())
2210 BufferedPacket p = channel->incoming_reliables.popFirst();
2211 peer_id = readPeerId(*p.data);
2212 u8 channelnum = readChannel(*p.data);
2213 u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE+1]);
2215 LOG(dout_con<<m_connection->getDesc()
2216 <<"UNBUFFERING TYPE_RELIABLE"
2217 <<" seqnum="<<seqnum
2218 <<" peer_id="<<peer_id
2219 <<" channel="<<((int)channelnum&0xff)
2222 channel->incNextIncomingSeqNum();
2224 u32 headers_size = BASE_HEADER_SIZE + RELIABLE_HEADER_SIZE;
2225 // Get out the inside packet and re-process it
2226 SharedBuffer<u8> payload(p.data.getSize() - headers_size);
2227 memcpy(*payload, &p.data[headers_size], payload.getSize());
2229 dst = processPacket(channel, payload, peer_id, channelnum, true);
2236 SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
2237 SharedBuffer<u8> packetdata, u16 peer_id, u8 channelnum, bool reliable)
2239 PeerHelper peer = m_connection->getPeerNoEx(peer_id);
2242 errorstream << "Peer not found (possible timeout)" << std::endl;
2243 throw ProcessedSilentlyException("Peer not found (possible timeout)");
2246 if (packetdata.getSize() < 1)
2247 throw InvalidIncomingDataException("packetdata.getSize() < 1");
2249 u8 type = readU8(&(packetdata[0]));
2251 if (MAX_UDP_PEERS <= 65535 && peer_id >= MAX_UDP_PEERS) {
2252 std::string errmsg = "Invalid peer_id=" + itos(peer_id);
2253 errorstream << errmsg << std::endl;
2254 throw InvalidIncomingDataException(errmsg.c_str());
2257 if (type == TYPE_CONTROL)
2259 if (packetdata.getSize() < 2)
2260 throw InvalidIncomingDataException("packetdata.getSize() < 2");
2262 u8 controltype = readU8(&(packetdata[1]));
2264 if (controltype == CONTROLTYPE_ACK)
2266 assert(channel != NULL);
2268 if (packetdata.getSize() < 4) {
2269 throw InvalidIncomingDataException(
2270 "packetdata.getSize() < 4 (ACK header size)");
2273 u16 seqnum = readU16(&packetdata[2]);
2274 LOG(dout_con<<m_connection->getDesc()
2275 <<" [ CONTROLTYPE_ACK: channelnum="
2276 <<((int)channelnum&0xff)<<", peer_id="<<peer_id
2277 <<", seqnum="<<seqnum<< " ]"<<std::endl);
2281 channel->outgoing_reliables_sent.popSeqnum(seqnum);
2283 // only calculate rtt from straight sent packets
2284 if (p.resend_count == 0) {
2285 // Get round trip time
2286 u64 current_time = porting::getTimeMs();
2288 // a overflow is quite unlikely but as it'd result in major
2289 // rtt miscalculation we handle it here
2290 if (current_time > p.absolute_send_time)
2292 float rtt = (current_time - p.absolute_send_time) / 1000.0;
2294 // Let peer calculate stuff according to it
2295 // (avg_rtt and resend_timeout)
2296 dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2298 else if (p.totaltime > 0)
2300 float rtt = p.totaltime;
2302 // Let peer calculate stuff according to it
2303 // (avg_rtt and resend_timeout)
2304 dynamic_cast<UDPPeer*>(&peer)->reportRTT(rtt);
2307 //put bytes for max bandwidth calculation
2308 channel->UpdateBytesSent(p.data.getSize(),1);
2309 if (channel->outgoing_reliables_sent.size() == 0)
2311 m_connection->TriggerSend();
2314 catch(NotFoundException &e) {
2315 LOG(derr_con<<m_connection->getDesc()
2316 <<"WARNING: ACKed packet not "
2319 channel->UpdatePacketTooLateCounter();
2321 throw ProcessedSilentlyException("Got an ACK");
2323 else if (controltype == CONTROLTYPE_SET_PEER_ID) {
2324 // Got a packet to set our peer id
2325 if (packetdata.getSize() < 4)
2326 throw InvalidIncomingDataException
2327 ("packetdata.getSize() < 4 (SET_PEER_ID header size)");
2328 u16 peer_id_new = readU16(&packetdata[2]);
2329 LOG(dout_con<<m_connection->getDesc()
2330 <<"Got new peer id: "<<peer_id_new<<"... "<<std::endl);
2332 if (m_connection->GetPeerID() != PEER_ID_INEXISTENT)
2334 LOG(derr_con<<m_connection->getDesc()
2335 <<"WARNING: Not changing"
2336 " existing peer id."<<std::endl);
2340 LOG(dout_con<<m_connection->getDesc()<<"changing own peer id"<<std::endl);
2341 m_connection->SetPeerID(peer_id_new);
2344 ConnectionCommand cmd;
2346 SharedBuffer<u8> reply(2);
2347 writeU8(&reply[0], TYPE_CONTROL);
2348 writeU8(&reply[1], CONTROLTYPE_ENABLE_BIG_SEND_WINDOW);
2349 cmd.disableLegacy(PEER_ID_SERVER,reply);
2350 m_connection->putCommand(cmd);
2352 throw ProcessedSilentlyException("Got a SET_PEER_ID");
2354 else if (controltype == CONTROLTYPE_PING)
2356 // Just ignore it, the incoming data already reset
2357 // the timeout counter
2358 LOG(dout_con<<m_connection->getDesc()<<"PING"<<std::endl);
2359 throw ProcessedSilentlyException("Got a PING");
2361 else if (controltype == CONTROLTYPE_DISCO)
2363 // Just ignore it, the incoming data already reset
2364 // the timeout counter
2365 LOG(dout_con<<m_connection->getDesc()
2366 <<"DISCO: Removing peer "<<(peer_id)<<std::endl);
2368 if (!m_connection->deletePeer(peer_id, false)) {
2369 derr_con<<m_connection->getDesc()
2370 <<"DISCO: Peer not found"<<std::endl;
2373 throw ProcessedSilentlyException("Got a DISCO");
2375 else if (controltype == CONTROLTYPE_ENABLE_BIG_SEND_WINDOW)
2377 dynamic_cast<UDPPeer*>(&peer)->setNonLegacyPeer();
2378 throw ProcessedSilentlyException("Got non legacy control");
2381 LOG(derr_con<<m_connection->getDesc()
2382 <<"INVALID TYPE_CONTROL: invalid controltype="
2383 <<((int)controltype&0xff)<<std::endl);
2384 throw InvalidIncomingDataException("Invalid control type");
2387 else if (type == TYPE_ORIGINAL)
2389 if (packetdata.getSize() <= ORIGINAL_HEADER_SIZE)
2390 throw InvalidIncomingDataException
2391 ("packetdata.getSize() <= ORIGINAL_HEADER_SIZE");
2392 LOG(dout_con<<m_connection->getDesc()
2393 <<"RETURNING TYPE_ORIGINAL to user"
2395 // Get the inside packet out and return it
2396 SharedBuffer<u8> payload(packetdata.getSize() - ORIGINAL_HEADER_SIZE);
2397 memcpy(*payload, &(packetdata[ORIGINAL_HEADER_SIZE]), payload.getSize());
2400 else if (type == TYPE_SPLIT)
2402 Address peer_address;
2404 if (peer->getAddress(MTP_UDP, peer_address)) {
2406 // We have to create a packet again for buffering
2407 // This isn't actually too bad an idea.
2408 BufferedPacket packet = makePacket(
2411 m_connection->GetProtocolID(),
2415 // Buffer the packet
2416 SharedBuffer<u8> data =
2417 peer->addSpiltPacket(channelnum,packet,reliable);
2419 if (data.getSize() != 0)
2421 LOG(dout_con<<m_connection->getDesc()
2422 <<"RETURNING TYPE_SPLIT: Constructed full data, "
2423 <<"size="<<data.getSize()<<std::endl);
2426 LOG(dout_con<<m_connection->getDesc()<<"BUFFERED TYPE_SPLIT"<<std::endl);
2427 throw ProcessedSilentlyException("Buffered a split packet chunk");
2430 //TODO throw some error
2433 else if (type == TYPE_RELIABLE)
2435 assert(channel != NULL);
2437 // Recursive reliable packets not allowed
2439 throw InvalidIncomingDataException("Found nested reliable packets");
2441 if (packetdata.getSize() < RELIABLE_HEADER_SIZE)
2442 throw InvalidIncomingDataException
2443 ("packetdata.getSize() < RELIABLE_HEADER_SIZE");
2445 u16 seqnum = readU16(&packetdata[1]);
2446 bool is_future_packet = false;
2447 bool is_old_packet = false;
2449 /* packet is within our receive window send ack */
2450 if (seqnum_in_window(seqnum, channel->readNextIncomingSeqNum(),MAX_RELIABLE_WINDOW_SIZE))
2452 m_connection->sendAck(peer_id,channelnum,seqnum);
2455 is_future_packet = seqnum_higher(seqnum, channel->readNextIncomingSeqNum());
2456 is_old_packet = seqnum_higher(channel->readNextIncomingSeqNum(), seqnum);
2459 /* packet is not within receive window, don't send ack. *
2460 * if this was a valid packet it's gonna be retransmitted */
2461 if (is_future_packet)
2463 throw ProcessedSilentlyException("Received packet newer then expected, not sending ack");
2466 /* seems like our ack was lost, send another one for a old packet */
2469 LOG(dout_con<<m_connection->getDesc()
2470 << "RE-SENDING ACK: peer_id: " << peer_id
2471 << ", channel: " << (channelnum&0xFF)
2472 << ", seqnum: " << seqnum << std::endl;)
2473 m_connection->sendAck(peer_id,channelnum,seqnum);
2475 // we already have this packet so this one was on wire at least
2476 // the current timeout
2477 // we don't know how long this packet was on wire don't do silly guessing
2478 // dynamic_cast<UDPPeer*>(&peer)->reportRTT(dynamic_cast<UDPPeer*>(&peer)->getResendTimeout());
2480 throw ProcessedSilentlyException("Retransmitting ack for old packet");
2484 if (seqnum != channel->readNextIncomingSeqNum())
2486 Address peer_address;
2488 // this is a reliable packet so we have a udp address for sure
2489 peer->getAddress(MTP_MINETEST_RELIABLE_UDP, peer_address);
2490 // This one comes later, buffer it.
2491 // Actually we have to make a packet to buffer one.
2492 // Well, we have all the ingredients, so just do it.
2493 BufferedPacket packet = con::makePacket(
2496 m_connection->GetProtocolID(),
2500 channel->incoming_reliables.insert(packet,channel->readNextIncomingSeqNum());
2502 LOG(dout_con<<m_connection->getDesc()
2503 << "BUFFERING, TYPE_RELIABLE peer_id: " << peer_id
2504 << ", channel: " << (channelnum&0xFF)
2505 << ", seqnum: " << seqnum << std::endl;)
2507 throw ProcessedQueued("Buffered future reliable packet");
2509 catch(AlreadyExistsException &e)
2512 catch(IncomingDataCorruption &e)
2514 ConnectionCommand discon;
2515 discon.disconnect_peer(peer_id);
2516 m_connection->putCommand(discon);
2518 LOG(derr_con<<m_connection->getDesc()
2519 << "INVALID, TYPE_RELIABLE peer_id: " << peer_id
2520 << ", channel: " << (channelnum&0xFF)
2521 << ", seqnum: " << seqnum
2522 << "DROPPING CLIENT!" << std::endl;)
2526 /* we got a packet to process right now */
2527 LOG(dout_con<<m_connection->getDesc()
2528 << "RECURSIVE, TYPE_RELIABLE peer_id: " << peer_id
2529 << ", channel: " << (channelnum&0xFF)
2530 << ", seqnum: " << seqnum << std::endl;)
2533 /* check for resend case */
2534 u16 queued_seqnum = 0;
2535 if (channel->incoming_reliables.getFirstSeqnum(queued_seqnum))
2537 if (queued_seqnum == seqnum)
2539 BufferedPacket queued_packet = channel->incoming_reliables.popFirst();
2540 /** TODO find a way to verify the new against the old packet */
2544 channel->incNextIncomingSeqNum();
2546 // Get out the inside packet and re-process it
2547 SharedBuffer<u8> payload(packetdata.getSize() - RELIABLE_HEADER_SIZE);
2548 memcpy(*payload, &packetdata[RELIABLE_HEADER_SIZE], payload.getSize());
2550 return processPacket(channel, payload, peer_id, channelnum, true);
2554 derr_con<<m_connection->getDesc()
2555 <<"Got invalid type="<<((int)type&0xff)<<std::endl;
2556 throw InvalidIncomingDataException("Invalid packet type");
2559 // We should never get here.
2560 FATAL_ERROR("Invalid execution point");
2567 Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
2568 bool ipv6, PeerHandler *peerhandler) :
2570 m_protocol_id(protocol_id),
2571 m_sendThread(max_packet_size, timeout),
2572 m_receiveThread(max_packet_size),
2573 m_bc_peerhandler(peerhandler)
2576 m_udpSocket.setTimeoutMs(5);
2578 m_sendThread.setParent(this);
2579 m_receiveThread.setParent(this);
2581 m_sendThread.start();
2582 m_receiveThread.start();
2587 Connection::~Connection()
2589 m_shutting_down = true;
2590 // request threads to stop
2591 m_sendThread.stop();
2592 m_receiveThread.stop();
2594 //TODO for some unkonwn reason send/receive threads do not exit as they're
2595 // supposed to be but wait on peer timeout. To speed up shutdown we reduce
2596 // timeout to half a second.
2597 m_sendThread.setPeerTimeout(0.5);
2599 // wait for threads to finish
2600 m_sendThread.wait();
2601 m_receiveThread.wait();
2604 for (auto &peer : m_peers) {
2609 /* Internal stuff */
2610 void Connection::putEvent(ConnectionEvent &e)
2612 assert(e.type != CONNEVENT_NONE); // Pre-condition
2613 m_event_queue.push_back(e);
2616 PeerHelper Connection::getPeer(u16 peer_id)
2618 MutexAutoLock peerlock(m_peers_mutex);
2619 std::map<u16, Peer*>::iterator node = m_peers.find(peer_id);
2621 if (node == m_peers.end()) {
2622 throw PeerNotFoundException("GetPeer: Peer not found (possible timeout)");
2626 FATAL_ERROR_IF(node->second->id != peer_id, "Invalid peer id");
2628 return PeerHelper(node->second);
2631 PeerHelper Connection::getPeerNoEx(u16 peer_id)
2633 MutexAutoLock peerlock(m_peers_mutex);
2634 std::map<u16, Peer*>::iterator node = m_peers.find(peer_id);
2636 if (node == m_peers.end()) {
2637 return PeerHelper(NULL);
2641 FATAL_ERROR_IF(node->second->id != peer_id, "Invalid peer id");
2643 return PeerHelper(node->second);
2646 /* find peer_id for address */
2647 u16 Connection::lookupPeer(Address& sender)
2649 MutexAutoLock peerlock(m_peers_mutex);
2650 std::map<u16, Peer*>::iterator j;
2651 j = m_peers.begin();
2652 for(; j != m_peers.end(); ++j)
2654 Peer *peer = j->second;
2655 if (peer->isPendingDeletion())
2660 if ((peer->getAddress(MTP_MINETEST_RELIABLE_UDP, tocheck)) && (tocheck == sender))
2663 if ((peer->getAddress(MTP_UDP, tocheck)) && (tocheck == sender))
2667 return PEER_ID_INEXISTENT;
2670 std::list<Peer*> Connection::getPeers()
2672 std::list<Peer*> list;
2673 for (auto &p : m_peers) {
2674 Peer *peer = p.second;
2675 list.push_back(peer);
2680 bool Connection::deletePeer(u16 peer_id, bool timeout)
2684 /* lock list as short as possible */
2686 MutexAutoLock peerlock(m_peers_mutex);
2687 if (m_peers.find(peer_id) == m_peers.end())
2689 peer = m_peers[peer_id];
2690 m_peers.erase(peer_id);
2691 m_peer_ids.remove(peer_id);
2694 Address peer_address;
2695 //any peer has a primary address this never fails!
2696 peer->getAddress(MTP_PRIMARY, peer_address);
2699 e.peerRemoved(peer_id, timeout, peer_address);
2709 ConnectionEvent Connection::waitEvent(u32 timeout_ms)
2712 return m_event_queue.pop_front(timeout_ms);
2713 } catch(ItemNotFoundException &ex) {
2715 e.type = CONNEVENT_NONE;
2720 void Connection::putCommand(ConnectionCommand &c)
2722 if (!m_shutting_down) {
2723 m_command_queue.push_back(c);
2724 m_sendThread.Trigger();
2728 void Connection::Serve(Address bind_addr)
2730 ConnectionCommand c;
2735 void Connection::Connect(Address address)
2737 ConnectionCommand c;
2742 bool Connection::Connected()
2744 MutexAutoLock peerlock(m_peers_mutex);
2746 if (m_peers.size() != 1)
2749 std::map<u16, Peer*>::iterator node = m_peers.find(PEER_ID_SERVER);
2750 if (node == m_peers.end())
2753 if (m_peer_id == PEER_ID_INEXISTENT)
2759 void Connection::Disconnect()
2761 ConnectionCommand c;
2766 void Connection::Receive(NetworkPacket* pkt)
2769 ConnectionEvent e = waitEvent(m_bc_receive_timeout);
2770 if (e.type != CONNEVENT_NONE)
2771 LOG(dout_con << getDesc() << ": Receive: got event: "
2772 << e.describe() << std::endl);
2774 case CONNEVENT_NONE:
2775 throw NoIncomingDataException("No incoming data");
2776 case CONNEVENT_DATA_RECEIVED:
2777 // Data size is lesser than command size, ignoring packet
2778 if (e.data.getSize() < 2) {
2782 pkt->putRawPacket(*e.data, e.data.getSize(), e.peer_id);
2784 case CONNEVENT_PEER_ADDED: {
2785 UDPPeer tmp(e.peer_id, e.address, this);
2786 if (m_bc_peerhandler)
2787 m_bc_peerhandler->peerAdded(&tmp);
2790 case CONNEVENT_PEER_REMOVED: {
2791 UDPPeer tmp(e.peer_id, e.address, this);
2792 if (m_bc_peerhandler)
2793 m_bc_peerhandler->deletingPeer(&tmp, e.timeout);
2796 case CONNEVENT_BIND_FAILED:
2797 throw ConnectionBindFailed("Failed to bind socket "
2798 "(port already in use?)");
2801 throw NoIncomingDataException("No incoming data");
2804 void Connection::Send(u16 peer_id, u8 channelnum,
2805 NetworkPacket* pkt, bool reliable)
2807 assert(channelnum < CHANNEL_COUNT); // Pre-condition
2809 ConnectionCommand c;
2811 c.send(peer_id, channelnum, pkt, reliable);
2815 Address Connection::GetPeerAddress(u16 peer_id)
2817 PeerHelper peer = getPeerNoEx(peer_id);
2820 throw PeerNotFoundException("No address for peer found!");
2821 Address peer_address;
2822 peer->getAddress(MTP_PRIMARY, peer_address);
2823 return peer_address;
2826 float Connection::getPeerStat(u16 peer_id, rtt_stat_type type)
2828 PeerHelper peer = getPeerNoEx(peer_id);
2829 if (!peer) return -1;
2830 return peer->getStat(type);
2833 float Connection::getLocalStat(rate_stat_type type)
2835 PeerHelper peer = getPeerNoEx(PEER_ID_SERVER);
2837 FATAL_ERROR_IF(!peer, "Connection::getLocalStat we couldn't get our own peer? are you serious???");
2841 for (Channel &channel : dynamic_cast<UDPPeer *>(&peer)->channels) {
2844 retval += channel.getCurrentDownloadRateKB();
2847 retval += channel.getAvgDownloadRateKB();
2850 retval += channel.getCurrentIncomingRateKB();
2853 retval += channel.getAvgIncomingRateKB();
2856 retval += channel.getAvgLossRateKB();
2859 retval += channel.getCurrentLossRateKB();
2862 FATAL_ERROR("Connection::getLocalStat Invalid stat type");
2868 u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
2870 // Somebody wants to make a new connection
2872 // Get a unique peer id (2 or higher)
2873 u16 peer_id_new = m_next_remote_peer_id;
2874 u16 overflow = MAX_UDP_PEERS;
2877 Find an unused peer id
2879 MutexAutoLock lock(m_peers_mutex);
2880 bool out_of_ids = false;
2883 if (m_peers.find(peer_id_new) == m_peers.end())
2886 // Check for overflow
2887 if (peer_id_new == overflow) {
2895 errorstream << getDesc() << " ran out of peer ids" << std::endl;
2896 return PEER_ID_INEXISTENT;
2901 peer = new UDPPeer(peer_id_new, sender, this);
2903 m_peers[peer->id] = peer;
2904 m_peer_ids.push_back(peer->id);
2906 m_next_remote_peer_id = (peer_id_new +1 ) % MAX_UDP_PEERS;
2908 LOG(dout_con << getDesc()
2909 << "createPeer(): giving peer_id=" << peer_id_new << std::endl);
2911 ConnectionCommand cmd;
2912 SharedBuffer<u8> reply(4);
2913 writeU8(&reply[0], TYPE_CONTROL);
2914 writeU8(&reply[1], CONTROLTYPE_SET_PEER_ID);
2915 writeU16(&reply[2], peer_id_new);
2916 cmd.createPeer(peer_id_new,reply);
2919 // Create peer addition event
2921 e.peerAdded(peer_id_new, sender);
2924 // We're now talking to a valid peer_id
2928 void Connection::PrintInfo(std::ostream &out)
2930 m_info_mutex.lock();
2931 out<<getDesc()<<": ";
2932 m_info_mutex.unlock();
2935 void Connection::PrintInfo()
2937 PrintInfo(dout_con);
2940 const std::string Connection::getDesc()
2942 return std::string("con(")+
2943 itos(m_udpSocket.GetHandle())+"/"+itos(m_peer_id)+")";
2946 void Connection::DisconnectPeer(u16 peer_id)
2948 ConnectionCommand discon;
2949 discon.disconnect_peer(peer_id);
2953 void Connection::sendAck(u16 peer_id, u8 channelnum, u16 seqnum)
2955 assert(channelnum < CHANNEL_COUNT); // Pre-condition
2957 LOG(dout_con<<getDesc()
2958 <<" Queuing ACK command to peer_id: " << peer_id <<
2959 " channel: " << (channelnum & 0xFF) <<
2960 " seqnum: " << seqnum << std::endl);
2962 ConnectionCommand c;
2963 SharedBuffer<u8> ack(4);
2964 writeU8(&ack[0], TYPE_CONTROL);
2965 writeU8(&ack[1], CONTROLTYPE_ACK);
2966 writeU16(&ack[2], seqnum);
2968 c.ack(peer_id, channelnum, ack);
2970 m_sendThread.Trigger();
2973 UDPPeer* Connection::createServerPeer(Address& address)
2975 if (getPeerNoEx(PEER_ID_SERVER) != 0)
2977 throw ConnectionException("Already connected to a server");
2980 UDPPeer *peer = new UDPPeer(PEER_ID_SERVER, address, this);
2983 MutexAutoLock lock(m_peers_mutex);
2984 m_peers[peer->id] = peer;
2985 m_peer_ids.push_back(peer->id);