]> git.lizzy.rs Git - minetest.git/blobdiff - src/connection.cpp
Deduplicate code and use stdlib in string functions
[minetest.git] / src / connection.cpp
index bd0d872caf5a3f08c00b7825e5746b2a3181d973..02ca91a91bbf83b1b4dfb7bd60b37d2189d8a5ff 100644 (file)
@@ -1236,10 +1236,9 @@ SharedBuffer<u8> UDPPeer::addSpiltPacket(u8 channel,
 /* Connection Threads                                                         */
 /******************************************************************************/
 
-ConnectionSendThread::ConnectionSendThread(Connection* parent,
-                                                                                       unsigned int max_packet_size,
+ConnectionSendThread::ConnectionSendThread( unsigned int max_packet_size,
                                                                                        float timeout) :
-       m_connection(parent),
+       m_connection(NULL),
        m_max_packet_size(max_packet_size),
        m_timeout(timeout),
        m_max_commands_per_iteration(1),
@@ -1250,6 +1249,7 @@ ConnectionSendThread::ConnectionSendThread(Connection* parent,
 
 void * ConnectionSendThread::Thread()
 {
+       assert(m_connection != NULL);
        ThreadStarted();
        log_register_thread("ConnectionSend");
 
@@ -1300,7 +1300,7 @@ void * ConnectionSendThread::Thread()
                /* send non reliable packets */
                sendPackets(dtime);
 
-               END_DEBUG_EXCEPTION_HANDLER(derr_con);
+               END_DEBUG_EXCEPTION_HANDLER(errorstream);
        }
 
        PROFILE(g_profiler->remove(ThreadIdentifier.str()));
@@ -1316,7 +1316,7 @@ bool ConnectionSendThread::packetsQueued()
 {
        std::list<u16> peerIds = m_connection->getPeerIDs();
 
-       if ((this->m_outgoing_queue.size() > 0) && (peerIds.size() > 0))
+       if (!m_outgoing_queue.empty() && !peerIds.empty())
                return true;
 
        for(std::list<u16>::iterator j = peerIds.begin();
@@ -1995,14 +1995,14 @@ void ConnectionSendThread::sendAsPacket(u16 peer_id, u8 channelnum,
        m_outgoing_queue.push_back(packet);
 }
 
-ConnectionReceiveThread::ConnectionReceiveThread(Connection* parent,
-               unsigned int max_packet_size) :
-       m_connection(parent)
+ConnectionReceiveThread::ConnectionReceiveThread(unsigned int max_packet_size) :
+       m_connection(NULL)
 {
 }
 
 void * ConnectionReceiveThread::Thread()
 {
+       assert(m_connection != NULL);
        ThreadStarted();
        log_register_thread("ConnectionReceive");
 
@@ -2085,7 +2085,7 @@ void * ConnectionReceiveThread::Thread()
                        }
                }
 #endif
-               END_DEBUG_EXCEPTION_HANDLER(derr_con);
+               END_DEBUG_EXCEPTION_HANDLER(errorstream);
        }
        PROFILE(g_profiler->remove(ThreadIdentifier.str()));
        return NULL;
@@ -2322,13 +2322,23 @@ bool ConnectionReceiveThread::checkIncomingBuffers(Channel *channel,
 SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
                SharedBuffer<u8> packetdata, u16 peer_id, u8 channelnum, bool reliable)
 {
-       PeerHelper peer = m_connection->getPeer(peer_id);
+       PeerHelper peer = m_connection->getPeerNoEx(peer_id);
+
+       if (!peer) {
+               errorstream << "Peer not found (possible timeout)" << std::endl;
+               throw ProcessedSilentlyException("Peer not found (possible timeout)");
+       }
 
        if(packetdata.getSize() < 1)
                throw InvalidIncomingDataException("packetdata.getSize() < 1");
 
        u8 type = readU8(&(packetdata[0]));
 
+       if (MAX_UDP_PEERS <= 65535 && peer_id >= MAX_UDP_PEERS) {
+               errorstream << "Something is wrong with peer_id" << std::endl;
+               assert(0);
+       }
+
        if(type == TYPE_CONTROL)
        {
                if(packetdata.getSize() < 2)
@@ -2336,8 +2346,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
 
                u8 controltype = readU8(&(packetdata[1]));
 
-               if( (controltype == CONTROLTYPE_ACK)
-                               && (peer_id <= MAX_UDP_PEERS))
+               if(controltype == CONTROLTYPE_ACK)
                {
                        assert(channel != 0);
                        if(packetdata.getSize() < 4)
@@ -2394,8 +2403,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
                        }
                        throw ProcessedSilentlyException("Got an ACK");
                }
-               else if((controltype == CONTROLTYPE_SET_PEER_ID)
-                               && (peer_id <= MAX_UDP_PEERS))
+               else if(controltype == CONTROLTYPE_SET_PEER_ID)
                {
                        // Got a packet to set our peer id
                        if(packetdata.getSize() < 4)
@@ -2427,8 +2435,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
 
                        throw ProcessedSilentlyException("Got a SET_PEER_ID");
                }
-               else if((controltype == CONTROLTYPE_PING)
-                               && (peer_id <= MAX_UDP_PEERS))
+               else if(controltype == CONTROLTYPE_PING)
                {
                        // Just ignore it, the incoming data already reset
                        // the timeout counter
@@ -2450,8 +2457,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
 
                        throw ProcessedSilentlyException("Got a DISCO");
                }
-               else if((controltype == CONTROLTYPE_ENABLE_BIG_SEND_WINDOW)
-                               && (peer_id <= MAX_UDP_PEERS))
+               else if(controltype == CONTROLTYPE_ENABLE_BIG_SEND_WINDOW)
                {
                        dynamic_cast<UDPPeer*>(&peer)->setNonLegacyPeer();
                        throw ProcessedSilentlyException("Got non legacy control");
@@ -2509,7 +2515,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
                        //TODO throw some error
                }
        }
-       else if((peer_id <= MAX_UDP_PEERS) && (type == TYPE_RELIABLE))
+       else if(type == TYPE_RELIABLE)
        {
                assert(channel != 0);
                // Recursive reliable packets not allowed
@@ -2652,8 +2658,8 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
        m_event_queue(),
        m_peer_id(0),
        m_protocol_id(protocol_id),
-       m_sendThread(this, max_packet_size, timeout),
-       m_receiveThread(this, max_packet_size),
+       m_sendThread(max_packet_size, timeout),
+       m_receiveThread(max_packet_size),
        m_info_mutex(),
        m_bc_peerhandler(0),
        m_bc_receive_timeout(0),
@@ -2662,6 +2668,9 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
 {
        m_udpSocket.setTimeoutMs(5);
 
+       m_sendThread.setParent(this);
+       m_receiveThread.setParent(this);
+
        m_sendThread.Start();
        m_receiveThread.Start();
 }
@@ -2673,8 +2682,8 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
        m_event_queue(),
        m_peer_id(0),
        m_protocol_id(protocol_id),
-       m_sendThread(this, max_packet_size, timeout),
-       m_receiveThread(this, max_packet_size),
+       m_sendThread(max_packet_size, timeout),
+       m_receiveThread(max_packet_size),
        m_info_mutex(),
        m_bc_peerhandler(peerhandler),
        m_bc_receive_timeout(0),
@@ -2684,6 +2693,9 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
 {
        m_udpSocket.setTimeoutMs(5);
 
+       m_sendThread.setParent(this);
+       m_receiveThread.setParent(this);
+
        m_sendThread.Start();
        m_receiveThread.Start();