]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/network/connectionthreads.cpp
minimal: Move get_craft_result tests to test mod
[dragonfireclient.git] / src / network / connectionthreads.cpp
index 63c1855c5036d8b04160ddb9e0bc846edbf4957e..f8b58c025fd4c886391df5d4895d7659b670e74c 100644 (file)
@@ -53,7 +53,7 @@ std::mutex log_conthread_mutex;
 
 #define WINDOW_SIZE 5
 
-static u16 readPeerId(u8 *packetdata)
+static session_t readPeerId(u8 *packetdata)
 {
        return readU16(&packetdata[4]);
 }
@@ -138,12 +138,12 @@ void ConnectionSendThread::Trigger()
 
 bool ConnectionSendThread::packetsQueued()
 {
-       std::list<u16> peerIds = m_connection->getPeerIDs();
+       std::list<session_t> peerIds = m_connection->getPeerIDs();
 
        if (!m_outgoing_queue.empty() && !peerIds.empty())
                return true;
 
-       for (u16 peerId : peerIds) {
+       for (session_t peerId : peerIds) {
                PeerHelper peer = m_connection->getPeerNoEx(peerId);
 
                if (!peer)
@@ -165,10 +165,10 @@ bool ConnectionSendThread::packetsQueued()
 
 void ConnectionSendThread::runTimeouts(float dtime)
 {
-       std::list<u16> timeouted_peers;
-       std::list<u16> peerIds = m_connection->getPeerIDs();
+       std::list<session_t> timeouted_peers;
+       std::list<session_t> peerIds = m_connection->getPeerIDs();
 
-       for (u16 &peerId : peerIds) {
+       for (session_t &peerId : peerIds) {
                PeerHelper peer = m_connection->getPeerNoEx(peerId);
 
                if (!peer)
@@ -206,9 +206,6 @@ void ConnectionSendThread::runTimeouts(float dtime)
                for (Channel &channel : udpPeer->channels) {
                        std::list<BufferedPacket> timed_outs;
 
-                       if (udpPeer->getLegacyPeer())
-                               channel.setWindowSize(WINDOW_SIZE);
-
                        // Remove timed out incomplete unreliable split packets
                        channel.incoming_splits.removeUnreliableTimedOuts(dtime, m_timeout);
 
@@ -231,7 +228,7 @@ void ConnectionSendThread::runTimeouts(float dtime)
 
                        for (std::list<BufferedPacket>::iterator k = timed_outs.begin();
                                k != timed_outs.end(); ++k) {
-                               u16 peer_id = readPeerId(*(k->data));
+                               session_t peer_id = readPeerId(*(k->data));
                                u8 channelnum = readChannel(*(k->data));
                                u16 seqnum = readU16(&(k->data[BASE_HEADER_SIZE + 1]));
 
@@ -264,7 +261,7 @@ void ConnectionSendThread::runTimeouts(float dtime)
                                break; /* no need to check other channels if we already did timeout */
                        }
 
-                       channel.UpdateTimers(dtime, udpPeer->getLegacyPeer());
+                       channel.UpdateTimers(dtime);
                }
 
                /* skip to next peer if we did timeout */
@@ -329,7 +326,7 @@ void ConnectionSendThread::sendAsPacketReliable(BufferedPacket &p, Channel *chan
        rawSend(p);
 }
 
-bool ConnectionSendThread::rawSendAsPacket(u16 peer_id, u8 channelnum,
+bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
        const SharedBuffer<u8> &data, bool reliable)
 {
        PeerHelper peer = m_connection->getPeerNoEx(peer_id);
@@ -365,7 +362,7 @@ bool ConnectionSendThread::rawSendAsPacket(u16 peer_id, u8 channelnum,
                        < channel->getWindowSize()) {
                        LOG(dout_con << m_connection->getDesc()
                                << " INFO: sending a reliable packet to peer_id " << peer_id
-                               << " channel: " << channelnum
+                               << " channel: " << (u32)channelnum
                                << " seqnum: " << seqnum << std::endl);
                        sendAsPacketReliable(p, channel);
                        return true;
@@ -373,7 +370,7 @@ bool ConnectionSendThread::rawSendAsPacket(u16 peer_id, u8 channelnum,
 
                LOG(dout_con << m_connection->getDesc()
                        << " INFO: queueing reliable packet for peer_id: " << peer_id
-                       << " channel: " << channelnum
+                       << " channel: " << (u32)channelnum
                        << " seqnum: " << seqnum << std::endl);
                channel->queued_reliables.push(p);
                return false;
@@ -428,15 +425,6 @@ void ConnectionSendThread::processReliableCommand(ConnectionCommand &c)
                        }
                        return;
 
-               case CONCMD_DISABLE_LEGACY:
-                       LOG(dout_con << m_connection->getDesc()
-                               << "UDP processing reliable CONCMD_DISABLE_LEGACY" << std::endl);
-                       if (!rawSendAsPacket(c.peer_id, c.channelnum, c.data, c.reliable)) {
-                               /* put to queue if we couldn't send it immediately */
-                               sendReliable(c);
-                       }
-                       return;
-
                case CONNCMD_SERVE:
                case CONNCMD_CONNECT:
                case CONNCMD_DISCONNECT:
@@ -557,14 +545,14 @@ void ConnectionSendThread::disconnect()
 
 
        // Send to all
-       std::list<u16> peerids = m_connection->getPeerIDs();
+       std::list<session_t> peerids = m_connection->getPeerIDs();
 
-       for (u16 peerid : peerids) {
+       for (session_t peerid : peerids) {
                sendAsPacket(peerid, 0, data, false);
        }
 }
 
-void ConnectionSendThread::disconnect_peer(u16 peer_id)
+void ConnectionSendThread::disconnect_peer(session_t peer_id)
 {
        LOG(dout_con << m_connection->getDesc() << " disconnecting peer" << std::endl);
 
@@ -586,8 +574,8 @@ void ConnectionSendThread::disconnect_peer(u16 peer_id)
        dynamic_cast<UDPPeer *>(&peer)->m_pending_disconnect = true;
 }
 
-void ConnectionSendThread::send(u16 peer_id, u8 channelnum,
-       SharedBuffer<u8> data)
+void ConnectionSendThread::send(session_t peer_id, u8 channelnum,
+       const SharedBuffer<u8> &data)
 {
        assert(channelnum < CHANNEL_COUNT); // Pre-condition
 
@@ -627,20 +615,20 @@ void ConnectionSendThread::sendReliable(ConnectionCommand &c)
        peer->PutReliableSendCommand(c, m_max_packet_size);
 }
 
-void ConnectionSendThread::sendToAll(u8 channelnum, SharedBuffer<u8> data)
+void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer<u8> &data)
 {
-       std::list<u16> peerids = m_connection->getPeerIDs();
+       std::list<session_t> peerids = m_connection->getPeerIDs();
 
-       for (u16 peerid : peerids) {
+       for (session_t peerid : peerids) {
                send(peerid, channelnum, data);
        }
 }
 
 void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)
 {
-       std::list<u16> peerids = m_connection->getPeerIDs();
+       std::list<session_t> peerids = m_connection->getPeerIDs();
 
-       for (u16 peerid : peerids) {
+       for (session_t peerid : peerids) {
                PeerHelper peer = m_connection->getPeerNoEx(peerid);
 
                if (!peer)
@@ -652,11 +640,11 @@ void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)
 
 void ConnectionSendThread::sendPackets(float dtime)
 {
-       std::list<u16> peerIds = m_connection->getPeerIDs();
-       std::list<u16> pendingDisconnect;
-       std::map<u16, bool> pending_unreliable;
+       std::list<session_t> peerIds = m_connection->getPeerIDs();
+       std::list<session_t> pendingDisconnect;
+       std::map<session_t, bool> pending_unreliable;
 
-       for (u16 peerId : peerIds) {
+       for (session_t peerId : peerIds) {
                PeerHelper peer = m_connection->getPeerNoEx(peerId);
                //peer may have been removed
                if (!peer) {
@@ -780,15 +768,15 @@ void ConnectionSendThread::sendPackets(float dtime)
                }
        }
 
-       for (u16 peerId : pendingDisconnect) {
+       for (session_t peerId : pendingDisconnect) {
                if (!pending_unreliable[peerId]) {
                        m_connection->deletePeer(peerId, false);
                }
        }
 }
 
-void ConnectionSendThread::sendAsPacket(u16 peer_id, u8 channelnum,
-       SharedBuffer<u8> data, bool ack)
+void ConnectionSendThread::sendAsPacket(session_t peer_id, u8 channelnum,
+       const SharedBuffer<u8> &data, bool ack)
 {
        OutgoingPacket packet(peer_id, channelnum, data, false, ack);
        m_outgoing_queue.push(packet);
@@ -835,9 +823,9 @@ void *ConnectionReceiveThread::run()
                if (debug_print_timer > 20.0) {
                        debug_print_timer -= 20.0;
 
-                       std::list<u16> peerids = m_connection->getPeerIDs();
+                       std::list<session_t> peerids = m_connection->getPeerIDs();
 
-                       for (std::list<u16>::iterator i = peerids.begin();
+                       for (std::list<session_t>::iterator i = peerids.begin();
                                        i != peerids.end();
                                        i++)
                        {
@@ -910,7 +898,7 @@ void ConnectionReceiveThread::receive()
                try {
                        if (packet_queued) {
                                bool data_left = true;
-                               u16 peer_id;
+                               session_t peer_id;
                                SharedBuffer<u8> resultdata;
                                while (data_left) {
                                        try {
@@ -943,12 +931,12 @@ void ConnectionReceiveThread::receive()
                                continue;
                        }
 
-                       u16 peer_id = readPeerId(*packetdata);
+                       session_t peer_id = readPeerId(*packetdata);
                        u8 channelnum = readChannel(*packetdata);
 
                        if (channelnum > CHANNEL_COUNT - 1) {
                                LOG(derr_con << m_connection->getDesc()
-                                       << "Receive(): Invalid channel " << channelnum << std::endl);
+                                       << "Receive(): Invalid channel " << (u32)channelnum << std::endl);
                                throw InvalidIncomingDataException("Channel doesn't exist");
                        }
 
@@ -1024,7 +1012,7 @@ void ConnectionReceiveThread::receive()
 
                                LOG(dout_con << m_connection->getDesc()
                                        << " ProcessPacket from peer_id: " << peer_id
-                                       << ",channel: " << (channelnum & 0xFF) << ", returned "
+                                       << ", channel: " << (u32)channelnum << ", returned "
                                        << resultdata.getSize() << " bytes" << std::endl);
 
                                ConnectionEvent e;
@@ -1044,11 +1032,11 @@ void ConnectionReceiveThread::receive()
        }
 }
 
-bool ConnectionReceiveThread::getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst)
+bool ConnectionReceiveThread::getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst)
 {
-       std::list<u16> peerids = m_connection->getPeerIDs();
+       std::list<session_t> peerids = m_connection->getPeerIDs();
 
-       for (u16 peerid : peerids) {
+       for (session_t peerid : peerids) {
                PeerHelper peer = m_connection->getPeerNoEx(peerid);
                if (!peer)
                        continue;
@@ -1066,7 +1054,7 @@ bool ConnectionReceiveThread::getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst
 }
 
 bool ConnectionReceiveThread::checkIncomingBuffers(Channel *channel,
-       u16 &peer_id, SharedBuffer<u8> &dst)
+       session_t &peer_id, SharedBuffer<u8> &dst)
 {
        u16 firstseqnum = 0;
        if (channel->incoming_reliables.getFirstSeqnum(firstseqnum)) {
@@ -1098,7 +1086,7 @@ bool ConnectionReceiveThread::checkIncomingBuffers(Channel *channel,
 }
 
 SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
-       SharedBuffer<u8> packetdata, u16 peer_id, u8 channelnum, bool reliable)
+       const SharedBuffer<u8> &packetdata, session_t peer_id, u8 channelnum, bool reliable)
 {
        PeerHelper peer = m_connection->getPeerNoEx(peer_id);
 
@@ -1137,7 +1125,7 @@ const ConnectionReceiveThread::PacketTypeHandler
 };
 
 SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *channel,
-       SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum, bool reliable)
+       const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum, bool reliable)
 {
        if (packetdata.getSize() < 2)
                throw InvalidIncomingDataException("packetdata.getSize() < 2");
@@ -1197,7 +1185,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
                if (packetdata.getSize() < 4)
                        throw InvalidIncomingDataException
                                ("packetdata.getSize() < 4 (SET_PEER_ID header size)");
-               u16 peer_id_new = readU16(&packetdata[2]);
+               session_t peer_id_new = readU16(&packetdata[2]);
                LOG(dout_con << m_connection->getDesc() << "Got new peer id: " << peer_id_new
                        << "... " << std::endl);
 
@@ -1210,14 +1198,6 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
                        m_connection->SetPeerID(peer_id_new);
                }
 
-               ConnectionCommand cmd;
-
-               SharedBuffer<u8> reply(2);
-               writeU8(&reply[0], PACKET_TYPE_CONTROL);
-               writeU8(&reply[1], CONTROLTYPE_ENABLE_BIG_SEND_WINDOW);
-               cmd.disableLegacy(PEER_ID_SERVER, reply);
-               m_connection->putCommand(cmd);
-
                throw ProcessedSilentlyException("Got a SET_PEER_ID");
        } else if (controltype == CONTROLTYPE_PING) {
                // Just ignore it, the incoming data already reset
@@ -1235,9 +1215,6 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
                }
 
                throw ProcessedSilentlyException("Got a DISCO");
-       } else if (controltype == CONTROLTYPE_ENABLE_BIG_SEND_WINDOW) {
-               dynamic_cast<UDPPeer *>(peer)->setNonLegacyPeer();
-               throw ProcessedSilentlyException("Got non legacy control");
        } else {
                LOG(derr_con << m_connection->getDesc()
                        << "INVALID TYPE_CONTROL: invalid controltype="
@@ -1247,7 +1224,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
 }
 
 SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Original(Channel *channel,
-       SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum, bool reliable)
+       const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum, bool reliable)
 {
        if (packetdata.getSize() <= ORIGINAL_HEADER_SIZE)
                throw InvalidIncomingDataException
@@ -1261,7 +1238,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Original(Channel *cha
 }
 
 SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Split(Channel *channel,
-       SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum, bool reliable)
+       const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum, bool reliable)
 {
        Address peer_address;
 
@@ -1292,7 +1269,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Split(Channel *channe
 }
 
 SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Reliable(Channel *channel,
-       SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum, bool reliable)
+       const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum, bool reliable)
 {
        assert(channel != NULL);