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.
20 #ifndef CONNECTION_HEADER
21 #define CONNECTION_HEADER
23 #include "irrlichttypes_bloated.h"
25 #include "exceptions.h"
26 #include "constants.h"
27 #include "network/networkpacket.h"
28 #include "util/pointer.h"
29 #include "util/container.h"
30 #include "util/thread.h"
31 #include "util/numeric.h"
43 class NotFoundException : public BaseException
46 NotFoundException(const char *s):
51 class PeerNotFoundException : public BaseException
54 PeerNotFoundException(const char *s):
59 class ConnectionException : public BaseException
62 ConnectionException(const char *s):
67 class ConnectionBindFailed : public BaseException
70 ConnectionBindFailed(const char *s):
75 class InvalidIncomingDataException : public BaseException
78 InvalidIncomingDataException(const char *s):
83 class InvalidOutgoingDataException : public BaseException
86 InvalidOutgoingDataException(const char *s):
91 class NoIncomingDataException : public BaseException
94 NoIncomingDataException(const char *s):
99 class ProcessedSilentlyException : public BaseException
102 ProcessedSilentlyException(const char *s):
107 class ProcessedQueued : public BaseException
110 ProcessedQueued(const char *s):
115 class IncomingDataCorruption : public BaseException
118 IncomingDataCorruption(const char *s):
123 typedef enum MTProtocols {
126 MTP_MINETEST_RELIABLE_UDP
129 #define SEQNUM_MAX 65535
130 inline bool seqnum_higher(u16 totest, u16 base)
134 if ((totest - base) > (SEQNUM_MAX/2))
141 if ((base - totest) > (SEQNUM_MAX/2))
148 inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
150 u16 window_start = next;
151 u16 window_end = ( next + window_size ) % (SEQNUM_MAX+1);
153 if (window_start < window_end)
155 return ((seqnum >= window_start) && (seqnum < window_end));
159 return ((seqnum < window_end) || (seqnum >= window_start));
163 struct BufferedPacket
165 BufferedPacket(u8 *a_data, u32 a_size):
166 data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
169 BufferedPacket(u32 a_size):
170 data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
173 SharedBuffer<u8> data; // Data of the packet, including headers
174 float time; // Seconds from buffering the packet or re-sending
175 float totaltime; // Seconds from buffering the packet
176 unsigned int absolute_send_time;
177 Address address; // Sender or destination
178 unsigned int resend_count;
181 // This adds the base headers to the data and makes a packet out of it
182 BufferedPacket makePacket(Address &address, u8 *data, u32 datasize,
183 u32 protocol_id, u16 sender_peer_id, u8 channel);
184 BufferedPacket makePacket(Address &address, SharedBuffer<u8> &data,
185 u32 protocol_id, u16 sender_peer_id, u8 channel);
187 // Add the TYPE_ORIGINAL header to the data
188 SharedBuffer<u8> makeOriginalPacket(
189 SharedBuffer<u8> data);
191 // Split data in chunks and add TYPE_SPLIT headers to them
192 std::list<SharedBuffer<u8> > makeSplitPacket(
193 SharedBuffer<u8> data,
197 // Depending on size, make a TYPE_ORIGINAL or TYPE_SPLIT packet
198 // Increments split_seqnum if a split packet is made
199 std::list<SharedBuffer<u8> > makeAutoSplitPacket(
200 SharedBuffer<u8> data,
204 // Add the TYPE_RELIABLE header to the data
205 SharedBuffer<u8> makeReliablePacket(
206 SharedBuffer<u8> data,
209 struct IncomingSplitPacket
211 IncomingSplitPacket()
216 // Key is chunk number, value is data without headers
217 std::map<u16, SharedBuffer<u8> > chunks;
219 float time; // Seconds from adding
220 bool reliable; // If true, isn't deleted on timeout
224 return (chunks.size() == chunk_count);
231 A packet is sent through a channel to a peer with a basic header:
232 TODO: Should we have a receiver_peer_id also?
235 [4] u16 sender_peer_id
239 value 0 (PEER_ID_INEXISTENT) is reserved for making new connections
240 value 1 (PEER_ID_SERVER) is reserved for server
241 these constants are defined in constants.h
243 The lower the number, the higher the priority is.
244 Only channels 0, 1 and 2 exist.
246 #define BASE_HEADER_SIZE 7
247 #define CHANNEL_COUNT 3
251 CONTROL: This is a packet used by the protocol.
252 - When this is processed, nothing is handed to the user.
256 controltype and data description:
259 CONTROLTYPE_SET_PEER_ID
262 - There is no actual reply, but this can be sent in a reliable
263 packet to get a reply
266 #define TYPE_CONTROL 0
267 #define CONTROLTYPE_ACK 0
268 #define CONTROLTYPE_SET_PEER_ID 1
269 #define CONTROLTYPE_PING 2
270 #define CONTROLTYPE_DISCO 3
271 #define CONTROLTYPE_ENABLE_BIG_SEND_WINDOW 4
274 ORIGINAL: This is a plain packet with no control and no error
276 - When this is processed, it is directly handed to the user.
280 #define TYPE_ORIGINAL 1
281 #define ORIGINAL_HEADER_SIZE 1
283 SPLIT: These are sequences of packets forming one bigger piece of
285 - When processed and all the packet_nums 0...packet_count-1 are
286 present (this should be buffered), the resulting data shall be
287 directly handed to the user.
288 - If the data fails to come up in a reasonable time, the buffer shall
289 be silently discarded.
290 - These can be sent as-is or atop of a RELIABLE packet stream.
299 RELIABLE: Delivery of all RELIABLE packets shall be forced by ACKs,
300 and they shall be delivered in the same order as sent. This is done
301 with a buffer in the receiving and transmitting end.
302 - When this is processed, the contents of each packet is recursively
303 processed as packets.
309 #define TYPE_RELIABLE 3
310 #define RELIABLE_HEADER_SIZE 3
311 #define SEQNUM_INITIAL 65500
314 A buffer which stores reliable packets and sorts them internally
315 for fast access to the smallest one.
318 typedef std::list<BufferedPacket>::iterator RPBSearchResult;
320 class ReliablePacketBuffer
323 ReliablePacketBuffer();
325 bool getFirstSeqnum(u16& result);
327 BufferedPacket popFirst();
328 BufferedPacket popSeqnum(u16 seqnum);
329 void insert(BufferedPacket &p,u16 next_expected);
331 void incrementTimeouts(float dtime);
332 std::list<BufferedPacket> getTimedOuts(float timeout,
333 unsigned int max_packets);
337 bool containsPacket(u16 seqnum);
338 RPBSearchResult notFound();
343 RPBSearchResult findPacket(u16 seqnum);
345 std::list<BufferedPacket> m_list;
348 u16 m_oldest_non_answered_ack;
354 A buffer for reconstructing split packets
357 class IncomingSplitBuffer
360 ~IncomingSplitBuffer();
362 Returns a reference counted buffer of length != 0 when a full split
363 packet is constructed. If not, returns one of length 0.
365 SharedBuffer<u8> insert(BufferedPacket &p, bool reliable);
367 void removeUnreliableTimedOuts(float dtime, float timeout);
371 std::map<u16, IncomingSplitPacket*> m_buf;
376 struct OutgoingPacket
380 SharedBuffer<u8> data;
384 OutgoingPacket(u16 peer_id_, u8 channelnum_, SharedBuffer<u8> data_,
385 bool reliable_,bool ack_=false):
387 channelnum(channelnum_),
395 enum ConnectionCommandType{
400 CONNCMD_DISCONNECT_PEER,
405 CONCMD_DISABLE_LEGACY
408 struct ConnectionCommand
410 enum ConnectionCommandType type;
418 ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {}
420 void serve(Address address_)
422 type = CONNCMD_SERVE;
425 void connect(Address address_)
427 type = CONNCMD_CONNECT;
432 type = CONNCMD_DISCONNECT;
434 void disconnect_peer(u16 peer_id_)
436 type = CONNCMD_DISCONNECT_PEER;
439 void send(u16 peer_id_, u8 channelnum_,
440 NetworkPacket* pkt, bool reliable_)
444 channelnum = channelnum_;
445 data = pkt->oldForgePacket();
446 reliable = reliable_;
449 void ack(u16 peer_id_, u8 channelnum_, SharedBuffer<u8> data_)
453 channelnum = channelnum_;
458 void createPeer(u16 peer_id_, SharedBuffer<u8> data_)
460 type = CONCMD_CREATE_PEER;
468 void disableLegacy(u16 peer_id_, SharedBuffer<u8> data_)
470 type = CONCMD_DISABLE_LEGACY;
483 u16 readNextIncomingSeqNum();
484 u16 incNextIncomingSeqNum();
486 u16 getOutgoingSequenceNumber(bool& successfull);
487 u16 readOutgoingSequenceNumber();
488 bool putBackSequenceNumber(u16);
490 u16 readNextSplitSeqNum();
491 void setNextSplitSeqNum(u16 seqnum);
493 // This is for buffering the incoming packets that are coming in
495 ReliablePacketBuffer incoming_reliables;
496 // This is for buffering the sent packets so that the sender can
497 // re-send them if no ACK is received
498 ReliablePacketBuffer outgoing_reliables_sent;
500 //queued reliable packets
501 std::queue<BufferedPacket> queued_reliables;
503 //queue commands prior splitting to packets
504 std::deque<ConnectionCommand> queued_commands;
506 IncomingSplitBuffer incoming_splits;
511 void UpdatePacketLossCounter(unsigned int count);
512 void UpdatePacketTooLateCounter();
513 void UpdateBytesSent(unsigned int bytes,unsigned int packages=1);
514 void UpdateBytesLost(unsigned int bytes);
515 void UpdateBytesReceived(unsigned int bytes);
517 void UpdateTimers(float dtime, bool legacy_peer);
519 const float getCurrentDownloadRateKB()
520 { JMutexAutoLock lock(m_internal_mutex); return cur_kbps; };
521 const float getMaxDownloadRateKB()
522 { JMutexAutoLock lock(m_internal_mutex); return max_kbps; };
524 const float getCurrentLossRateKB()
525 { JMutexAutoLock lock(m_internal_mutex); return cur_kbps_lost; };
526 const float getMaxLossRateKB()
527 { JMutexAutoLock lock(m_internal_mutex); return max_kbps_lost; };
529 const float getCurrentIncomingRateKB()
530 { JMutexAutoLock lock(m_internal_mutex); return cur_incoming_kbps; };
531 const float getMaxIncomingRateKB()
532 { JMutexAutoLock lock(m_internal_mutex); return max_incoming_kbps; };
534 const float getAvgDownloadRateKB()
535 { JMutexAutoLock lock(m_internal_mutex); return avg_kbps; };
536 const float getAvgLossRateKB()
537 { JMutexAutoLock lock(m_internal_mutex); return avg_kbps_lost; };
538 const float getAvgIncomingRateKB()
539 { JMutexAutoLock lock(m_internal_mutex); return avg_incoming_kbps; };
541 const unsigned int getWindowSize() const { return window_size; };
543 void setWindowSize(unsigned int size) { window_size = size; };
545 JMutex m_internal_mutex;
548 u16 next_incoming_seqnum;
550 u16 next_outgoing_seqnum;
551 u16 next_outgoing_split_seqnum;
553 unsigned int current_packet_loss;
554 unsigned int current_packet_too_late;
555 unsigned int current_packet_successfull;
556 float packet_loss_counter;
558 unsigned int current_bytes_transfered;
559 unsigned int current_bytes_received;
560 unsigned int current_bytes_lost;
564 float max_incoming_kbps;
565 float cur_incoming_kbps;
566 float avg_incoming_kbps;
572 unsigned int rate_samples;
596 virtual ~PeerHandler()
601 This is called after the Peer has been inserted into the
602 Connection's peer container.
604 virtual void peerAdded(Peer *peer) = 0;
606 This is called before the Peer has been removed from the
607 Connection's peer container.
609 virtual void deletingPeer(Peer *peer, bool timeout) = 0;
616 PeerHelper(Peer* peer);
619 PeerHelper& operator=(Peer* peer);
620 Peer* operator->() const;
622 Peer* operator&() const;
623 bool operator!=(void* ptr);
651 friend class PeerHelper;
653 Peer(Address address_,u16 id_,Connection* connection) :
655 m_increment_packets_remaining(9),
656 m_increment_bytes_remaining(0),
657 m_pending_deletion(false),
658 m_connection(connection),
663 m_timeout_counter(0.0),
664 m_last_timeout_check(porting::getTimeMs()),
665 m_has_sent_with_id(false)
667 m_rtt.avg_rtt = -1.0;
668 m_rtt.jitter_avg = -1.0;
669 m_rtt.jitter_max = 0.0;
671 m_rtt.jitter_min = FLT_MAX;
672 m_rtt.min_rtt = FLT_MAX;
676 JMutexAutoLock usage_lock(m_exclusive_access_mutex);
677 FATAL_ERROR_IF(m_usage != 0, "Reference counting failure");
680 // Unique id of the peer
685 virtual void PutReliableSendCommand(ConnectionCommand &c,
686 unsigned int max_packet_size) {};
688 virtual bool isActive() { return false; };
690 virtual bool getAddress(MTProtocols type, Address& toset) = 0;
693 {JMutexAutoLock lock(m_exclusive_access_mutex); m_timeout_counter=0.0; };
695 bool isTimedOut(float timeout);
698 { JMutexAutoLock lock(m_exclusive_access_mutex); m_has_sent_with_id = true; };
701 { JMutexAutoLock lock(m_exclusive_access_mutex); return m_has_sent_with_id; };
703 unsigned int m_increment_packets_remaining;
704 unsigned int m_increment_bytes_remaining;
706 virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; };
707 virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {};
708 virtual SharedBuffer<u8> addSpiltPacket(u8 channel,
709 BufferedPacket toadd,
712 fprintf(stderr,"Peer: addSplitPacket called, this is supposed to be never called!\n");
713 return SharedBuffer<u8>(0);
716 virtual bool Ping(float dtime, SharedBuffer<u8>& data) { return false; };
718 virtual float getStat(rtt_stat_type type) const {
721 return m_rtt.min_rtt;
723 return m_rtt.max_rtt;
725 return m_rtt.avg_rtt;
727 return m_rtt.jitter_min;
729 return m_rtt.jitter_max;
731 return m_rtt.jitter_avg;
736 virtual void reportRTT(float rtt) {};
738 void RTTStatistics(float rtt,
739 std::string profiler_id="",
740 unsigned int num_samples=1000);
745 JMutex m_exclusive_access_mutex;
747 bool m_pending_deletion;
749 Connection* m_connection;
751 // Address of the peer
770 // current usage count
771 unsigned int m_usage;
773 // Seconds from last receive
774 float m_timeout_counter;
776 u32 m_last_timeout_check;
778 bool m_has_sent_with_id;
781 class UDPPeer : public Peer
785 friend class PeerHelper;
786 friend class ConnectionReceiveThread;
787 friend class ConnectionSendThread;
788 friend class Connection;
790 UDPPeer(u16 a_id, Address a_address, Connection* connection);
791 virtual ~UDPPeer() {};
793 void PutReliableSendCommand(ConnectionCommand &c,
794 unsigned int max_packet_size);
797 { return ((hasSentWithID()) && (!m_pending_deletion)); };
799 bool getAddress(MTProtocols type, Address& toset);
801 void setNonLegacyPeer();
804 { return m_legacy_peer; }
806 u16 getNextSplitSequenceNumber(u8 channel);
807 void setNextSplitSequenceNumber(u8 channel, u16 seqnum);
809 SharedBuffer<u8> addSpiltPacket(u8 channel,
810 BufferedPacket toadd,
816 Calculates avg_rtt and resend_timeout.
817 rtt=-1 only recalculates resend_timeout
819 void reportRTT(float rtt);
821 void RunCommandQueues(
822 unsigned int max_packet_size,
823 unsigned int maxcommands,
824 unsigned int maxtransfer);
826 float getResendTimeout()
827 { JMutexAutoLock lock(m_exclusive_access_mutex); return resend_timeout; }
829 void setResendTimeout(float timeout)
830 { JMutexAutoLock lock(m_exclusive_access_mutex); resend_timeout = timeout; }
831 bool Ping(float dtime,SharedBuffer<u8>& data);
833 Channel channels[CHANNEL_COUNT];
834 bool m_pending_disconnect;
836 // This is changed dynamically
837 float resend_timeout;
839 bool processReliableSendCommand(
840 ConnectionCommand &c,
841 unsigned int max_packet_size);
850 enum ConnectionEventType{
852 CONNEVENT_DATA_RECEIVED,
853 CONNEVENT_PEER_ADDED,
854 CONNEVENT_PEER_REMOVED,
855 CONNEVENT_BIND_FAILED,
858 struct ConnectionEvent
860 enum ConnectionEventType type;
866 ConnectionEvent(): type(CONNEVENT_NONE) {}
868 std::string describe()
872 return "CONNEVENT_NONE";
873 case CONNEVENT_DATA_RECEIVED:
874 return "CONNEVENT_DATA_RECEIVED";
875 case CONNEVENT_PEER_ADDED:
876 return "CONNEVENT_PEER_ADDED";
877 case CONNEVENT_PEER_REMOVED:
878 return "CONNEVENT_PEER_REMOVED";
879 case CONNEVENT_BIND_FAILED:
880 return "CONNEVENT_BIND_FAILED";
882 return "Invalid ConnectionEvent";
885 void dataReceived(u16 peer_id_, SharedBuffer<u8> data_)
887 type = CONNEVENT_DATA_RECEIVED;
891 void peerAdded(u16 peer_id_, Address address_)
893 type = CONNEVENT_PEER_ADDED;
897 void peerRemoved(u16 peer_id_, bool timeout_, Address address_)
899 type = CONNEVENT_PEER_REMOVED;
906 type = CONNEVENT_BIND_FAILED;
910 class ConnectionSendThread : public JThread {
913 friend class UDPPeer;
915 ConnectionSendThread(unsigned int max_packet_size, float timeout);
921 void setParent(Connection* parent) {
922 assert(parent != NULL); // Pre-condition
923 m_connection = parent;
926 void setPeerTimeout(float peer_timeout)
927 { m_timeout = peer_timeout; }
930 void runTimeouts (float dtime);
931 void rawSend (const BufferedPacket &packet);
932 bool rawSendAsPacket(u16 peer_id, u8 channelnum,
933 SharedBuffer<u8> data, bool reliable);
935 void processReliableCommand (ConnectionCommand &c);
936 void processNonReliableCommand (ConnectionCommand &c);
937 void serve (Address bind_address);
938 void connect (Address address);
940 void disconnect_peer(u16 peer_id);
941 void send (u16 peer_id, u8 channelnum,
942 SharedBuffer<u8> data);
943 void sendReliable (ConnectionCommand &c);
944 void sendToAll (u8 channelnum,
945 SharedBuffer<u8> data);
946 void sendToAllReliable(ConnectionCommand &c);
948 void sendPackets (float dtime);
950 void sendAsPacket (u16 peer_id, u8 channelnum,
951 SharedBuffer<u8> data,bool ack=false);
953 void sendAsPacketReliable(BufferedPacket& p, Channel* channel);
955 bool packetsQueued();
957 Connection* m_connection;
958 unsigned int m_max_packet_size;
960 std::queue<OutgoingPacket> m_outgoing_queue;
961 JSemaphore m_send_sleep_semaphore;
963 unsigned int m_iteration_packets_avaialble;
964 unsigned int m_max_commands_per_iteration;
965 unsigned int m_max_data_packets_per_iteration;
966 unsigned int m_max_packets_requeued;
969 class ConnectionReceiveThread : public JThread {
971 ConnectionReceiveThread(unsigned int max_packet_size);
975 void setParent(Connection* parent) {
976 assert(parent != NULL); // Pre-condition
977 m_connection = parent;
983 // Returns next data from a buffer if possible
984 // If found, returns true; if not, false.
985 // If found, sets peer_id and dst
986 bool getFromBuffers (u16 &peer_id, SharedBuffer<u8> &dst);
988 bool checkIncomingBuffers(Channel *channel, u16 &peer_id,
989 SharedBuffer<u8> &dst);
992 Processes a packet with the basic header stripped out.
994 packetdata: Data in packet (with no base headers)
995 peer_id: peer id of the sender of the packet in question
996 channelnum: channel on which the packet was sent
997 reliable: true if recursing into a reliable packet
999 SharedBuffer<u8> processPacket(Channel *channel,
1000 SharedBuffer<u8> packetdata, u16 peer_id,
1001 u8 channelnum, bool reliable);
1004 Connection* m_connection;
1010 friend class ConnectionSendThread;
1011 friend class ConnectionReceiveThread;
1013 Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6);
1014 Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6,
1015 PeerHandler *peerhandler);
1019 ConnectionEvent getEvent();
1020 ConnectionEvent waitEvent(u32 timeout_ms);
1021 void putCommand(ConnectionCommand &c);
1023 void SetTimeoutMs(int timeout) { m_bc_receive_timeout = timeout; }
1024 void Serve(Address bind_addr);
1025 void Connect(Address address);
1028 u32 Receive(u16 &peer_id, SharedBuffer<u8> &data);
1029 void Send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
1030 u16 GetPeerID() { return m_peer_id; }
1031 Address GetPeerAddress(u16 peer_id);
1032 float getPeerStat(u16 peer_id, rtt_stat_type type);
1033 float getLocalStat(rate_stat_type type);
1034 const u32 GetProtocolID() const { return m_protocol_id; };
1035 const std::string getDesc();
1036 void DisconnectPeer(u16 peer_id);
1039 PeerHelper getPeer(u16 peer_id);
1040 PeerHelper getPeerNoEx(u16 peer_id);
1041 u16 lookupPeer(Address& sender);
1043 u16 createPeer(Address& sender, MTProtocols protocol, int fd);
1044 UDPPeer* createServerPeer(Address& sender);
1045 bool deletePeer(u16 peer_id, bool timeout);
1047 void SetPeerID(u16 id) { m_peer_id = id; }
1049 void sendAck(u16 peer_id, u8 channelnum, u16 seqnum);
1051 void PrintInfo(std::ostream &out);
1054 std::list<u16> getPeerIDs() { return m_peer_ids; }
1056 UDPSocket m_udpSocket;
1057 MutexedQueue<ConnectionCommand> m_command_queue;
1059 void putEvent(ConnectionEvent &e);
1062 { m_sendThread.Trigger(); }
1064 std::list<Peer*> getPeers();
1066 MutexedQueue<ConnectionEvent> m_event_queue;
1071 std::map<u16, Peer*> m_peers;
1072 std::list<u16> m_peer_ids;
1073 JMutex m_peers_mutex;
1075 ConnectionSendThread m_sendThread;
1076 ConnectionReceiveThread m_receiveThread;
1078 JMutex m_info_mutex;
1080 // Backwards compatibility
1081 PeerHandler *m_bc_peerhandler;
1082 int m_bc_receive_timeout;
1084 bool m_shutting_down;
1086 u16 m_next_remote_peer_id;