]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/connection.h
FIx wrong error message on invalid use of the formspec element image_button
[dragonfireclient.git] / src / connection.h
index 570bc92ab22ba817873c0e549af2a59882b46940..a1d564849810b1afc1d995ed858c56d2cf03db72 100644 (file)
@@ -1,18 +1,18 @@
 /*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
@@ -20,14 +20,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef CONNECTION_HEADER
 #define CONNECTION_HEADER
 
-#include <iostream>
-#include <fstream>
-#include "debug.h"
-#include "common_irrlicht.h"
+#include "irrlichttypes_bloated.h"
 #include "socket.h"
-#include "utility.h"
 #include "exceptions.h"
 #include "constants.h"
+#include "util/pointer.h"
+#include "util/container.h"
+#include "util/thread.h"
+#include <iostream>
+#include <fstream>
+#include <list>
+#include <map>
 
 namespace con
 {
@@ -59,6 +62,14 @@ class ConnectionException : public BaseException
        {}
 };
 
+class ConnectionBindFailed : public BaseException
+{
+public:
+       ConnectionBindFailed(const char *s):
+               BaseException(s)
+       {}
+};
+
 /*class ThrottlingException : public BaseException
 {
 public:
@@ -99,15 +110,6 @@ class ProcessedSilentlyException : public BaseException
        {}
 };
 
-inline u16 readPeerId(u8 *packetdata)
-{
-       return readU16(&packetdata[4]);
-}
-inline u8 readChannel(u8 *packetdata)
-{
-       return readU8(&packetdata[6]);
-}
-
 #define SEQNUM_MAX 65535
 inline bool seqnum_higher(u16 higher, u16 lower)
 {
@@ -142,14 +144,14 @@ SharedBuffer<u8> makeOriginalPacket(
                SharedBuffer<u8> data);
 
 // Split data in chunks and add TYPE_SPLIT headers to them
-core::list<SharedBuffer<u8> > makeSplitPacket(
+std::list<SharedBuffer<u8> > makeSplitPacket(
                SharedBuffer<u8> data,
                u32 chunksize_max,
                u16 seqnum);
 
 // Depending on size, make a TYPE_ORIGINAL or TYPE_SPLIT packet
 // Increments split_seqnum if a split packet is made
-core::list<SharedBuffer<u8> > makeAutoSplitPacket(
+std::list<SharedBuffer<u8> > makeAutoSplitPacket(
                SharedBuffer<u8> data,
                u32 chunksize_max,
                u16 &split_seqnum);
@@ -167,7 +169,7 @@ struct IncomingSplitPacket
                reliable = false;
        }
        // Key is chunk number, value is data without headers
-       core::map<u16, SharedBuffer<u8> > chunks;
+       std::map<u16, SharedBuffer<u8> > chunks;
        u32 chunk_count;
        float time; // Seconds from adding
        bool reliable; // If true, isn't deleted on timeout
@@ -189,15 +191,14 @@ TODO: Should we have a receiver_peer_id also?
        [6] u8 channel
 sender_peer_id:
        Unique to each peer.
-       value 0 is reserved for making new connections
-       value 1 is reserved for server
+       value 0 (PEER_ID_INEXISTENT) is reserved for making new connections
+       value 1 (PEER_ID_SERVER) is reserved for server
+       these constants are defined in constants.h
 channel:
        The lower the number, the higher the priority is.
        Only channels 0, 1 and 2 exist.
 */
 #define BASE_HEADER_SIZE 7
-#define PEER_ID_INEXISTENT 0
-#define PEER_ID_SERVER 1
 #define CHANNEL_COUNT 3
 /*
 Packet types:
@@ -268,28 +269,29 @@ with a buffer in the receiving and transmitting end.
        for fast access to the smallest one.
 */
 
