Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetHost.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "NetHost.h"
21 
23 #include "network/NetMessage.h"
24 #include "ps/CLogger.h"
25 
26 bool CNetHost::SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName)
27 {
28  ENetPacket* packet = CreatePacket(message);
29  if (!packet)
30  return false;
31 
32  LOGMESSAGE(L"Net: Sending message %hs of size %lu to %hs", message->ToString().c_str(), (unsigned long)packet->dataLength, peerName);
33 
34  // Let ENet send the message to peer
35  if (enet_peer_send(peer, DEFAULT_CHANNEL, packet) < 0)
36  {
37  LOGERROR(L"Net: Failed to send packet to peer");
38  return false;
39  }
40 
41  // Don't call enet_host_flush now - let it queue up all the packets
42  // and send them during the next frame
43  //
44  // TODO: we should flush explicitly at some appropriate point before the next frame
45 
46  return true;
47 }
48 
50 {
51  size_t size = message->GetSerializedLength();
52 
53  ENSURE(size); // else we'll fail when accessing the 0th element
54 
55  // Adjust buffer for message
56  std::vector<u8> buffer;
57  buffer.resize(size);
58 
59  // Save message to internal buffer
60  message->Serialize(&buffer[0]);
61 
62  // Create a reliable packet
63  ENetPacket* packet = enet_packet_create(&buffer[0], size, ENET_PACKET_FLAG_RELIABLE);
64  if (!packet)
65  LOGERROR(L"Net: Failed to construct packet");
66 
67  return packet;
68 }
69 
71 {
72  int ret = enet_initialize();
73  ENSURE(ret == 0);
74 }
75 
77 {
78  enet_deinitialize();
79 }
#define LOGERROR
Definition: CLogger.h:35
#define LOGMESSAGE
Definition: CLogger.h:32
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
static bool SendMessage(const CNetMessage *message, ENetPeer *peer, const char *peerName)
Transmit a message to the given peer.
Definition: NetHost.cpp:26
static void Initialize()
Initialize ENet.
Definition: NetHost.cpp:70
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessage.cpp:44
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
static ENetPacket * CreatePacket(const CNetMessage *message)
Construct an ENet packet by serialising the given message.
Definition: NetHost.cpp:49
Various declarations shared by networking code.
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
struct _ENetPacket ENetPacket
Definition: NetHost.h:31
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
static const int DEFAULT_CHANNEL
Definition: NetHost.h:68
static void Deinitialize()
Deinitialize ENet.
Definition: NetHost.cpp:76
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessage.cpp:84