X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fconnection.h;h=516702cb8e19daa5639b0db0749bfcc711df0117;hb=55c646c5c2b40931d24be8541b3056ab3322a70f;hp=e43d93fb30a7e1cd08365448c5c9bede903c5fa1;hpb=9edb91da5754cf194637d1d7faa513719b61f9b4;p=oweals%2Fminetest.git diff --git a/src/connection.h b/src/connection.h index e43d93fb3..516702cb8 100644 --- a/src/connection.h +++ b/src/connection.h @@ -71,14 +71,6 @@ public: {} }; -/*class ThrottlingException : public BaseException -{ -public: - ThrottlingException(const char *s): - BaseException(s) - {} -};*/ - class InvalidIncomingDataException : public BaseException { public: @@ -128,9 +120,9 @@ public: }; typedef enum MTProtocols { - PRIMARY, - UDP, - MINETEST_RELIABLE_UDP + MTP_PRIMARY, + MTP_UDP, + MTP_MINETEST_RELIABLE_UDP } MTProtocols; #define SEQNUM_MAX 65535 @@ -170,16 +162,19 @@ inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size) struct BufferedPacket { BufferedPacket(u8 *a_data, u32 a_size): - data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1) + data(a_data, a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), + resend_count(0) {} BufferedPacket(u32 a_size): - data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1) + data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1), + resend_count(0) {} SharedBuffer data; // Data of the packet, including headers float time; // Seconds from buffering the packet or re-sending float totaltime; // Seconds from buffering the packet unsigned int absolute_send_time; Address address; // Sender or destination + unsigned int resend_count; }; // This adds the base headers to the data and makes a packet out of it @@ -347,13 +342,11 @@ private: RPBSearchResult findPacket(u16 seqnum); std::list m_list; - u16 m_list_size; + u32 m_list_size; u16 m_oldest_non_answered_ack; JMutex m_list_mutex; - - unsigned int writeptr; }; /* @@ -406,7 +399,6 @@ enum ConnectionCommandType{ CONNCMD_DISCONNECT_PEER, CONNCMD_SEND, CONNCMD_SEND_TO_ALL, - CONNCMD_DELETE_PEER, CONCMD_ACK, CONCMD_CREATE_PEER, CONCMD_DISABLE_LEGACY @@ -415,7 +407,6 @@ enum ConnectionCommandType{ struct ConnectionCommand { enum ConnectionCommandType type; - u16 port; Address address; u16 peer_id; u8 channelnum; @@ -425,10 +416,10 @@ struct ConnectionCommand ConnectionCommand(): type(CONNCMD_NONE), peer_id(PEER_ID_INEXISTENT), reliable(false), raw(false) {} - void serve(u16 port_) + void serve(Address address_) { type = CONNCMD_SERVE; - port = port_; + address = address_; } void connect(Address address_) { @@ -460,11 +451,6 @@ struct ConnectionCommand data = data_; reliable = reliable_; } - void deletePeer(u16 peer_id_) - { - type = CONNCMD_DELETE_PEER; - peer_id = peer_id_; - } void ack(u16 peer_id_, u8 channelnum_, SharedBuffer data_) { @@ -532,8 +518,9 @@ public: void UpdatePacketTooLateCounter(); void UpdateBytesSent(unsigned int bytes,unsigned int packages=1); void UpdateBytesLost(unsigned int bytes); + void UpdateBytesReceived(unsigned int bytes); - void UpdateTimers(float dtime); + void UpdateTimers(float dtime, bool legacy_peer); const float getCurrentDownloadRateKB() { JMutexAutoLock lock(m_internal_mutex); return cur_kbps; }; @@ -545,17 +532,24 @@ public: const float getMaxLossRateKB() { JMutexAutoLock lock(m_internal_mutex); return max_kbps_lost; }; + const float getCurrentIncomingRateKB() + { JMutexAutoLock lock(m_internal_mutex); return cur_incoming_kbps; }; + const float getMaxIncomingRateKB() + { JMutexAutoLock lock(m_internal_mutex); return max_incoming_kbps; }; + const float getAvgDownloadRateKB() { JMutexAutoLock lock(m_internal_mutex); return avg_kbps; }; const float getAvgLossRateKB() { JMutexAutoLock lock(m_internal_mutex); return avg_kbps_lost; }; + const float getAvgIncomingRateKB() + { JMutexAutoLock lock(m_internal_mutex); return avg_incoming_kbps; }; const unsigned int getWindowSize() const { return window_size; }; void setWindowSize(unsigned int size) { window_size = size; }; private: JMutex m_internal_mutex; - unsigned int window_size; + int window_size; u16 next_incoming_seqnum; @@ -568,28 +562,47 @@ private: float packet_loss_counter; unsigned int current_bytes_transfered; + unsigned int current_bytes_received; unsigned int current_bytes_lost; float max_kbps; float cur_kbps; float avg_kbps; + float max_incoming_kbps; + float cur_incoming_kbps; + float avg_incoming_kbps; float max_kbps_lost; float cur_kbps_lost; float avg_kbps_lost; float bpm_counter; + + unsigned int rate_samples; }; class Peer; +enum PeerChangeType +{ + PEER_ADDED, + PEER_REMOVED +}; +struct PeerChange +{ + PeerChangeType type; + u16 peer_id; + bool timeout; +}; + class PeerHandler { public: + PeerHandler() { } virtual ~PeerHandler() { } - + /* This is called after the Peer has been inserted into the Connection's peer container. @@ -621,7 +634,7 @@ private: class Connection; -typedef enum rtt_stat_type { +typedef enum { MIN_RTT, MAX_RTT, AVG_RTT, @@ -630,6 +643,15 @@ typedef enum rtt_stat_type { AVG_JITTER } rtt_stat_type; +typedef enum { + CUR_DL_RATE, + AVG_DL_RATE, + CUR_INC_RATE, + AVG_INC_RATE, + CUR_LOSS_RATE, + AVG_LOSS_RATE, +} rate_stat_type; + class Peer { public: friend class PeerHelper; @@ -769,9 +791,10 @@ public: friend class PeerHelper; friend class ConnectionReceiveThread; friend class ConnectionSendThread; + friend class Connection; UDPPeer(u16 a_id, Address a_address, Connection* connection); - virtual ~UDPPeer(); + virtual ~UDPPeer() {}; void PutReliableSendCommand(ConnectionCommand &c, unsigned int max_packet_size); @@ -781,8 +804,7 @@ public: bool getAddress(MTProtocols type, Address& toset); - void setNonLegacyPeer() - { m_legacy_peer = false; } + void setNonLegacyPeer(); bool getLegacyPeer() { return m_legacy_peer; } @@ -793,6 +815,8 @@ public: SharedBuffer addSpiltPacket(u8 channel, BufferedPacket toadd, bool reliable); + + protected: /* Calculates avg_rtt and resend_timeout. @@ -813,6 +837,7 @@ protected: bool Ping(float dtime,SharedBuffer& data); Channel channels[CHANNEL_COUNT]; + bool m_pending_disconnect; private: // This is changed dynamically float resend_timeout; @@ -911,7 +936,7 @@ private: void processReliableCommand (ConnectionCommand &c); void processNonReliableCommand (ConnectionCommand &c); - void serve (u16 port); + void serve (Address bind_address); void connect (Address address); void disconnect (); void disconnect_peer(u16 peer_id); @@ -975,7 +1000,6 @@ private: Connection* m_connection; - unsigned int m_max_packet_size; }; class Connection @@ -995,20 +1019,20 @@ public: void putCommand(ConnectionCommand &c); void SetTimeoutMs(int timeout){ m_bc_receive_timeout = timeout; } - void Serve(unsigned short port); + void Serve(Address bind_addr); void Connect(Address address); bool Connected(); void Disconnect(); u32 Receive(u16 &peer_id, SharedBuffer &data); void SendToAll(u8 channelnum, SharedBuffer data, bool reliable); void Send(u16 peer_id, u8 channelnum, SharedBuffer data, bool reliable); - void RunTimeouts(float dtime); // dummy u16 GetPeerID(){ return m_peer_id; } Address GetPeerAddress(u16 peer_id); - float GetPeerAvgRTT(u16 peer_id); - void DeletePeer(u16 peer_id); + float getPeerStat(u16 peer_id, rtt_stat_type type); + float getLocalStat(rate_stat_type type); const u32 GetProtocolID() const { return m_protocol_id; }; const std::string getDesc(); + void DisconnectPeer(u16 peer_id); protected: PeerHelper getPeer(u16 peer_id); @@ -1033,6 +1057,8 @@ protected: void putEvent(ConnectionEvent &e); + void TriggerSend() + { m_sendThread.Trigger(); } private: std::list getPeers(); @@ -1054,6 +1080,8 @@ private: int m_bc_receive_timeout; bool m_shutting_down; + + u16 m_next_remote_peer_id; }; } // namespace