-typedef core::list<BufferedPacket>::Iterator RPBSearchResult;
+typedef std::list<BufferedPacket>::iterator RPBSearchResult;
 
 class ReliablePacketBuffer
 {
 public:
-       
+       ReliablePacketBuffer();
        void print();
        bool empty();
        u32 size();
        RPBSearchResult findPacket(u16 seqnum);
        RPBSearchResult notFound();
-       u16 getFirstSeqnum();
+       bool getFirstSeqnum(u16 *result);
        BufferedPacket popFirst();
        BufferedPacket popSeqnum(u16 seqnum);
        void insert(BufferedPacket &p);
        void incrementTimeouts(float dtime);
        void resetTimedOuts(float timeout);
        bool anyTotaltimeReached(float timeout);
-       core::list<BufferedPacket> getTimedOuts(float timeout);
+       std::list<BufferedPacket> getTimedOuts(float timeout);
 
 private:
-       core::list<BufferedPacket> m_list;
+       std::list<BufferedPacket> m_list;
+       u16 m_list_size;
 };
 
 /*
@@ -310,7 +312,7 @@ class IncomingSplitBuffer
        
 private:
        // Key is seqnum
-       core::map<u16, IncomingSplitPacket*> m_buf;
+       std::map<u16, IncomingSplitPacket*> m_buf;
 };
 
 class Connection;
@@ -394,7 +396,11 @@ class Peer
        float m_max_packets_per_second;
        int m_num_sent;
        int m_max_num_sent;
-       
+
+       // Updated from configuration by Connection
+       float congestion_control_aim_rtt;
+       float congestion_control_max_rate;
+       float congestion_control_min_rate;
 private:
 };
 
@@ -424,13 +430,14 @@ enum ConnectionEventType{
        CONNEVENT_DATA_RECEIVED,
        CONNEVENT_PEER_ADDED,
        CONNEVENT_PEER_REMOVED,
+       CONNEVENT_BIND_FAILED,
 };
 
 struct ConnectionEvent
 {
        enum ConnectionEventType type;
        u16 peer_id;
-       SharedBuffer<u8> data;
+       Buffer<u8> data;
        bool timeout;
        Address address;
 
@@ -447,6 +454,8 @@ struct ConnectionEvent
                        return "CONNEVENT_PEER_ADDED";
                case CONNEVENT_PEER_REMOVED: 
                        return "CONNEVENT_PEER_REMOVED";
+               case CONNEVENT_BIND_FAILED: 
+                       return "CONNEVENT_BIND_FAILED";
                }
                return "Invalid ConnectionEvent";
        }
@@ -470,6 +479,10 @@ struct ConnectionEvent
                timeout = timeout_;
                address = address_;
        }
+       void bindFailed()
+       {
+               type = CONNEVENT_BIND_FAILED;
+       }
 };
 
 enum ConnectionCommandType{
@@ -489,7 +502,7 @@ struct ConnectionCommand
        Address address;
        u16 peer_id;
        u8 channelnum;
-       SharedBuffer<u8> data;
+       Buffer<u8> data;
        bool reliable;
        
        ConnectionCommand(): type(CONNCMD_NONE) {}
@@ -534,8 +547,8 @@ struct ConnectionCommand
 class Connection: public SimpleThread
 {
 public:
-       Connection(u32 protocol_id, u32 max_packet_size, float timeout);
-       Connection(u32 protocol_id, u32 max_packet_size, float timeout,
+       Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6);
+       Connection(u32 protocol_id, u32 max_packet_size, float timeout, bool ipv6,
                        PeerHandler *peerhandler);
        ~Connection();
        void * Thread();
@@ -551,7 +564,7 @@ class Connection: public SimpleThread
        void Connect(Address address);
        bool Connected();
        void Disconnect();
-       u32 Receive(u16 &peer_id, u8 *data, u32 datasize);
+       u32 Receive(u16 &peer_id, SharedBuffer<u8> &data);
        void SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable);
        void Send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
        void RunTimeouts(float dtime); // dummy
@@ -578,7 +591,7 @@ class Connection: public SimpleThread
        void rawSend(const BufferedPacket &packet);
        Peer* getPeer(u16 peer_id);
        Peer* getPeerNoEx(u16 peer_id);
-       core::list<Peer*> getPeers();
+       std::list<Peer*> getPeers();
        bool getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst);
        // Returns next data from a buffer if possible
        // If found, returns true; if not, false.
@@ -608,7 +621,7 @@ class Connection: public SimpleThread
        UDPSocket m_socket;
        u16 m_peer_id;
        
-       core::map<u16, Peer*> m_peers;
+       std::map<u16, Peer*> m_peers;
        JMutex m_peers_mutex;
 
        // Backwards compatibility