X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fconnection.cpp;h=b07e0de906124fad5c9400f7070e31f44640b000;hb=6edb8c567cdc4f037452e43beb0545e6c538902b;hp=42bfdfb9f24e77621e5f50e5355f4eecb64e65b1;hpb=4e249fb3fbf75f0359758760d88e22aa5b14533c;p=minetest.git diff --git a/src/connection.cpp b/src/connection.cpp index 42bfdfb9f..b07e0de90 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -1,3 +1,22 @@ +/* +Minetest-c55 +Copyright (C) 2010 celeron55, Perttu Ahola + +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 +(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. + +You should have received a copy of the GNU 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. +*/ + #include "connection.h" #include "main.h" #include "serialization.h" @@ -426,6 +445,7 @@ Peer::Peer(u16 a_id, Address a_address) address = a_address; timeout_counter = 0.0; //resend_timeout = RESEND_TIMEOUT_MINIMUM; + ping_timer = 0.0; resend_timeout = 0.5; avg_rtt = -1.0; has_sent_with_id = false; @@ -477,7 +497,7 @@ Connection::Connection( m_protocol_id = protocol_id; m_max_packet_size = max_packet_size; m_timeout = timeout; - m_peer_id = PEER_ID_NEW; + m_peer_id = PEER_ID_INEXISTENT; //m_waiting_new_peer_id = false; m_indentation = 0; m_peerhandler = peerhandler; @@ -514,14 +534,31 @@ void Connection::Connect(Address address) m_socket.Bind(0); - // Send a dummy packet to server with peer_id = PEER_ID_NEW - m_peer_id = PEER_ID_NEW; + // Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT + m_peer_id = PEER_ID_INEXISTENT; SharedBuffer data(0); Send(PEER_ID_SERVER, 0, data, true); //m_waiting_new_peer_id = true; } +void Connection::Disconnect() +{ + // Create and send DISCO packet + SharedBuffer data(2); + writeU8(&data[0], TYPE_CONTROL); + writeU8(&data[1], CONTROLTYPE_DISCO); + + // Send to all + core::map::Iterator j; + j = m_peers.getIterator(); + for(; j.atEnd() == false; j++) + { + Peer *peer = j.getNode()->getValue(); + SendAsPacket(peer->id, 0, data, false); + } +} + bool Connection::Connected() { if(m_peers.size() != 1) @@ -531,7 +568,7 @@ bool Connection::Connected() if(node == NULL) return false; - if(m_peer_id == PEER_ID_NEW) + if(m_peer_id == PEER_ID_INEXISTENT) return false; return true; @@ -606,7 +643,7 @@ SharedBuffer Channel::ProcessPacket( con->PrintInfo(); dout_con<<"Got new peer id: "<GetPeerID() != PEER_ID_NEW) + if(con->GetPeerID() != PEER_ID_INEXISTENT) { con->PrintInfo(derr_con); derr_con<<"WARNING: Not changing" @@ -625,7 +662,22 @@ SharedBuffer Channel::ProcessPacket( // the timeout counter con->PrintInfo(); dout_con<<"PING"<PrintInfo(); + dout_con<<"DISCO: Removing peer "<<(peer_id)<deletePeer(peer_id, false) == false) + { + con->PrintInfo(derr_con); + derr_con<<"DISCO: Peer not found"<PrintInfo(derr_con); @@ -899,7 +951,7 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize) throw InvalidIncomingDataException("Channel doesn't exist"); } - if(peer_id == PEER_ID_NEW) + if(peer_id == PEER_ID_INEXISTENT) { /* Somebody is trying to send stuff to us with no peer id. @@ -942,7 +994,7 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize) /* The peer was not found in our lists. Add it. */ - if(peer_id == PEER_ID_NEW) + if(peer_id == PEER_ID_INEXISTENT) { // Somebody wants to make a new connection @@ -964,7 +1016,7 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize) } PrintInfo(); - dout_con<<"Receive(): Got a packet with peer_id=PEER_ID_NEW," + dout_con<<"Receive(): Got a packet with peer_id=PEER_ID_INEXISTENT," " giving peer_id="<::Iterator i = timeouted_peers.begin(); for(; i != timeouted_peers.end(); i++) { PrintInfo(derr_con); derr_con<<"RunTimeouts(): Removing peer "<<(*i)<deletingPeer(m_peers[*i], true); - delete m_peers[*i]; - m_peers.remove(*i); + deletePeer(*i, true); } } @@ -1267,7 +1317,7 @@ Peer* Connection::GetPeer(u16 peer_id) if(node == NULL){ // Peer not found - throw PeerNotFoundException("Peer not found (possible timeout)"); + throw PeerNotFoundException("GetPeer: Peer not found (possible timeout)"); } // Error checking @@ -1303,6 +1353,16 @@ core::list Connection::GetPeers() return list; } +bool Connection::deletePeer(u16 peer_id, bool timeout) +{ + if(m_peers.find(peer_id) == NULL) + return false; + m_peerhandler->deletingPeer(m_peers[peer_id], timeout); + delete m_peers[peer_id]; + m_peers.remove(peer_id); + return true; +} + void Connection::PrintInfo(std::ostream &out) { out<