Network cleanup (#6302)
[oweals/minetest.git] / src / network / connection.h
index 3dc87b489eaf4995844703aa5a2efb4eeecd8222..3d3a502aafb55a2c2292eb78d55208cf8baebf52 100644 (file)
@@ -17,12 +17,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef CONNECTION_HEADER
-#define CONNECTION_HEADER
+#pragma once
 
 #include "irrlichttypes_bloated.h"
+#include "peerhandler.h"
 #include "socket.h"
-#include "exceptions.h"
 #include "constants.h"
 #include "util/pointer.h"
 #include "util/container.h"
@@ -38,89 +37,6 @@ class NetworkPacket;
 namespace con
 {
 
-/*
-       Exceptions
-*/
-class NotFoundException : public BaseException
-{
-public:
-       NotFoundException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class PeerNotFoundException : public BaseException
-{
-public:
-       PeerNotFoundException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class ConnectionException : public BaseException
-{
-public:
-       ConnectionException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class ConnectionBindFailed : public BaseException
-{
-public:
-       ConnectionBindFailed(const char *s):
-               BaseException(s)
-       {}
-};
-
-class InvalidIncomingDataException : public BaseException
-{
-public:
-       InvalidIncomingDataException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class InvalidOutgoingDataException : public BaseException
-{
-public:
-       InvalidOutgoingDataException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class NoIncomingDataException : public BaseException
-{
-public:
-       NoIncomingDataException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class ProcessedSilentlyException : public BaseException
-{
-public:
-       ProcessedSilentlyException(const char *s):
-               BaseException(s)
-       {}
-};
-
-class ProcessedQueued : public BaseException
-{
-public:
-       ProcessedQueued(const char *s):
-               BaseException(s)
-       {}
-};
-
-class IncomingDataCorruption : public BaseException
-{
-public:
-       IncomingDataCorruption(const char *s):
-               BaseException(s)
-       {}
-};
-
 typedef enum MTProtocols {
        MTP_PRIMARY,
        MTP_UDP,
@@ -134,16 +50,14 @@ inline bool seqnum_higher(u16 totest, u16 base)
        {
                if ((totest - base) > (SEQNUM_MAX/2))
                        return false;
-               else
-                       return true;
-       }
-       else
-       {
-               if ((base - totest) > (SEQNUM_MAX/2))
-                       return true;
-               else
-                       return false;
+
+               return true;
        }
+
+       if ((base - totest) > (SEQNUM_MAX/2))
+               return true;
+
+       return false;
 }
 
 inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
@@ -151,14 +65,12 @@ inline bool seqnum_in_window(u16 seqnum, u16 next,u16 window_size)
        u16 window_start = next;
        u16 window_end   = ( next + window_size ) % (SEQNUM_MAX+1);
 
-       if (window_start < window_end)
-       {
+       if (window_start < window_end) {
                return ((seqnum >= window_start) && (seqnum < window_end));
        }
-       else
-       {
-               return ((seqnum < window_end) || (seqnum >= window_start));
-       }
+
+
+       return ((seqnum < window_end) || (seqnum >= window_start));
 }
 
 struct BufferedPacket
@@ -167,8 +79,7 @@ struct BufferedPacket
                data(a_data, a_size)
        {}
        BufferedPacket(u32 a_size):
-               data(a_size), time(0.0), totaltime(0.0), absolute_send_time(-1),
-               resend_count(0)
+               data(a_size)
        {}
        Buffer<u8> data; // Data of the packet, including headers
        float time = 0.0f; // Seconds from buffering the packet or re-sending
@@ -203,12 +114,13 @@ std::list<SharedBuffer<u8> > makeAutoSplitPacket(
 
 // Add the TYPE_RELIABLE header to the data
 SharedBuffer<u8> makeReliablePacket(
-               SharedBuffer<u8> data,
+               const SharedBuffer<u8> &data,
                u16 seqnum);
 
 struct IncomingSplitPacket
 {
-       IncomingSplitPacket() {}
+       IncomingSplitPacket() = default;
+
        // Key is chunk number, value is data without headers
        std::map<u16, SharedBuffer<u8> > chunks;
        u32 chunk_count;
@@ -316,7 +228,7 @@ typedef std::list<BufferedPacket>::iterator RPBSearchResult;
 class ReliablePacketBuffer
 {
 public:
-       ReliablePacketBuffer() {};
+       ReliablePacketBuffer() = default;
 
        bool getFirstSeqnum(u16& result);
 
@@ -411,7 +323,7 @@ struct ConnectionCommand
        bool reliable = false;
        bool raw = false;
 
-       ConnectionCommand() {}
+       ConnectionCommand() = default;
 
        void serve(Address address_)
        {
@@ -502,8 +414,8 @@ public:
 
        IncomingSplitBuffer incoming_splits;
 
-       Channel() {};
-       ~Channel() {};
+       Channel() = default;
+       ~Channel() = default;
 
        void UpdatePacketLossCounter(unsigned int count);
        void UpdatePacketTooLateCounter();
@@ -571,49 +483,10 @@ private:
 
 class Peer;
 
-enum PeerChangeType
-{
-       PEER_ADDED,
-       PEER_REMOVED
-};
-struct PeerChange
-{
-       PeerChange(PeerChangeType t, u16 _peer_id, bool _timeout):
-               type(t), peer_id(_peer_id), timeout(_timeout) {}
-       PeerChange() = delete;
-
-       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.
-       */
-       virtual void peerAdded(Peer *peer) = 0;
-       /*
-               This is called before the Peer has been removed from the
-               Connection's peer container.
-       */
-       virtual void deletingPeer(Peer *peer, bool timeout) = 0;
-};
-
 class PeerHelper
 {
 public:
-       PeerHelper() {};
+       PeerHelper() = default;
        PeerHelper(Peer* peer);
        ~PeerHelper();
 
@@ -629,15 +502,6 @@ private:
 
 class Connection;
 
-typedef enum {
-       MIN_RTT,
-       MAX_RTT,
-       AVG_RTT,
-       MIN_JITTER,
-       MAX_JITTER,
-       AVG_JITTER
-} rtt_stat_type;
-
 typedef enum {
        CUR_DL_RATE,
        AVG_DL_RATE,
@@ -745,7 +609,7 @@ class Peer {
                        float max_rtt = 0.0f;
                        float avg_rtt = -1.0f;
 
-                       rttstats() {};
+                       rttstats() = default;
                };
 
                rttstats m_rtt;
@@ -770,7 +634,7 @@ public:
        friend class Connection;
 
        UDPPeer(u16 a_id, Address a_address, Connection* connection);
-       virtual ~UDPPeer() {};
+       virtual ~UDPPeer() = default;
 
        void PutReliableSendCommand(ConnectionCommand &c,
                                                        unsigned int max_packet_size);
@@ -842,7 +706,7 @@ struct ConnectionEvent
        bool timeout = false;
        Address address;
 
-       ConnectionEvent() {}
+       ConnectionEvent() = default;
 
        std::string describe()
        {
@@ -983,6 +847,8 @@ private:
        Connection *m_connection = nullptr;
 };
 
+class PeerHandler;
+
 class Connection
 {
 public:
@@ -1068,5 +934,3 @@ private:
 };
 
 } // namespace
-
-#endif