2010-06-08 00:19:05 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-06-08 00:19:05 +02:00
|
|
|
#include "NetHost.h"
|
|
|
|
|
2010-08-10 23:49:33 +02:00
|
|
|
#include "lib/external_libraries/enet.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "network/NetMessage.h"
|
2010-06-08 00:19:05 +02:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
bool CNetHost::SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName)
|
2010-06-08 00:19:05 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
ENetPacket* packet = CreatePacket(message);
|
|
|
|
if (!packet)
|
2010-06-08 00:19:05 +02:00
|
|
|
return false;
|
|
|
|
|
2010-07-04 18:52:14 +02:00
|
|
|
LOGMESSAGE(L"Net: Sending message %hs of size %lu to %hs", message->ToString().c_str(), (unsigned long)packet->dataLength, peerName);
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Let ENet send the message to peer
|
|
|
|
if (enet_peer_send(peer, DEFAULT_CHANNEL, packet) < 0)
|
2010-06-08 00:19:05 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
LOGERROR(L"Net: Failed to send packet to peer");
|
2010-06-08 00:19:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
// Don't call enet_host_flush now - let it queue up all the packets
|
|
|
|
// and send them during the next frame
|
|
|
|
//
|
|
|
|
// TODO: we should flush explicitly at some appropriate point before the next frame
|
2010-06-08 00:19:05 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
ENetPacket* CNetHost::CreatePacket(const CNetMessage* message)
|
2010-06-08 00:19:05 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
size_t size = message->GetSerializedLength();
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(size); // else we'll fail when accessing the 0th element
|
2010-06-08 00:19:05 +02:00
|
|
|
|
|
|
|
// Adjust buffer for message
|
|
|
|
std::vector<u8> buffer;
|
|
|
|
buffer.resize(size);
|
|
|
|
|
|
|
|
// Save message to internal buffer
|
2010-06-30 23:41:04 +02:00
|
|
|
message->Serialize(&buffer[0]);
|
2010-06-08 00:19:05 +02:00
|
|
|
|
|
|
|
// Create a reliable packet
|
2010-06-30 23:41:04 +02:00
|
|
|
ENetPacket* packet = enet_packet_create(&buffer[0], size, ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
if (!packet)
|
|
|
|
LOGERROR(L"Net: Failed to construct packet");
|
2010-06-08 00:19:05 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
return packet;
|
2010-06-08 00:19:05 +02:00
|
|
|
}
|
2010-07-03 21:31:14 +02:00
|
|
|
|
|
|
|
void CNetHost::Initialize()
|
|
|
|
{
|
|
|
|
int ret = enet_initialize();
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ret == 0);
|
2010-07-03 21:31:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNetHost::Deinitialize()
|
|
|
|
{
|
|
|
|
enet_deinitialize();
|
2010-08-10 23:49:33 +02:00
|
|
|
}
|