Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace std::list<session_t> in networking code (#10215)
  • Loading branch information
sfan5 committed Jul 23, 2020
1 parent 76afde8 commit 8ca6021
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/network/connection.cpp
Expand Up @@ -1269,7 +1269,8 @@ bool Connection::deletePeer(session_t peer_id, bool timeout)
return false;
peer = m_peers[peer_id];
m_peers.erase(peer_id);
m_peer_ids.remove(peer_id);
auto it = std::find(m_peer_ids.begin(), m_peer_ids.end(), peer_id);
m_peer_ids.erase(it);
}

Address peer_address;
Expand Down
6 changes: 3 additions & 3 deletions src/network/connection.h
Expand Up @@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "networkprotocol.h"
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
#include <map>

class NetworkPacket;
Expand Down Expand Up @@ -795,7 +795,7 @@ class Connection

void PrintInfo(std::ostream &out);

std::list<session_t> getPeerIDs()
std::vector<session_t> getPeerIDs()
{
MutexAutoLock peerlock(m_peers_mutex);
return m_peer_ids;
Expand All @@ -816,7 +816,7 @@ class Connection
u32 m_protocol_id;

std::map<session_t, Peer *> m_peers;
std::list<session_t> m_peer_ids;
std::vector<session_t> m_peer_ids;
std::mutex m_peers_mutex;

std::unique_ptr<ConnectionSendThread> m_sendThread;
Expand Down
26 changes: 12 additions & 14 deletions src/network/connectionthreads.cpp
Expand Up @@ -144,7 +144,7 @@ void ConnectionSendThread::Trigger()

bool ConnectionSendThread::packetsQueued()
{
std::list<session_t> peerIds = m_connection->getPeerIDs();
std::vector<session_t> peerIds = m_connection->getPeerIDs();

if (!m_outgoing_queue.empty() && !peerIds.empty())
return true;
Expand All @@ -171,8 +171,8 @@ bool ConnectionSendThread::packetsQueued()

void ConnectionSendThread::runTimeouts(float dtime)
{
std::list<session_t> timeouted_peers;
std::list<session_t> peerIds = m_connection->getPeerIDs();
std::vector<session_t> timeouted_peers;
std::vector<session_t> peerIds = m_connection->getPeerIDs();

for (session_t &peerId : peerIds) {
PeerHelper peer = m_connection->getPeerNoEx(peerId);
Expand Down Expand Up @@ -548,7 +548,7 @@ void ConnectionSendThread::disconnect()


// Send to all
std::list<session_t> peerids = m_connection->getPeerIDs();
std::vector<session_t> peerids = m_connection->getPeerIDs();

for (session_t peerid : peerids) {
sendAsPacket(peerid, 0, data, false);
Expand Down Expand Up @@ -620,7 +620,7 @@ void ConnectionSendThread::sendReliable(ConnectionCommand &c)

void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer<u8> &data)
{
std::list<session_t> peerids = m_connection->getPeerIDs();
std::vector<session_t> peerids = m_connection->getPeerIDs();

for (session_t peerid : peerids) {
send(peerid, channelnum, data);
Expand All @@ -629,7 +629,7 @@ void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer<u8> &data

void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)
{
std::list<session_t> peerids = m_connection->getPeerIDs();
std::vector<session_t> peerids = m_connection->getPeerIDs();

for (session_t peerid : peerids) {
PeerHelper peer = m_connection->getPeerNoEx(peerid);
Expand All @@ -643,8 +643,8 @@ void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)

void ConnectionSendThread::sendPackets(float dtime)
{
std::list<session_t> peerIds = m_connection->getPeerIDs();
std::list<session_t> pendingDisconnect;
std::vector<session_t> peerIds = m_connection->getPeerIDs();
std::vector<session_t> pendingDisconnect;
std::map<session_t, bool> pending_unreliable;

const unsigned int peer_packet_quota = m_iteration_packets_avaialble
Expand Down Expand Up @@ -843,13 +843,11 @@ void *ConnectionReceiveThread::run()
if (debug_print_timer > 20.0) {
debug_print_timer -= 20.0;

std::list<session_t> peerids = m_connection->getPeerIDs();
std::vector<session_t> peerids = m_connection->getPeerIDs();

for (std::list<session_t>::iterator i = peerids.begin();
i != peerids.end();
i++)
for (auto id : peerids)
{
PeerHelper peer = m_connection->getPeerNoEx(*i);
PeerHelper peer = m_connection->getPeerNoEx(id);
if (!peer)
continue;

Expand Down Expand Up @@ -1039,7 +1037,7 @@ void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,

bool ConnectionReceiveThread::getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst)
{
std::list<session_t> peerids = m_connection->getPeerIDs();
std::vector<session_t> peerids = m_connection->getPeerIDs();

for (session_t peerid : peerids) {
PeerHelper peer = m_connection->getPeerNoEx(peerid);
Expand Down

0 comments on commit 8ca6021

Please sign in to comment.