1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#ifdef _MSC_VER
# include "StdAfx.h"
#elif defined(_WIN32)
# include <windows.h>
#endif
#include "UDPListener.h"
#include <boost/weak_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <list>
#include <queue>
#include "mmgr.h"
#include "ProtocolDef.h"
#include "LogOutput.h"
#include "UDPConnection.h"
#include "Socket.h"
namespace netcode
{
using namespace boost::asio;
UDPListener::UDPListener(int port)
{
SocketPtr temp(new ip::udp::socket(netservice));
boost::system::error_code err;
temp->open(ip::udp::v6(), err); // test v6
if (!err)
{
temp->bind(ip::udp::endpoint(ip::address_v6::any(), port));
}
else
{
// fallback to v4
temp->open(ip::udp::v4());
temp->bind(ip::udp::endpoint(ip::address_v4::any(), port));
}
boost::asio::socket_base::non_blocking_io command(true);
temp->io_control(command);
mySocket = temp;
acceptNewConnections = true;
}
UDPListener::~UDPListener()
{
}
void UDPListener::Update()
{
netservice.poll();
for (std::list< boost::weak_ptr< UDPConnection> >::iterator i = conn.begin(); i != conn.end(); )
{
if (i->expired())
i = conn.erase(i);
else
++i;
}
size_t bytes_avail = 0;
while ((bytes_avail = mySocket->available()) > 0)
{
std::vector<uint8_t> buffer(bytes_avail);
ip::udp::endpoint sender_endpoint;
boost::asio::ip::udp::socket::message_flags flags = 0;
boost::system::error_code err;
size_t bytesReceived = mySocket->receive_from(boost::asio::buffer(buffer), sender_endpoint, flags, err);
if (CheckErrorCode(err))
break;
if (bytesReceived < Packet::headerSize)
continue;
Packet data(&buffer[0], bytesReceived);
bool processed = false;
for (std::list< boost::weak_ptr<UDPConnection> >::iterator i = conn.begin(); i != conn.end(); ++i)
{
boost::shared_ptr<UDPConnection> locked(*i);
if (locked->CheckAddress(sender_endpoint))
{
locked->ProcessRawPacket(data);
processed = true;
break;
}
}
if (!processed) // still have the packet (means no connection with the sender's address found)
{
if (acceptNewConnections && data.lastContinuous == -1 && data.nakType == 0)
{
if (!data.chunks.empty() && (*data.chunks.begin())->chunkNumber == 0)
{
// new client wants to connect
boost::shared_ptr<UDPConnection> incoming(new UDPConnection(mySocket, sender_endpoint));
waiting.push(incoming);
conn.push_back(incoming);
incoming->ProcessRawPacket(data);
}
}
else
{
LogObject() << "Dropping packet from unknown IP: [" << sender_endpoint.address() << "]:" << sender_endpoint.port();
}
}
}
for (std::list< boost::weak_ptr< UDPConnection> >::iterator i = conn.begin(); i != conn.end(); ++i)
{
boost::shared_ptr<UDPConnection> temp = i->lock();
temp->Update();
}
}
boost::shared_ptr<UDPConnection> UDPListener::SpawnConnection(const std::string& address, const unsigned port)
{
boost::shared_ptr<UDPConnection> temp(new UDPConnection(mySocket, ip::udp::endpoint(ip::address_v4::from_string(address), port)));
conn.push_back(temp);
return temp;
}
bool UDPListener::Listen(const bool state)
{
acceptNewConnections = state;
return acceptNewConnections;
}
bool UDPListener::Listen() const
{
return acceptNewConnections;
}
bool UDPListener::HasIncomingConnections() const
{
return !waiting.empty();
}
boost::weak_ptr<UDPConnection> UDPListener::PreviewConnection()
{
return waiting.front();
}
boost::shared_ptr<UDPConnection> UDPListener::AcceptConnection()
{
boost::shared_ptr<UDPConnection> newConn = waiting.front();
waiting.pop();
conn.push_back(newConn);
return newConn;
}
void UDPListener::RejectConnection()
{
waiting.pop();
}
}
|