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"
45 class NotFoundException : public BaseException
48 NotFoundException(const char *s):
53 class PeerNotFoundException : public BaseException
56 PeerNotFoundException(const char *s):
61 class ConnectionException : public BaseException
64 ConnectionException(const char *s):
69 class ConnectionBindFailed : public BaseException
72 ConnectionBindFailed(const char *s):
77 class InvalidIncomingDataException : public BaseException
80 InvalidIncomingDataException(const char *s):
85 class InvalidOutgoingDataException : public BaseException
88 InvalidOutgoingDataException(const char *s):
93 class NoIncomingDataException : public BaseException
96 NoIncomingDataException(const char *s):
101 class ProcessedSilentlyException : public BaseException
104 ProcessedSilentlyException(const char *s):
109 class ProcessedQueued : public BaseException
112 ProcessedQueued(const char *s):
117 class IncomingDataCorruption : public BaseException
120 IncomingDataCorruption(const char *s):
125 typedef enum MTProtocols {
128 MTP_MINETEST_RELIABLE_UDP
131 #define SEQNUM_MAX 65535
132 inline bool seqnum_higher(u16 totest, u16 base)
136 if ((totest - base) > (SEQNUM_MAX/2))
143 if ((base - totest) > (SEQNUM_MAX/2))
150 inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
152 u16 window_start = next;
153 u16 window_end = ( next + window_size ) % (SEQNUM_MAX+1);
155 if (window_start < window_end)
157 return ((seqnum >= window_start) && (seqnum < window_end));
161 return ((seqnum < window_end) || (seqnum >= window_start));
165 struct BufferedPacket
167 BufferedPacket(u8 *a_data, u32 a_size):
168 data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
171 BufferedPacket(u32 a_size):
172 data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
175 SharedBuffer<u8> data; // Data of the packet, including headers
176 float time; // Seconds from buffering the packet or re-sending
177 float totaltime; // Seconds from buffering the packet
178 unsigned int absolute_send_time;
179 Address address; // Sender or destination
180 unsigned int resend_count;
183 // This adds the base headers to the data and makes a packet out of it
184 BufferedPacket makePacket(Address &address, u8 *data, u32 datasize,
185 u32 protocol_id, u16 sender_peer_id, u8 channel);
186 BufferedPacket makePacket(Address &address, SharedBuffer<u8> &data,
187 u32 protocol_id, u16 sender_peer_id, u8 channel);
189 // Add the TYPE_ORIGINAL header to the data
190 SharedBuffer<u8> makeOriginalPacket(
191 SharedBuffer<u8> data);
193 // Split data in chunks and add TYPE_SPLIT headers to them
194 std::list<SharedBuffer<u8> > makeSplitPacket(
195 SharedBuffer<u8> data,
199 // Depending on size, make a TYPE_ORIGINAL or TYPE_SPLIT packet
200 // Increments split_seqnum if a split packet is made
201 std::list<SharedBuffer<u8> > makeAutoSplitPacket(
202 SharedBuffer<u8> data,
206 // Add the TYPE_RELIABLE header to the data
207 SharedBuffer<u8> makeReliablePacket(
208 SharedBuffer<u8> data,
211 struct IncomingSplitPacket
213 IncomingSplitPacket()
218 // Key is chunk number, value is data without headers
219 std::map<u16, SharedBuffer<u8> > chunks;
221 float time; // Seconds from adding
222 bool reliable; // If true, isn't deleted on timeout
226 return (chunks.size() == chunk_count);
233 A packet is sent through a channel to a peer with a basic header:
234 TODO: Should we have a receiver_peer_id also?
237 [4] u16 sender_peer_id
241 value 0 (PEER_ID_INEXISTENT) is reserved for making new connections
242 value 1 (PEER_ID_SERVER) is reserved for server
243 these constants are defined in constants.h
245 The lower the number, the higher the priority is.
246 Only channels 0, 1 and 2 exist.
248 #define BASE_HEADER_SIZE 7
249 #define CHANNEL_COUNT 3
253 CONTROL: This is a packet used by the protocol.
254 - When this is processed, nothing is handed to the user.
258 controltype and data description:
261 CONTROLTYPE_SET_PEER_ID
264 - There is no actual reply, but this can be sent in a reliable
265 packet to get a reply
268 #define TYPE_CONTROL 0
269 #define CONTROLTYPE_ACK 0
270 #define CONTROLTYPE_SET_PEER_ID 1
271 #define CONTROLTYPE_PING 2
272 #define CONTROLTYPE_DISCO 3
273 #define CONTROLTYPE_ENABLE_BIG_SEND_WINDOW 4
276 ORIGINAL: This is a plain packet with no control and no error
278 - When this is processed, it is directly handed to the user.
282 #define TYPE_ORIGINAL 1
283 #define ORIGINAL_HEADER_SIZE 1
285 SPLIT: These are sequences of packets forming one bigger piece of
287 - When processed and all the packet_nums 0...packet_count-1 are
288 present (this should be buffered), the resulting data shall be
289 directly handed to the user.
290 - If the data fails to come up in a reasonable time, the buffer shall
291 be silently discarded.
292 - These can be sent as-is or atop of a RELIABLE packet stream.
301 RELIABLE: Delivery of all RELIABLE packets shall be forced by ACKs,
302 and they shall be delivered in the same order as sent. This is done
303 with a buffer in the receiving and transmitting end.
304 - When this is processed, the contents of each packet is recursively
305 processed as packets.
311 #define TYPE_RELIABLE 3
312 #define RELIABLE_HEADER_SIZE 3
313 #define SEQNUM_INITIAL 65500
316 A buffer which stores reliable packets and sorts them internally
317 for fast access to the smallest one.
320 typedef std::list<BufferedPacket>::iterator RPBSearchResult;
322 class ReliablePacketBuffer
325 ReliablePacketBuffer();
327 bool getFirstSeqnum(u16& result);
329 BufferedPacket popFirst();
330 BufferedPacket popSeqnum(u16 seqnum);
331 void insert(BufferedPacket &p,u16 next_expected);
333 void incrementTimeouts(float dtime);
334 std::list<BufferedPacket> getTimedOuts(float timeout,
335 unsigned int max_packets);
339 bool containsPacket(u16 seqnum);
340 RPBSearchResult notFound();
345 RPBSearchResult findPacket(u16 seqnum);
347 std::list<BufferedPacket> m_list;
350 u16 m_oldest_non_answered_ack;
356 A buffer for reconstructing split packets
359 class IncomingSplitBuffer
362 ~IncomingSplitBuffer();
364 Returns a reference counted buffer of length != 0 when a full split
365 packet is constructed. If not, returns one of length 0.
367 SharedBuffer<u8> insert(BufferedPacket &p, bool reliable);
369 void removeUnreliableTimedOuts(float dtime, float timeout);
373 std::map<u16, IncomingSplitPacket*> m_buf;
378 struct OutgoingPacket
382 SharedBuffer<u8> data;
386 OutgoingPacket(u16 peer_id_, u8 channelnum_, SharedBuffer<u8> data_,
387 bool reliable_,bool ack_=false):
389 channelnum(channelnum_),
397 enum ConnectionCommandType{
402 CONNCMD_DISCONNECT_PEER,
407 CONCMD_DISABLE_LEGACY
410 struct ConnectionCommand
412 enum ConnectionCommandType type;
420 ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {}
422 void serve(Address address_)
424 type = CONNCMD_SERVE;
427 void connect(Address address_)
429 type = CONNCMD_CONNECT;
434 type = CONNCMD_DISCONNECT;
436 void disconnect_peer(u16 peer_id_)
438 type = CONNCMD_DISCONNECT_PEER;
441 void send(u16 peer_id_, u8 channelnum_,
442 NetworkPacket* pkt, bool reliable_)
446 channelnum = channelnum_;
447 data = pkt->oldForgePacket();
448 reliable = reliable_;
451 void ack(u16 peer_id_, u8 channelnum_, SharedBuffer<u8> data_)
455 channelnum = channelnum_;
460 void createPeer(u16 peer_id_, SharedBuffer<u8> data_)
462 type = CONCMD_CREATE_PEER;
470 void disableLegacy(u16 peer_id_, SharedBuffer<u8> data_)
472 type = CONCMD_DISABLE_LEGACY;
485 u16 readNextIncomingSeqNum();
486 u16 incNextIncomingSeqNum();
488 u16 getOutgoingSequenceNumber(bool& successfull);
489 u16 readOutgoingSequenceNumber();
490 bool putBackSequenceNumber(u16);
492 u16 readNextSplitSeqNum();
493 void setNextSplitSeqNum(u16 seqnum);
495 // This is for buffering the incoming packets that are coming in
497 ReliablePacketBuffer incoming_reliables;
498 // This is for buffering the sent packets so that the sender can
499 // re-send them if no ACK is received
500 ReliablePacketBuffer outgoing_reliables_sent;
502 //queued reliable packets
503 std::queue<BufferedPacket> queued_reliables;
505 //queue commands prior splitting to packets
506 std::deque<ConnectionCommand> queued_commands;
508 IncomingSplitBuffer incoming_splits;
513 void UpdatePacketLossCounter(unsigned int count);
514 void UpdatePacketTooLateCounter();
515 void UpdateBytesSent(unsigned int bytes,unsigned int packages=1);
516 void UpdateBytesLost(unsigned int bytes);
517 void UpdateBytesReceived(unsigned int bytes);
519 void UpdateTimers(float dtime, bool legacy_peer);
521 const float getCurrentDownloadRateKB()
522 { JMutexAutoLock lock(m_internal_mutex); return cur_kbps; };
523 const float getMaxDownloadRateKB()
524 { JMutexAutoLock lock(m_internal_mutex); return max_kbps; };
526 const float getCurrentLossRateKB()
527 { JMutexAutoLock lock(m_internal_mutex); return cur_kbps_lost; };
528 const float getMaxLossRateKB()
529 { JMutexAutoLock lock(m_internal_mutex); return max_kbps_lost; };
531 const float getCurrentIncomingRateKB()
532 { JMutexAutoLock lock(m_internal_mutex); return cur_incoming_kbps; };
533 const float getMaxIncomingRateKB()
534 { JMutexAutoLock lock(m_internal_mutex); return max_incoming_kbps; };
536 const float getAvgDownloadRateKB()
537 { JMutexAutoLock lock(m_internal_mutex); return avg_kbps; };
538 const float getAvgLossRateKB()
539 { JMutexAutoLock lock(m_internal_mutex); return avg_kbps_lost; };
540 const float getAvgIncomingRateKB()
541 { JMutexAutoLock lock(m_internal_mutex); return avg_incoming_kbps; };
543 const unsigned int getWindowSize() const { return window_size; };
545 void setWindowSize(unsigned int size) { window_size = size; };
547 JMutex m_internal_mutex;
550 u16 next_incoming_seqnum;
552 u16 next_outgoing_seqnum;
553 u16 next_outgoing_split_seqnum;
555 unsigned int current_packet_loss;
556 unsigned int current_packet_too_late;
557 unsigned int current_packet_successfull;
558 float packet_loss_counter;
560 unsigned int current_bytes_transfered;
561 unsigned int current_bytes_received;
562 unsigned int current_bytes_lost;
566 float max_incoming_kbps;
567 float cur_incoming_kbps;
568 float avg_incoming_kbps;
574 unsigned int rate_samples;
598 virtual ~PeerHandler()
603 This is called after the Peer has been inserted into the
604 Connection's peer container.
606 virtual void peerAdded(Peer *peer) = 0;
608 This is called before the Peer has been removed from the
609 Connection's peer container.
611 virtual void deletingPeer(Peer *peer, bool timeout) = 0;
618 PeerHelper(Peer* peer);
621 PeerHelper& operator=(Peer* peer);
622 Peer* operator->() const;
624 Peer* operator&() const;
625 bool operator!=(void* ptr);
653 friend class PeerHelper;
655 Peer(Address address_,u16 id_,Connection* connection) :
657 m_increment_packets_remaining(9),
658 m_increment_bytes_remaining(0),
659 m_pending_deletion(false),
660 m_connection(connection),
665 m_timeout_counter(0.0),
666 m_last_timeout_check(porting::getTimeMs()),
667 m_has_sent_with_id(false)
669 m_rtt.avg_rtt = -1.0;
670 m_rtt.jitter_avg = -1.0;
671 m_rtt.jitter_max = 0.0;
673 m_rtt.jitter_min = FLT_MAX;
674 m_rtt.min_rtt = FLT_MAX;
678 JMutexAutoLock usage_lock(m_exclusive_access_mutex);
679 FATAL_ERROR_IF(m_usage != 0, "Reference counting failure");
682 // Unique id of the peer
687 virtual void PutReliableSendCommand(ConnectionCommand &c,
688 unsigned int max_packet_size) {};
690 virtual bool isActive() { return false; };
692 virtual bool getAddress(MTProtocols type, Address& toset) = 0;
695 {JMutexAutoLock lock(m_exclusive_access_mutex); m_timeout_counter=0.0; };
697 bool isTimedOut(float timeout);
700 { JMutexAutoLock lock(m_exclusive_access_mutex); m_has_sent_with_id = true; };
703 { JMutexAutoLock lock(m_exclusive_access_mutex); return m_has_sent_with_id; };
705 unsigned int m_increment_packets_remaining;
706 unsigned int m_increment_bytes_remaining;
708 virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; };
709 virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {};
710 virtual SharedBuffer<u8> addSpiltPacket(u8 channel,
711 BufferedPacket toadd,
714 fprintf(stderr,"Peer: addSplitPacket called, this is supposed to be never called!\n");
715 return SharedBuffer<u8>(0);
718 virtual bool Ping(float dtime, SharedBuffer<u8>& data) { return false; };
720 virtual float getStat(rtt_stat_type type) const {
723 return m_rtt.min_rtt;
725 return m_rtt.max_rtt;
727 return m_rtt.avg_rtt;
729 return m_rtt.jitter_min;
731 return m_rtt.jitter_max;
733 return m_rtt.jitter_avg;
738 virtual void reportRTT(float rtt) {};
740 void RTTStatistics(float rtt,
741 std::string profiler_id="",
742 unsigned int num_samples=1000);
747 JMutex m_exclusive_access_mutex;
749 bool m_pending_deletion;
751 Connection* m_connection;
753 // Address of the peer
772 // current usage count
773 unsigned int m_usage;
775 // Seconds from last receive
776 float m_timeout_counter;
778 u32 m_last_timeout_check;
780 bool m_has_sent_with_id;
783 class UDPPeer : public Peer
787 friend class PeerHelper;
788 friend class ConnectionReceiveThread;
789 friend class ConnectionSendThread;
790 friend class Connection;
792 UDPPeer(u16 a_id, Address a_address, Connection* connection);
793 virtual ~UDPPeer() {};
795 void PutReliableSendCommand(ConnectionCommand &c,
796 unsigned int max_packet_size);
799 { return ((hasSentWithID()) && (!m_pending_deletion)); };
801 bool getAddress(MTProtocols type, Address& toset);
803 void setNonLegacyPeer();
806 { return m_legacy_peer; }
808 u16 getNextSplitSequenceNumber(u8 channel);
809 void setNextSplitSequenceNumber(u8 channel, u16 seqnum);
811 SharedBuffer<u8> addSpiltPacket(u8 channel,
812 BufferedPacket toadd,
818 Calculates avg_rtt and resend_timeout.
819 rtt=-1 only recalculates resend_timeout
821 void reportRTT(float rtt);
823 void RunCommandQueues(
824 unsigned int max_packet_size,
825 unsigned int maxcommands,
826 unsigned int maxtransfer);
828 float getResendTimeout()
829 { JMutexAutoLock lock(m_exclusive_access_mutex); return resend_timeout; }
831 void setResendTimeout(float timeout)
832 { JMutexAutoLock lock(m_exclusive_access_mutex); resend_timeout = timeout; }
833 bool Ping(float dtime,SharedBuffer<u8>& data);
835 Channel channels[CHANNEL_COUNT];
836 bool m_pending_disconnect;
838 // This is changed dynamically
839 float resend_timeout;
841 bool processReliableSendCommand(
842 ConnectionCommand &c,
843 unsigned int max_packet_size);
852 enum ConnectionEventType{
854 CONNEVENT_DATA_RECEIVED,
855 CONNEVENT_PEER_ADDED,
856 CONNEVENT_PEER_REMOVED,
857 CONNEVENT_BIND_FAILED,
860 struct ConnectionEvent
862 enum ConnectionEventType type;
868 ConnectionEvent(): type(CONNEVENT_NONE), peer_id(0),
871 std::string describe()
875 return "CONNEVENT_NONE";
876 case CONNEVENT_DATA_RECEIVED:
877 return "CONNEVENT_DATA_RECEIVED";
878 case CONNEVENT_PEER_ADDED:
879 return "CONNEVENT_PEER_ADDED";
880 case CONNEVENT_PEER_REMOVED:
881 return "CONNEVENT_PEER_REMOVED";
882 case CONNEVENT_BIND_FAILED:
883 return "CONNEVENT_BIND_FAILED";
885 return "Invalid ConnectionEvent";
888 void dataReceived(u16 peer_id_, SharedBuffer<u8> data_)
890 type = CONNEVENT_DATA_RECEIVED;
894 void peerAdded(u16 peer_id_, Address address_)
896 type = CONNEVENT_PEER_ADDED;
900 void peerRemoved(u16 peer_id_, bool timeout_, Address address_)
902 type = CONNEVENT_PEER_REMOVED;
909 type = CONNEVENT_BIND_FAILED;
913 class ConnectionSendThread : public JThread {
916 friend class UDPPeer;
918 ConnectionSendThread(unsigned int max_packet_size, float timeout);
924 void setParent(Connection* parent) {
925 assert(parent != NULL); // Pre-condition
926 m_connection = parent;
929 void setPeerTimeout(float peer_timeout)
930 { m_timeout = peer_timeout; }
933 void runTimeouts (float dtime);
934 void rawSend (const BufferedPacket &packet);
935 bool rawSendAsPacket(u16 peer_id, u8 channelnum,
936 SharedBuffer<u8> data, bool reliable);
938 void processReliableCommand (ConnectionCommand &c);
939 void processNonReliableCommand (ConnectionCommand &c);
940 void serve (Address bind_address);
941 void connect (Address address);
943 void disconnect_peer(u16 peer_id);
944 void send (u16 peer_id, u8 channelnum,
945 SharedBuffer<u8> data);
946 void sendReliable (ConnectionCommand &c);
947 void sendToAll (u8 channelnum,
948 SharedBuffer<u8> data);
949 void sendToAllReliable(ConnectionCommand &c);
951 void sendPackets (float dtime);
953 void sendAsPacket (u16 peer_id, u8 channelnum,
954 SharedBuffer<u8> data,bool ack=false);
956 void sendAsPacketReliable(BufferedPacket& p, Channel* channel);
958 bool packetsQueued();
960 Connection* m_connection;
961 unsigned int m_max_packet_size;
963 std::queue<OutgoingPacket> m_outgoing_queue;
964 JSemaphore m_send_sleep_semaphore;
966 unsigned int m_iteration_packets_avaialble;
967 unsigned int m_max_commands_per_iteration;
968 unsigned int m_max_data_packets_per_iteration;
969 unsigned int m_max_packets_requeued;
972 class ConnectionReceiveThread : public JThread {
974 ConnectionReceiveThread(unsigned int max_packet_size);
978 void setParent(Connection* parent) {
979 assert(parent != NULL); // Pre-condition
980 m_connection = parent;
986 // Returns next data from a buffer if possible
987 // If found, returns true; if not, false.
988 // If found, sets peer_id and dst
989 bool getFromBuffers (u16 &peer_id, SharedBuffer<u8> &dst);
991 bool checkIncomingBuffers(Channel *channel, u16 &peer_id,
992 SharedBuffer<u8> &dst);
995 Processes a packet with the basic header stripped out.
997 packetdata: Data in packet (with no base headers)
998 peer_id: peer id of the sender of the packet in question
999 channelnum: channel on which the packet was sent
1000 reliable: true if recursing into a reliable packet
1002 SharedBuffer<u8> processPacket(Channel *channel,
1003 SharedBuffer<u8> packetdata, u16 peer_id,
1004 u8 channelnum, bool reliable);
1007 Connection* m_connection;
1013 friend class ConnectionSendThread;
1014 friend class ConnectionReceiveThread;
1016 Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6);
1017 Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6,
1018 PeerHandler *peerhandler);
1022 ConnectionEvent getEvent();
1023 ConnectionEvent waitEvent(u32 timeout_ms);
1024 void putCommand(ConnectionCommand &c);
1026 void SetTimeoutMs(int timeout) { m_bc_receive_timeout = timeout; }
1027 void Serve(Address bind_addr);
1028 void Connect(Address address);
1031 void Receive(NetworkPacket* pkt);
1032 void Send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
1033 u16 GetPeerID() { return m_peer_id; }
1034 Address GetPeerAddress(u16 peer_id);
1035 float getPeerStat(u16 peer_id, rtt_stat_type type);
1036 float getLocalStat(rate_stat_type type);
1037 const u32 GetProtocolID() const { return m_protocol_id; };
1038 const std::string getDesc();
1039 void DisconnectPeer(u16 peer_id);
1042 PeerHelper getPeer(u16 peer_id);
1043 PeerHelper getPeerNoEx(u16 peer_id);
1044 u16 lookupPeer(Address& sender);
1046 u16 createPeer(Address& sender, MTProtocols protocol, int fd);
1047 UDPPeer* createServerPeer(Address& sender);
1048 bool deletePeer(u16 peer_id, bool timeout);
1050 void SetPeerID(u16 id) { m_peer_id = id; }
1052 void sendAck(u16 peer_id, u8 channelnum, u16 seqnum);
1054 void PrintInfo(std::ostream &out);
1057 std::list<u16> getPeerIDs() { return m_peer_ids; }
1059 UDPSocket m_udpSocket;
1060 MutexedQueue<ConnectionCommand> m_command_queue;
1062 void putEvent(ConnectionEvent &e);
1065 { m_sendThread.Trigger(); }
1067 std::list<Peer*> getPeers();
1069 MutexedQueue<ConnectionEvent> m_event_queue;
1074 std::map<u16, Peer*> m_peers;
1075 std::list<u16> m_peer_ids;
1076 JMutex m_peers_mutex;
1078 ConnectionSendThread m_sendThread;
1079 ConnectionReceiveThread m_receiveThread;
1081 JMutex m_info_mutex;
1083 // Backwards compatibility
1084 PeerHandler *m_bc_peerhandler;
1085 int m_bc_receive_timeout;
1087 bool m_shutting_down;
1089 u16 m_next_remote_peer_id;