Tune caves
[oweals/minetest.git] / src / connection.cpp
index d941f368a568d71ddf6f50a871bf4824a0ecd0dc..127cabdc51ead787e586cb7069b662fca840370c 100644 (file)
@@ -463,7 +463,7 @@ void Peer::reportRTT(float rtt)
 {
        if(rtt >= 0.0){
                if(rtt < 0.01){
-                       if(m_max_packets_per_second < 100)
+                       if(m_max_packets_per_second < 400)
                                m_max_packets_per_second += 10;
                } else if(rtt < 0.2){
                        if(m_max_packets_per_second < 100)
@@ -537,6 +537,14 @@ Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
 Connection::~Connection()
 {
        stop();
+       // Delete peers
+       for(core::map<u16, Peer*>::Iterator
+                       j = m_peers.getIterator();
+                       j.atEnd() == false; j++)
+       {
+               Peer *peer = j.getNode()->getValue();
+               delete peer;
+       }
 }
 
 /* Internal stuff */
@@ -666,11 +674,11 @@ void Connection::send(float dtime)
 // Receive packets from the network and buffers and create ConnectionEvents
 void Connection::receive()
 {
-       u32 datasize = 100000;
+       u32 datasize = m_max_packet_size * 2;  // Double it just to be safe
        // TODO: We can not know how many layers of header there are.
        // For now, just assume there are no other than the base headers.
        u32 packet_maxsize = datasize + BASE_HEADER_SIZE;
-       Buffer<u8> packetdata(packet_maxsize);
+       SharedBuffer<u8> packetdata(packet_maxsize);
 
        bool single_wait_done = false;
        
@@ -854,10 +862,6 @@ void Connection::receive()
                        dout_con<<"ProcessPacket returned data of size "
                                        <<resultdata.getSize()<<std::endl;
                        
-                       if(datasize < resultdata.getSize())
-                               throw InvalidIncomingDataException
-                                               ("Buffer too small for received data");
-                       
                        ConnectionEvent e;
                        e.dataReceived(peer_id, resultdata);
                        putEvent(e);
@@ -990,8 +994,16 @@ nextpeer:
 void Connection::serve(u16 port)
 {
        dout_con<<getDesc()<<" serving at port "<<port<<std::endl;
-       m_socket.Bind(port);
-       m_peer_id = PEER_ID_SERVER;
+       try{
+               m_socket.Bind(port);
+               m_peer_id = PEER_ID_SERVER;
+       }
+       catch(SocketException &e){
+               // Create event
+               ConnectionEvent ce;
+               ce.bindFailed();
+               putEvent(ce);
+       }
 }
 
 void Connection::connect(Address address)
@@ -1527,7 +1539,7 @@ ConnectionEvent Connection::waitEvent(u32 timeout_ms)
 {
        try{
                return m_event_queue.pop_front(timeout_ms);
-       } catch(ItemNotFoundException &e){
+       } catch(ItemNotFoundException &ex){
                ConnectionEvent e;
                e.type = CONNEVENT_NONE;
                return e;
@@ -1577,7 +1589,7 @@ void Connection::Disconnect()
        putCommand(c);
 }
 
-u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize)
+u32 Connection::Receive(u16 &peer_id, SharedBuffer<u8> &data)
 {
        for(;;){
                ConnectionEvent e = waitEvent(m_bc_receive_timeout);
@@ -1589,7 +1601,7 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize)
                        throw NoIncomingDataException("No incoming data");
                case CONNEVENT_DATA_RECEIVED:
                        peer_id = e.peer_id;
-                       memcpy(data, *e.data, e.data.getSize());
+                       data = SharedBuffer<u8>(e.data);
                        return e.data.getSize();
                case CONNEVENT_PEER_ADDED: {
                        Peer tmp(e.peer_id, e.address);
@@ -1601,6 +1613,9 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize)
                        if(m_bc_peerhandler)
                                m_bc_peerhandler->deletingPeer(&tmp, e.timeout);
                        continue; }
+               case CONNEVENT_BIND_FAILED:
+                       throw ConnectionBindFailed("Failed to bind socket "
+                                       "(port already in use?)");
                }
        }
        throw NoIncomingDataException("No incoming data");