]> git.lizzy.rs Git - minetest.git/blobdiff - src/connection.cpp
Fixes for android
[minetest.git] / src / connection.cpp
index 64ef9a50e42f9638008adeb0f9273190212068f8..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");
 
@@ -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");
 
@@ -2334,6 +2334,11 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
 
        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)
@@ -2341,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)
@@ -2399,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)
@@ -2432,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
@@ -2455,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");
@@ -2514,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
@@ -2657,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),
@@ -2667,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();
 }
@@ -2678,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),
@@ -2689,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();