1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-12-23 08:08:47 -05:00

[Patch 2/4] Network rework: packet writing, sending and cleanups

NetworkPacket.cpp:
* Remove some deprecated functions, we must use streaming interface
* m_data converted from u8* to std::vector<u8>
* Add an exporter to forge packet to Connection object
* implement operator << std::wstring. n
* implement operator << std::string
* dynamic resize when write packet content.
* fix string writing and performances.
* create ServerCommandFactory, used by client to get useful informations about packet processing (sending).
  * Reliability
  * Transmit channel
* Implement putRawString for some ugly char (_INIT packet), and use it.
* Many packet read and write migrated
* Implement oldForgePacket to interface writing with current connection
* fix U8/char/bool writing
* fix string writing and performances.
* add some missing functions
* Use v3s16 read instead of reading x,y,z separately
* Add irr::video::SColor support into packets
* Add some missing handlers
* Add a template function to increase offset
* Throw a serialization error on packet reading (must be improved)

PacketFactories:
* Create ServerCommandFactory, used by client to get useful informations about packet processing (sending).
* Create ClientCommandFactory, used by server to get useful informations about packet processing (sending).

Client.cpp:
* implement NetworkPacket ::Send interface.
* Move packet handlers to a dedicated file
* Remove Client::Send(SharedBuffer)

Server.cpp:
* implement NetworkPacket ::Send interface.
* Rewrite all packets using NetworkPacket
* Move packet handlers to a dedicated file
* Remove Server::Send(SharedBuffer)

ClientIface.cpp:
* Remove sendToAll(SharedBuffer<u8>)

Connection.hpp rework:
* Remove duplicate include
* Remove duplicate negation
* Remove a useless variable
* Improve code performance by using a m_peers_list instead of scanning m_peers map
* Remove Connection::Send(SharedBuffer)
* Fix useafterfree into NetworkPacket Sending
* Remove unused Connection::sendToAll

Test.cpp:
* Remove dead code
* Update tests to use NetworkPackets

Misc:
* add new wrappers to Send packets in client, using NetworkPacket
* Add NetworkPacket methods for Connection
* coding style fix
* dead code since changes cleanup
* Use v3s16 read instead of reading x,y,z separately in some packets
* Use different files to handle packets received by client and server
* Cleanup: Remove useless includes

ok @Zeno-
Tested by @Zeno- @VanessaE and @nerzhul on running servers
This commit is contained in:
Loic Blot 2015-01-16 11:37:49 +01:00
parent efa977518a
commit ed04e8e9e4
22 changed files with 3921 additions and 4182 deletions

View File

@ -361,7 +361,6 @@ set(common_SRCS
cavegen.cpp
clientiface.cpp
collision.cpp
connection.cpp
content_abm.cpp
content_mapnode.cpp
content_nodemeta.cpp
@ -429,9 +428,10 @@ set(common_SRCS
version.cpp
voxel.cpp
voxelalgorithms.cpp
network/connection.cpp
network/networkpacket.cpp
network/packethandlers/server.cpp
network/serveropcodes.cpp
network/toserverpacket.cpp
${JTHREAD_SRCS}
${common_SCRIPT_SRCS}
${UTIL_SRCS}
@ -496,7 +496,7 @@ set(minetest_SRCS
wieldmesh.cpp
client/clientlauncher.cpp
network/clientopcodes.cpp
network/toclientpacket.cpp
network/packethandlers/client.cpp
${minetest_SCRIPT_SRCS}
)
list(SORT minetest_SRCS)

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef CLIENT_HEADER
#define CLIENT_HEADER
#include "connection.h"
#include "network/connection.h"
#include "environment.h"
#include "irrlichttypes_extrabloated.h"
#include "jthread/jmutex.h"
@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "localplayer.h"
#include "hud.h"
#include "particles.h"
#include "network/toclientpacket.h"
#include "network/networkpacket.h"
struct MeshMakeData;
class MapBlockMesh;
@ -346,57 +346,57 @@ public:
* Command Handlers
*/
void handleCommand(ToClientPacket* pkt);
void handleCommand(NetworkPacket* pkt);
void handleCommand_Null(ToClientPacket* pkt) {};
void handleCommand_Deprecated(ToClientPacket* pkt);
void handleCommand_Init(ToClientPacket* pkt);
void handleCommand_AccessDenied(ToClientPacket* pkt);
void handleCommand_RemoveNode(ToClientPacket* pkt);
void handleCommand_AddNode(ToClientPacket* pkt);
void handleCommand_BlockData(ToClientPacket* pkt);
void handleCommand_Inventory(ToClientPacket* pkt);
void handleCommand_TimeOfDay(ToClientPacket* pkt);
void handleCommand_ChatMessage(ToClientPacket* pkt);
void handleCommand_ActiveObjectRemoveAdd(ToClientPacket* pkt);
void handleCommand_ActiveObjectMessages(ToClientPacket* pkt);
void handleCommand_Movement(ToClientPacket* pkt);
void handleCommand_HP(ToClientPacket* pkt);
void handleCommand_Breath(ToClientPacket* pkt);
void handleCommand_MovePlayer(ToClientPacket* pkt);
void handleCommand_PlayerItem(ToClientPacket* pkt);
void handleCommand_DeathScreen(ToClientPacket* pkt);
void handleCommand_AnnounceMedia(ToClientPacket* pkt);
void handleCommand_Media(ToClientPacket* pkt);
void handleCommand_ToolDef(ToClientPacket* pkt);
void handleCommand_NodeDef(ToClientPacket* pkt);
void handleCommand_CraftItemDef(ToClientPacket* pkt);
void handleCommand_ItemDef(ToClientPacket* pkt);
void handleCommand_PlaySound(ToClientPacket* pkt);
void handleCommand_StopSound(ToClientPacket* pkt);
void handleCommand_Privileges(ToClientPacket* pkt);
void handleCommand_InventoryFormSpec(ToClientPacket* pkt);
void handleCommand_DetachedInventory(ToClientPacket* pkt);
void handleCommand_ShowFormSpec(ToClientPacket* pkt);
void handleCommand_SpawnParticle(ToClientPacket* pkt);
void handleCommand_AddParticleSpawner(ToClientPacket* pkt);
void handleCommand_DeleteParticleSpawner(ToClientPacket* pkt);
void handleCommand_HudAdd(ToClientPacket* pkt);
void handleCommand_HudRemove(ToClientPacket* pkt);
void handleCommand_HudChange(ToClientPacket* pkt);
void handleCommand_HudSetFlags(ToClientPacket* pkt);
void handleCommand_HudSetParam(ToClientPacket* pkt);
void handleCommand_HudSetSky(ToClientPacket* pkt);
void handleCommand_OverrideDayNightRatio(ToClientPacket* pkt);
void handleCommand_LocalPlayerAnimations(ToClientPacket* pkt);
void handleCommand_EyeOffset(ToClientPacket* pkt);
void handleCommand_Null(NetworkPacket* pkt) {};
void handleCommand_Deprecated(NetworkPacket* pkt);
void handleCommand_Init(NetworkPacket* pkt);
void handleCommand_AccessDenied(NetworkPacket* pkt);
void handleCommand_RemoveNode(NetworkPacket* pkt);
void handleCommand_AddNode(NetworkPacket* pkt);
void handleCommand_BlockData(NetworkPacket* pkt);
void handleCommand_Inventory(NetworkPacket* pkt);
void handleCommand_TimeOfDay(NetworkPacket* pkt);
void handleCommand_ChatMessage(NetworkPacket* pkt);
void handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt);
void handleCommand_ActiveObjectMessages(NetworkPacket* pkt);
void handleCommand_Movement(NetworkPacket* pkt);
void handleCommand_HP(NetworkPacket* pkt);
void handleCommand_Breath(NetworkPacket* pkt);
void handleCommand_MovePlayer(NetworkPacket* pkt);
void handleCommand_PlayerItem(NetworkPacket* pkt);
void handleCommand_DeathScreen(NetworkPacket* pkt);
void handleCommand_AnnounceMedia(NetworkPacket* pkt);
void handleCommand_Media(NetworkPacket* pkt);
void handleCommand_ToolDef(NetworkPacket* pkt);
void handleCommand_NodeDef(NetworkPacket* pkt);
void handleCommand_CraftItemDef(NetworkPacket* pkt);
void handleCommand_ItemDef(NetworkPacket* pkt);
void handleCommand_PlaySound(NetworkPacket* pkt);
void handleCommand_StopSound(NetworkPacket* pkt);
void handleCommand_Privileges(NetworkPacket* pkt);
void handleCommand_InventoryFormSpec(NetworkPacket* pkt);
void handleCommand_DetachedInventory(NetworkPacket* pkt);
void handleCommand_ShowFormSpec(NetworkPacket* pkt);
void handleCommand_SpawnParticle(NetworkPacket* pkt);
void handleCommand_AddParticleSpawner(NetworkPacket* pkt);
void handleCommand_DeleteParticleSpawner(NetworkPacket* pkt);
void handleCommand_HudAdd(NetworkPacket* pkt);
void handleCommand_HudRemove(NetworkPacket* pkt);
void handleCommand_HudChange(NetworkPacket* pkt);
void handleCommand_HudSetFlags(NetworkPacket* pkt);
void handleCommand_HudSetParam(NetworkPacket* pkt);
void handleCommand_HudSetSky(NetworkPacket* pkt);
void handleCommand_OverrideDayNightRatio(NetworkPacket* pkt);
void handleCommand_LocalPlayerAnimations(NetworkPacket* pkt);
void handleCommand_EyeOffset(NetworkPacket* pkt);
void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
// Returns true if something was received
bool AsyncProcessPacket();
bool AsyncProcessData();
void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
void Send(NetworkPacket* pkt);
void interact(u8 action, const PointedThing& pointed);

View File

@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
#include "settings.h"
#include "mapblock.h"
#include "connection.h"
#include "network/connection.h"
#include "environment.h"
#include "map.h"
#include "emerge.h"
@ -626,26 +626,29 @@ void ClientInterface::UpdatePlayerList()
}
void ClientInterface::send(u16 peer_id, u8 channelnum,
SharedBuffer<u8> data, bool reliable)
NetworkPacket* pkt, bool reliable, bool deletepkt)
{
m_con->Send(peer_id, channelnum, data, reliable);
m_con->Send(peer_id, channelnum, pkt, reliable);
if (deletepkt)
delete pkt;
}
void ClientInterface::sendToAll(u16 channelnum,
SharedBuffer<u8> data, bool reliable)
NetworkPacket* pkt, bool reliable)
{
JMutexAutoLock clientslock(m_clients_mutex);
for(std::map<u16, RemoteClient*>::iterator
i = m_clients.begin();
i != m_clients.end(); ++i)
{
i != m_clients.end(); ++i) {
RemoteClient *client = i->second;
if (client->net_proto_version != 0)
{
m_con->Send(client->peer_id, channelnum, data, reliable);
if (client->net_proto_version != 0) {
m_con->Send(client->peer_id, channelnum, pkt, reliable);
}
}
delete pkt;
}
RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "serialization.h" // for SER_FMT_VER_INVALID
#include "jthread/jmutex.h"
#include "network/networkpacket.h"
#include <list>
#include <vector>
@ -393,10 +394,10 @@ public:
std::vector<std::string> getPlayerNames();
/* send message to client */
void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable, bool deletepkt=true);
/* send to all clients */
void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
void sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable);
/* delete a client */
void DeleteClient(u16 peer_id);

View File

@ -108,3 +108,77 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_LOCAL_PLAYER_ANIMATIONS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_LocalPlayerAnimations }, // 0x51
{ "TOCLIENT_EYE_OFFSET", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_EyeOffset }, // 0x52
};
const static ServerCommandFactory null_command_factory = { "TOSERVER_NULL", 0, false };
const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
{
null_command_factory, // 0x00
null_command_factory, // 0x01
null_command_factory, // 0x02
null_command_factory, // 0x03
null_command_factory, // 0x04
null_command_factory, // 0x05
null_command_factory, // 0x06
null_command_factory, // 0x07
null_command_factory, // 0x08
null_command_factory, // 0x09
null_command_factory, // 0x0a
null_command_factory, // 0x0b
null_command_factory, // 0x0c
null_command_factory, // 0x0d
null_command_factory, // 0x0e
null_command_factory, // 0x0f
{ "TOSERVER_INIT", 1, false }, // 0x10
{ "TOSERVER_INIT2", 1, true }, // 0x11
null_command_factory, // 0x12
null_command_factory, // 0x13
null_command_factory, // 0x14
null_command_factory, // 0x15
null_command_factory, // 0x16
null_command_factory, // 0x17
null_command_factory, // 0x18
null_command_factory, // 0x19
null_command_factory, // 0x1a
null_command_factory, // 0x1b
null_command_factory, // 0x1c
null_command_factory, // 0x1d
null_command_factory, // 0x1e
null_command_factory, // 0x1f
null_command_factory, // 0x20
null_command_factory, // 0x21
null_command_factory, // 0x22
{ "TOSERVER_PLAYERPOS", 0, false }, // 0x23
{ "TOSERVER_GOTBLOCKS", 2, true }, // 0x24
{ "TOSERVER_DELETEDBLOCKS", 2, true }, // 0x25
null_command_factory, // 0x26
{ "TOSERVER_CLICK_OBJECT", 0, false }, // 0x27
{ "TOSERVER_GROUND_ACTION", 0, false }, // 0x28
{ "TOSERVER_RELEASE", 0, false }, // 0x29
null_command_factory, // 0x2a
null_command_factory, // 0x2b
null_command_factory, // 0x2c
null_command_factory, // 0x2d
null_command_factory, // 0x2e
null_command_factory, // 0x2f
{ "TOSERVER_SIGNTEXT", 0, false }, // 0x30
{ "TOSERVER_INVENTORY_ACTION", 0, true }, // 0x31
{ "TOSERVER_CHAT_MESSAGE", 0, true }, // 0x32
{ "TOSERVER_SIGNNODETEXT", 0, false }, // 0x33
{ "TOSERVER_CLICK_ACTIVEOBJECT", 0, false }, // 0x34
{ "TOSERVER_DAMAGE", 0, true }, // 0x35
{ "TOSERVER_PASSWORD", 0, true }, // 0x36
{ "TOSERVER_PLAYERITEM", 0, true }, // 0x37
{ "TOSERVER_RESPAWN", 0, true }, // 0x38
{ "TOSERVER_INTERACT", 0, true }, // 0x39
{ "TOSERVER_REMOVED_SOUNDS", 1, true }, // 0x3a
{ "TOSERVER_NODEMETA_FIELDS", 0, true }, // 0x3b
{ "TOSERVER_INVENTORY_FIELDS", 0, true }, // 0x3c
null_command_factory, // 0x3d
null_command_factory, // 0x3e
null_command_factory, // 0x3f
{ "TOSERVER_REQUEST_MEDIA", 1, true }, // 0x40
{ "TOSERVER_RECEIVED_MEDIA", 1, true }, // 0x41
{ "TOSERVER_BREATH", 0, true }, // 0x42
{ "TOSERVER_CLIENT_READY", 0, true }, // 0x43
};

View File

@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client.h"
#include "networkprotocol.h"
#include "toclientpacket.h"
#include "networkpacket.h"
enum ToClientConnectionState {
TOCLIENT_STATE_NOT_CONNECTED,
@ -33,11 +33,20 @@ enum ToClientConnectionState {
struct ToClientCommandHandler
{
char const* name;
const char* name;
ToClientConnectionState state;
void (Client::*handler)(ToClientPacket* pkt);
void (Client::*handler)(NetworkPacket* pkt);
};
struct ServerCommandFactory
{
const char* name;
u16 channel;
bool reliable;
};
extern const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES];
extern const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES];
#endif

View File

@ -29,7 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "settings.h"
#include "profiler.h"
#include "main.h" // for profiling
namespace con
{
@ -1728,8 +1727,8 @@ void ConnectionSendThread::connect(Address address)
// Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT
m_connection->SetPeerID(PEER_ID_INEXISTENT);
SharedBuffer<u8> data(0);
m_connection->Send(PEER_ID_SERVER, 0, data, true);
NetworkPacket* pkt = new NetworkPacket(0,0);
m_connection->Send(PEER_ID_SERVER, 0, pkt, true);
}
void ConnectionSendThread::disconnect()
@ -2107,20 +2106,17 @@ void ConnectionReceiveThread::receive()
/* first of all read packets from socket */
/* check for incoming data available */
while( (loop_count < 10) &&
(m_connection->m_udpSocket.WaitData(50)))
{
(m_connection->m_udpSocket.WaitData(50))) {
loop_count++;
try {
if (packet_queued)
{
bool no_data_left = false;
if (packet_queued) {
bool data_left = true;
u16 peer_id;
SharedBuffer<u8> resultdata;
while(!no_data_left)
{
while(data_left) {
try {
no_data_left = !getFromBuffers(peer_id, resultdata);
if (!no_data_left) {
data_left = getFromBuffers(peer_id, resultdata);
if (data_left) {
ConnectionEvent e;
e.dataReceived(peer_id, resultdata);
m_connection->putEvent(e);
@ -2136,8 +2132,7 @@ void ConnectionReceiveThread::receive()
Address sender;
s32 received_size = m_connection->m_udpSocket.Receive(sender, *packetdata, packet_maxsize);
if ((received_size < 0) ||
(received_size < BASE_HEADER_SIZE) ||
if ((received_size < BASE_HEADER_SIZE) ||
(readU32(&packetdata[0]) != m_connection->GetProtocolID()))
{
LOG(derr_con<<m_connection->getDesc()
@ -2162,14 +2157,12 @@ void ConnectionReceiveThread::receive()
u16 packet_peer_id = peer_id;
/* Try to identify peer by sender address (may happen on join) */
if(peer_id == PEER_ID_INEXISTENT)
{
if (peer_id == PEER_ID_INEXISTENT) {
peer_id = m_connection->lookupPeer(sender);
}
/* The peer was not found in our lists. Add it. */
if(peer_id == PEER_ID_INEXISTENT)
{
if (peer_id == PEER_ID_INEXISTENT) {
peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
}
@ -2245,11 +2238,14 @@ void ConnectionReceiveThread::receive()
ConnectionEvent e;
e.dataReceived(peer_id, resultdata);
m_connection->putEvent(e);
}catch(ProcessedSilentlyException &e){
}catch(ProcessedQueued &e){
}
catch(ProcessedSilentlyException &e) {
}
catch(ProcessedQueued &e) {
packet_queued = true;
}
}catch(InvalidIncomingDataException &e){
}
catch(InvalidIncomingDataException &e) {
}
catch(ProcessedSilentlyException &e) {
}
@ -2274,10 +2270,7 @@ bool ConnectionReceiveThread::getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst
{
Channel *channel = &(dynamic_cast<UDPPeer*>(&peer))->channels[i];
SharedBuffer<u8> resultdata;
bool got = checkIncomingBuffers(channel, peer_id, resultdata);
if(got){
dst = resultdata;
if (checkIncomingBuffers(channel, peer_id, dst)) {
return true;
}
}
@ -2403,8 +2396,7 @@ SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
}
throw ProcessedSilentlyException("Got an ACK");
}
else if(controltype == CONTROLTYPE_SET_PEER_ID)
{
else if (controltype == CONTROLTYPE_SET_PEER_ID) {
// Got a packet to set our peer id
if (packetdata.getSize() < 4)
throw InvalidIncomingDataException
@ -2811,6 +2803,7 @@ bool Connection::deletePeer(u16 peer_id, bool timeout)
return false;
peer = m_peers[peer_id];
m_peers.erase(peer_id);
m_peer_ids.remove(peer_id);
}
Address peer_address;
@ -2851,8 +2844,7 @@ ConnectionEvent Connection::waitEvent(u32 timeout_ms)
void Connection::putCommand(ConnectionCommand &c)
{
if (!m_shutting_down)
{
if (!m_shutting_down) {
m_command_queue.push_back(c);
m_sendThread.Trigger();
}
@ -2928,22 +2920,14 @@ u32 Connection::Receive(u16 &peer_id, SharedBuffer<u8> &data)
throw NoIncomingDataException("No incoming data");
}
void Connection::SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable)
{
assert(channelnum < CHANNEL_COUNT);
ConnectionCommand c;
c.sendToAll(channelnum, data, reliable);
putCommand(c);
}
void Connection::Send(u16 peer_id, u8 channelnum,
SharedBuffer<u8> data, bool reliable)
NetworkPacket* pkt, bool reliable)
{
assert(channelnum < CHANNEL_COUNT);
ConnectionCommand c;
c.send(peer_id, channelnum, data, reliable);
c.send(peer_id, channelnum, pkt->oldForgePacket(), reliable);
putCommand(c);
}
@ -3013,13 +2997,12 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
/*
Find an unused peer id
*/
{
JMutexAutoLock lock(m_peers_mutex);
bool out_of_ids = false;
for(;;)
{
for(;;) {
// Check if exists
if (m_peers.find(peer_id_new) == m_peers.end())
break;
// Check for overflow
if (peer_id_new == overflow) {
@ -3028,6 +3011,7 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
}
peer_id_new++;
}
if (out_of_ids) {
errorstream << getDesc() << " ran out of peer ids" << std::endl;
return PEER_ID_INEXISTENT;
@ -3038,7 +3022,7 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
peer = new UDPPeer(peer_id_new, sender, this);
m_peers[peer->id] = peer;
}
m_peer_ids.push_back(peer->id);
m_next_remote_peer_id = (peer_id_new +1 ) % MAX_UDP_PEERS;
@ -3051,7 +3035,7 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
writeU8(&reply[1], CONTROLTYPE_SET_PEER_ID);
writeU16(&reply[2], peer_id_new);
cmd.createPeer(peer_id_new,reply);
this->putCommand(cmd);
putCommand(cmd);
// Create peer addition event
ConnectionEvent e;
@ -3119,22 +3103,10 @@ UDPPeer* Connection::createServerPeer(Address& address)
{
JMutexAutoLock lock(m_peers_mutex);
m_peers[peer->id] = peer;
m_peer_ids.push_back(peer->id);
}
return peer;
}
std::list<u16> Connection::getPeerIDs()
{
std::list<u16> retval;
JMutexAutoLock lock(m_peers_mutex);
for(std::map<u16, Peer*>::iterator j = m_peers.begin();
j != m_peers.end(); ++j)
{
retval.push_back(j->first);
}
return retval;
}
} // namespace

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "socket.h"
#include "exceptions.h"
#include "constants.h"
#include "network/networkpacket.h"
#include "util/pointer.h"
#include "util/container.h"
#include "util/thread.h"
@ -1032,8 +1033,7 @@ public:
bool Connected();
void Disconnect();
u32 Receive(u16 &peer_id, SharedBuffer<u8> &data);
void SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable);
void Send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
void Send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
u16 GetPeerID() { return m_peer_id; }
Address GetPeerAddress(u16 peer_id);
float getPeerStat(u16 peer_id, rtt_stat_type type);
@ -1058,7 +1058,7 @@ protected:
void PrintInfo(std::ostream &out);
void PrintInfo();
std::list<u16> getPeerIDs();
std::list<u16> getPeerIDs() { return m_peer_ids; }
UDPSocket m_udpSocket;
MutexedQueue<ConnectionCommand> m_command_queue;
@ -1076,6 +1076,7 @@ private:
u32 m_protocol_id;
std::map<u16, Peer*> m_peers;
std::list<u16> m_peer_ids;
JMutex m_peers_mutex;
ConnectionSendThread m_sendThread;
@ -1095,4 +1096,3 @@ private:
} // namespace
#endif

View File

@ -1,6 +1,5 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
@ -20,37 +19,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "networkpacket.h"
#include "debug.h"
#include "exceptions.h"
#include "util/serialize.h"
NetworkPacket::NetworkPacket(u8 *data, u32 datasize, u16 peer_id):
m_peer_id(peer_id)
m_read_offset(0), m_peer_id(peer_id)
{
m_read_offset = 0;
m_datasize = datasize - 2;
// Copy data packet to remove opcode
m_data = new u8[m_datasize];
// split command and datas
m_command = readU16(&data[0]);
m_data = std::vector<u8>(&data[2], &data[2 + m_datasize]);
}
memcpy(m_data, &data[2], m_datasize);
NetworkPacket::NetworkPacket(u16 command, u32 datasize, u16 peer_id):
m_datasize(datasize), m_read_offset(0), m_command(command), m_peer_id(peer_id)
{
m_data.resize(m_datasize);
}
NetworkPacket::NetworkPacket(u16 command, u32 datasize):
m_datasize(datasize), m_read_offset(0), m_command(command), m_peer_id(0)
{
m_data.resize(m_datasize);
}
NetworkPacket::~NetworkPacket()
{
delete [] m_data;
m_data.clear();
}
char* NetworkPacket::getString(u32 from_offset)
{
assert(from_offset < m_datasize);
if (from_offset >= m_datasize)
throw SerializationError("Malformed packet read");
return (char*)&m_data[from_offset];
}
char NetworkPacket::getChar(u32 offset)
void NetworkPacket::putRawString(const char* src, u32 len)
{
assert(offset < m_datasize);
if (m_read_offset + len * sizeof(char) >= m_datasize) {
m_datasize += len * sizeof(char);
m_data.resize(m_datasize);
}
return m_data[offset];
memcpy(&m_data[m_read_offset], src, len);
m_read_offset += len;
}
NetworkPacket& NetworkPacket::operator>>(std::string& dst)
@ -64,7 +80,6 @@ NetworkPacket& NetworkPacket::operator>>(std::string& dst)
return *this;
}
dst.reserve(strLen);
dst.append((char*)&m_data[m_read_offset], strLen);
@ -72,6 +87,44 @@ NetworkPacket& NetworkPacket::operator>>(std::string& dst)
return *this;
}
NetworkPacket& NetworkPacket::operator<<(std::string src)
{
u16 msgsize = src.size();
if (msgsize > 0xFFFF) {
msgsize = 0xFFFF;
}
*this << msgsize;
if (m_read_offset + msgsize * sizeof(char) >= m_datasize) {
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}
memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
return *this;
}
void NetworkPacket::putLongString(std::string src)
{
u32 msgsize = src.size();
if (msgsize > 0xFFFFFFFF) {
msgsize = 0xFFFFFFFF;
}
*this << msgsize;
if (m_read_offset + msgsize * sizeof(char) >= m_datasize) {
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}
memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
}
NetworkPacket& NetworkPacket::operator>>(std::wstring& dst)
{
u16 strLen = readU16(&m_data[m_read_offset]);
@ -93,6 +146,23 @@ NetworkPacket& NetworkPacket::operator>>(std::wstring& dst)
return *this;
}
NetworkPacket& NetworkPacket::operator<<(std::wstring src)
{
u16 msgsize = src.size();
if (msgsize > 0xFFFF) {
msgsize = 0xFFFF;
}
*this << msgsize;
// Write string
for (u16 i=0; i<msgsize; i++) {
*this << (u16) src[i];
}
return *this;
}
std::string NetworkPacket::readLongString()
{
u32 strLen = readU32(&m_data[m_read_offset]);
@ -114,151 +184,251 @@ std::string NetworkPacket::readLongString()
NetworkPacket& NetworkPacket::operator>>(char& dst)
{
dst = getChar(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(char);
dst = readU8(&m_data[m_read_offset]);
incrOffset<char>();
return *this;
}
u8* NetworkPacket::getU8Ptr(u32 from_offset)
char NetworkPacket::getChar(u32 offset)
{
assert(from_offset < m_datasize);
if (offset >= m_datasize)
throw SerializationError("Malformed packet read");
return (u8*)&m_data[from_offset];
return readU8(&m_data[offset]);
}
u8 NetworkPacket::getU8(u32 offset)
NetworkPacket& NetworkPacket::operator<<(char src)
{
assert(offset < m_datasize);
checkDataSize<u8>();
return m_data[offset];
writeU8(&m_data[m_read_offset], src);
incrOffset<char>();
return *this;
}
NetworkPacket& NetworkPacket::operator>>(u8& dst)
NetworkPacket& NetworkPacket::operator<<(u8 src)
{
assert(m_read_offset < m_datasize);
dst = m_data[m_read_offset];
checkDataSize<u8>();
m_read_offset += sizeof(u8);
writeU8(&m_data[m_read_offset], src);
incrOffset<u8>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(bool src)
{
checkDataSize<u8>();
writeU8(&m_data[m_read_offset], src);
incrOffset<u8>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(u16 src)
{
checkDataSize<u16>();
writeU16(&m_data[m_read_offset], src);
incrOffset<u16>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(u32 src)
{
checkDataSize<u32>();
writeU32(&m_data[m_read_offset], src);
incrOffset<u32>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(u64 src)
{
checkDataSize<u64>();
writeU64(&m_data[m_read_offset], src);
incrOffset<u64>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(float src)
{
checkDataSize<float>();
writeF1000(&m_data[m_read_offset], src);
incrOffset<float>();
return *this;
}
NetworkPacket& NetworkPacket::operator>>(bool& dst)
{
assert(m_read_offset < m_datasize);
dst = m_data[m_read_offset];
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(u8);
dst = readU8(&m_data[m_read_offset]);
incrOffset<u8>();
return *this;
}
NetworkPacket& NetworkPacket::operator>>(u8& dst)
{
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readU8(&m_data[m_read_offset]);
incrOffset<u8>();
return *this;
}
u8 NetworkPacket::getU8(u32 offset)
{
if (offset >= m_datasize)
throw SerializationError("Malformed packet read");
return readU8(&m_data[offset]);
}
u8* NetworkPacket::getU8Ptr(u32 from_offset)
{
if (m_datasize == 0) {
return NULL;
}
if (from_offset >= m_datasize)
throw SerializationError("Malformed packet read");
return (u8*)&m_data[from_offset];
}
NetworkPacket& NetworkPacket::operator>>(u16& dst)
{
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readU16(&m_data[m_read_offset]);
incrOffset<u16>();
return *this;
}
u16 NetworkPacket::getU16(u32 from_offset)
{
assert(from_offset < m_datasize);
if (from_offset >= m_datasize)
throw SerializationError("Malformed packet read");
return readU16(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(u16& dst)
{
dst = getU16(m_read_offset);
m_read_offset += sizeof(u16);
return *this;
}
u32 NetworkPacket::getU32(u32 from_offset)
{
assert(from_offset < m_datasize);
return readU32(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(u32& dst)
{
dst = getU32(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(u32);
dst = readU32(&m_data[m_read_offset]);
incrOffset<u32>();
return *this;
}
u64 NetworkPacket::getU64(u32 from_offset)
{
assert(from_offset < m_datasize);
return readU64(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(u64& dst)
{
dst = getU64(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(u64);
dst = readU64(&m_data[m_read_offset]);
incrOffset<u64>();
return *this;
}
float NetworkPacket::getF1000(u32 from_offset)
{
assert(from_offset < m_datasize);
return readF1000(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(float& dst)
{
dst = getF1000(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(float);
dst = readF1000(&m_data[m_read_offset]);
incrOffset<float>();
return *this;
}
NetworkPacket& NetworkPacket::operator>>(v2f& dst)
{
assert(m_read_offset < m_datasize);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readV2F1000(&m_data[m_read_offset]);
m_read_offset += sizeof(v2f);
incrOffset<v2f>();
return *this;
}
NetworkPacket& NetworkPacket::operator>>(v3f& dst)
{
assert(m_read_offset < m_datasize);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readV3F1000(&m_data[m_read_offset]);
m_read_offset += sizeof(v3f);
incrOffset<v3f>();
return *this;
}
s16 NetworkPacket::getS16(u32 from_offset)
{
assert(from_offset < m_datasize);
return readS16(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(s16& dst)
{
dst = getS16(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(s16);
dst = readS16(&m_data[m_read_offset]);
incrOffset<s16>();
return *this;
}
s32 NetworkPacket::getS32(u32 from_offset)
NetworkPacket& NetworkPacket::operator<<(s16 src)
{
assert(from_offset < m_datasize);
return readS32(&m_data[from_offset]);
*this << (u16) src;
return *this;
}
NetworkPacket& NetworkPacket::operator>>(s32& dst)
{
dst = getS32(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(s32);
dst = readS32(&m_data[m_read_offset]);
incrOffset<s32>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(s32 src)
{
*this << (u32) src;
return *this;
}
NetworkPacket& NetworkPacket::operator>>(v3s16& dst)
{
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readV3S16(&m_data[m_read_offset]);
incrOffset<v3s16>();
return *this;
}
@ -266,36 +436,88 @@ NetworkPacket& NetworkPacket::operator>>(v2s32& dst)
{
dst = readV2S32(&m_data[m_read_offset]);
m_read_offset += sizeof(v2s32);
incrOffset<v2s32>();
return *this;
}
v3s16 NetworkPacket::getV3S16(u32 from_offset)
{
assert(from_offset < m_datasize);
return readV3S16(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(v3s16& dst)
{
dst = getV3S16(m_read_offset);
m_read_offset += sizeof(v3s16);
return *this;
}
v3s32 NetworkPacket::getV3S32(u32 from_offset)
{
assert(from_offset < m_datasize);
return readV3S32(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(v3s32& dst)
{
dst = getV3S32(m_read_offset);
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
m_read_offset += sizeof(v3s32);
dst = readV3S32(&m_data[m_read_offset]);
incrOffset<v3s32>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(v2f src)
{
*this << (float) src.X;
*this << (float) src.Y;
return *this;
}
NetworkPacket& NetworkPacket::operator<<(v3f src)
{
*this << (float) src.X;
*this << (float) src.Y;
*this << (float) src.Z;
return *this;
}
NetworkPacket& NetworkPacket::operator<<(v3s16 src)
{
*this << (s16) src.X;
*this << (s16) src.Y;
*this << (s16) src.Z;
return *this;
}
NetworkPacket& NetworkPacket::operator<<(v2s32 src)
{
*this << (s32) src.X;
*this << (s32) src.Y;
return *this;
}
NetworkPacket& NetworkPacket::operator<<(v3s32 src)
{
*this << (s32) src.X;
*this << (s32) src.Y;
*this << (s32) src.Z;
return *this;
}
NetworkPacket& NetworkPacket::operator>>(video::SColor& dst)
{
if (m_read_offset >= m_datasize)
throw SerializationError("Malformed packet read");
dst = readARGB8(&m_data[m_read_offset]);
incrOffset<u32>();
return *this;
}
NetworkPacket& NetworkPacket::operator<<(video::SColor src)
{
checkDataSize<u32>();
writeU32(&m_data[m_read_offset], src.color);
incrOffset<u32>();
return *this;
}
SharedBuffer<u8> NetworkPacket::oldForgePacket()
{
SharedBuffer<u8> sb(m_datasize + 2);
writeU16(&sb[0], m_command);
u8* datas = getU8Ptr(0);
if (datas != NULL)
memcpy(&sb[2], datas, m_datasize);
return sb;
}

View File

@ -1,6 +1,5 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
@ -21,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef NETWORKPACKET_HEADER
#define NETWORKPACKET_HEADER
#include "util/pointer.h"
#include "util/numeric.h"
#include "networkprotocol.h"
@ -29,57 +29,100 @@ class NetworkPacket
public:
NetworkPacket(u8 *data, u32 datasize, u16 peer_id);
NetworkPacket(u16 command, u32 datasize, u16 peer_id);
NetworkPacket(u16 command, u32 datasize);
~NetworkPacket();
// Getters
u32 getSize() { return m_datasize; }
u16 getPeerId() { return m_peer_id; }
u16 getCommand() { return m_command; }
// Data extractors
char* getString(u32 from_offset);
void putRawString(const char* src, u32 len);
NetworkPacket& operator>>(std::string& dst);
NetworkPacket& operator<<(std::string src);
void putLongString(std::string src);
NetworkPacket& operator>>(std::wstring& dst);
NetworkPacket& operator<<(std::wstring src);
std::string readLongString();
char getChar(u32 offset);
NetworkPacket& operator>>(char& dst);
NetworkPacket& operator<<(char src);
NetworkPacket& operator>>(bool& dst);
NetworkPacket& operator<<(bool src);
u8 getU8(u32 offset);
NetworkPacket& operator>>(u8& dst);
NetworkPacket& operator<<(u8 src);
u8* getU8Ptr(u32 offset);
u16 getU16(u32 from_offset);
NetworkPacket& operator>>(u16& dst);
u32 getU32(u32 from_offset);
NetworkPacket& operator<<(u16 src);
NetworkPacket& operator>>(u32& dst);
u64 getU64(u32 from_offset);
NetworkPacket& operator<<(u32 src);
NetworkPacket& operator>>(u64& dst);
NetworkPacket& operator<<(u64 src);
float getF1000(u32 offset);
NetworkPacket& operator>>(float& dst);
NetworkPacket& operator>>(v2f& dst);
NetworkPacket& operator>>(v3f& dst);
NetworkPacket& operator<<(float src);
NetworkPacket& operator>>(v2f& dst);
NetworkPacket& operator<<(v2f src);
NetworkPacket& operator>>(v3f& dst);
NetworkPacket& operator<<(v3f src);
s16 getS16(u32 from_offset);
NetworkPacket& operator>>(s16& dst);
s32 getS32(u32 from_offset);
NetworkPacket& operator<<(s16 src);
NetworkPacket& operator>>(s32& dst);
NetworkPacket& operator<<(s32 src);
NetworkPacket& operator>>(v2s32& dst);
NetworkPacket& operator<<(v2s32 src);
v3s16 getV3S16(u32 from_offset);
NetworkPacket& operator>>(v3s16& dst);
NetworkPacket& operator<<(v3s16 src);
v3s32 getV3S32(u32 from_offset);
NetworkPacket& operator>>(v3s32& dst);
NetworkPacket& operator<<(v3s32 src);
protected:
u8 *m_data;
NetworkPacket& operator>>(video::SColor& dst);
NetworkPacket& operator<<(video::SColor src);
// Temp, we remove SharedBuffer when migration finished
SharedBuffer<u8> oldForgePacket();
private:
template<typename T> void checkDataSize()
{
if (m_read_offset + sizeof(T) > m_datasize) {
m_datasize += sizeof(T);
m_data.resize(m_datasize);
}
}
template<typename T> void incrOffset()
{
m_read_offset += sizeof(T);
}
std::vector<u8> m_data;
u32 m_datasize;
u32 m_read_offset;
private:
u16 m_command;
u16 m_peer_id;
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -93,3 +93,92 @@ const ToServerCommandHandler toServerCommandTable[TOSERVER_NUM_MSG_TYPES] =
{ "TOSERVER_BREATH", TOSERVER_STATE_INGAME, &Server::handleCommand_Breath }, // 0x42
{ "TOSERVER_CLIENT_READY", TOSERVER_STATE_STARTUP, &Server::handleCommand_ClientReady }, // 0x43
};
const static ClientCommandFactory null_command_factory = { "TOCLIENT_NULL", 0, false };
const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
{ "TOCLIENT_INIT", 0, true }, // 0x10
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
{ "TOCLIENT_BLOCKDATA", 2, true }, // 0x20
{ "TOCLIENT_ADDNODE", 0, true }, // 0x21
{ "TOCLIENT_REMOVENODE", 0, true }, // 0x22
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
{ "TOCLIENT_INVENTORY", 0, true }, // 0x27
null_command_factory,
{ "TOCLIENT_TIME_OF_DAY", 0, true }, // 0x29
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
null_command_factory,
{ "TOCLIENT_CHAT_MESSAGE", 0, true }, // 0x30
{ "TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD", 0, true }, // 0x31
{ "TOCLIENT_ACTIVE_OBJECT_MESSAGES", 0, true }, // 0x32 Special packet, sent by 0 (rel) and 1 (unrel) channel
{ "TOCLIENT_HP", 0, true }, // 0x33
{ "TOCLIENT_MOVE_PLAYER", 0, true }, // 0x34
{ "TOCLIENT_ACCESS_DENIED", 0, true }, // 0x35
{ "TOCLIENT_PLAYERITEM", 0, false }, // 0x36 obsolete
{ "TOCLIENT_DEATHSCREEN", 0, true }, // 0x37
{ "TOCLIENT_MEDIA", 2, true }, // 0x38
{ "TOCLIENT_TOOLDEF", 0, false }, // 0x39 obsolete
{ "TOCLIENT_NODEDEF", 0, true }, // 0x3a
{ "TOCLIENT_CRAFTITEMDEF", 0, false }, // 0x3b obsolete
{ "TOCLIENT_ANNOUNCE_MEDIA", 0, true }, // 0x3c
{ "TOCLIENT_ITEMDEF", 0, true }, // 0x3d
null_command_factory,
{ "TOCLIENT_PLAY_SOUND", 0, true }, // 0x3f
{ "TOCLIENT_STOP_SOUND", 0, true }, // 0x40
{ "TOCLIENT_PRIVILEGES", 0, true }, // 0x41
{ "TOCLIENT_INVENTORY_FORMSPEC", 0, true }, // 0x42
{ "TOCLIENT_DETACHED_INVENTORY", 0, true }, // 0x43
{ "TOCLIENT_SHOW_FORMSPEC", 0, true }, // 0x44
{ "TOCLIENT_MOVEMENT", 0, true }, // 0x45
{ "TOCLIENT_SPAWN_PARTICLE", 0, true }, // 0x46
{ "TOCLIENT_ADD_PARTICLESPAWNER", 0, true }, // 0x47
{ "TOCLIENT_DELETE_PARTICLESPAWNER", 0, true }, // 0x48
{ "TOCLIENT_HUDADD", 1, true }, // 0x49
{ "TOCLIENT_HUDRM", 1, true }, // 0x4a
{ "TOCLIENT_HUDCHANGE", 0, true }, // 0x4b
{ "TOCLIENT_HUD_SET_FLAGS", 0, true }, // 0x4c
{ "TOCLIENT_HUD_SET_PARAM", 0, true }, // 0x4d
{ "TOCLIENT_BREATH", 0, true }, // 0x4e
{ "TOCLIENT_SET_SKY", 0, true }, // 0x4f
{ "TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO", 0, true }, // 0x50
{ "TOCLIENT_LOCAL_PLAYER_ANIMATIONS", 0, true }, // 0x51
{ "TOCLIENT_EYE_OFFSET", 0, true }, // 0x52
};

View File

@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "server.h"
#include "networkprotocol.h"
#include "toserverpacket.h"
#include "networkpacket.h"
enum ToServerConnectionState {
TOSERVER_STATE_NOT_CONNECTED,
@ -35,9 +35,18 @@ struct ToServerCommandHandler
{
const std::string name;
ToServerConnectionState state;
void (Server::*handler)(ToServerPacket* pkt);
void (Server::*handler)(NetworkPacket* pkt);
};
struct ClientCommandFactory
{
const char* name;
u16 channel;
bool reliable;
};
extern const ToServerCommandHandler toServerCommandTable[TOSERVER_NUM_MSG_TYPES];
extern const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES];
#endif

View File

@ -1,28 +0,0 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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 "toclientpacket.h"
#include "util/serialize.h"
ToClientPacket::ToClientPacket(u8 *data, u32 datasize, u16 peer_id):
NetworkPacket(data, datasize, peer_id)
{
m_command = (ToClientCommand)readU16(&data[0]);
}

View File

@ -1,38 +0,0 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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.
*/
#ifndef TOCLIENTPACKET_HEADER
#define TOCLIENTPACKET_HEADER
#include "util/numeric.h"
#include "networkprotocol.h"
#include "networkpacket.h"
class ToClientPacket: public NetworkPacket
{
public:
ToClientPacket(u8 *data, u32 datasize, u16 peer_id);
ToClientCommand getCommand() { return m_command; }
private:
ToClientCommand m_command;
};
#endif

View File

@ -1,28 +0,0 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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 "toserverpacket.h"
#include "util/serialize.h"
ToServerPacket::ToServerPacket(u8 *data, u32 datasize, u16 peer_id):
NetworkPacket(data, datasize, peer_id)
{
m_command = (ToServerCommand)readU16(&data[0]);
}

View File

@ -1,38 +0,0 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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.
*/
#ifndef TOSERVERPACKET_HEADER
#define TOSERVERPACKET_HEADER
#include "util/numeric.h"
#include "networkprotocol.h"
#include "networkpacket.h"
class ToServerPacket: public NetworkPacket
{
public:
ToServerPacket(u8 *data, u32 datasize, u16 peer_id);
ToServerCommand getCommand() { return m_command; }
private:
ToServerCommand m_command;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef SERVER_HEADER
#define SERVER_HEADER
#include "connection.h"
#include "network/connection.h"
#include "irr_v3d.h"
#include "map.h"
#include "hud.h"
@ -33,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/thread.h"
#include "environment.h"
#include "clientiface.h"
#include "network/toserverpacket.h"
#include "network/networkpacket.h"
#include <string>
#include <list>
#include <map>
@ -193,32 +193,34 @@ public:
* Command Handlers
*/
void handleCommand(ToServerPacket* pkt);
void handleCommand(NetworkPacket* pkt);
void handleCommand_Null(ToServerPacket* pkt) {};
void handleCommand_Deprecated(ToServerPacket* pkt);
void handleCommand_Init(ToServerPacket* pkt);
void handleCommand_Init2(ToServerPacket* pkt);
void handleCommand_RequestMedia(ToServerPacket* pkt);
void handleCommand_ReceivedMedia(ToServerPacket* pkt);
void handleCommand_ClientReady(ToServerPacket* pkt);
void handleCommand_GotBlocks(ToServerPacket* pkt);
void handleCommand_PlayerPos(ToServerPacket* pkt);
void handleCommand_DeletedBlocks(ToServerPacket* pkt);
void handleCommand_InventoryAction(ToServerPacket* pkt);
void handleCommand_ChatMessage(ToServerPacket* pkt);
void handleCommand_Damage(ToServerPacket* pkt);
void handleCommand_Breath(ToServerPacket* pkt);
void handleCommand_Password(ToServerPacket* pkt);
void handleCommand_PlayerItem(ToServerPacket* pkt);
void handleCommand_Respawn(ToServerPacket* pkt);
void handleCommand_Interact(ToServerPacket* pkt);
void handleCommand_RemovedSounds(ToServerPacket* pkt);
void handleCommand_NodeMetaFields(ToServerPacket* pkt);
void handleCommand_InventoryFields(ToServerPacket* pkt);
void handleCommand_Null(NetworkPacket* pkt) {};
void handleCommand_Deprecated(NetworkPacket* pkt);
void handleCommand_Init(NetworkPacket* pkt);
void handleCommand_Init2(NetworkPacket* pkt);
void handleCommand_RequestMedia(NetworkPacket* pkt);
void handleCommand_ReceivedMedia(NetworkPacket* pkt);
void handleCommand_ClientReady(NetworkPacket* pkt);
void handleCommand_GotBlocks(NetworkPacket* pkt);
void handleCommand_PlayerPos(NetworkPacket* pkt);
void handleCommand_DeletedBlocks(NetworkPacket* pkt);
void handleCommand_InventoryAction(NetworkPacket* pkt);
void handleCommand_ChatMessage(NetworkPacket* pkt);
void handleCommand_Damage(NetworkPacket* pkt);
void handleCommand_Breath(NetworkPacket* pkt);
void handleCommand_Password(NetworkPacket* pkt);
void handleCommand_PlayerItem(NetworkPacket* pkt);
void handleCommand_Respawn(NetworkPacket* pkt);
void handleCommand_Interact(NetworkPacket* pkt);
void handleCommand_RemovedSounds(NetworkPacket* pkt);
void handleCommand_NodeMetaFields(NetworkPacket* pkt);
void handleCommand_InventoryFields(NetworkPacket* pkt);
void ProcessData(u8 *data, u32 datasize, u16 peer_id);
void Send(NetworkPacket* pkt);
// Environment must be locked when called
void setTimeOfDay(u32 time);

View File

@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
#include "main.h"
#include "socket.h"
#include "connection.h"
#include "network/connection.h"
#include "serialization.h"
#include "voxel.h"
#include "collision.h"
@ -1986,17 +1986,17 @@ struct TestConnection: public TestBase
catch(con::NoIncomingDataException &e)
{
}
#if 1
/*
Simple send-receive test
*/
{
/*u8 data[] = "Hello World!";
u32 datasize = sizeof(data);*/
SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
NetworkPacket* pkt = new NetworkPacket((u8*) "Hello World !", 14, 0);
SharedBuffer<u8> sentdata = pkt->oldForgePacket();
infostream<<"** running client.Send()"<<std::endl;
client.Send(PEER_ID_SERVER, 0, data, true);
client.Send(PEER_ID_SERVER, 0, pkt, true);
sleep_ms(50);
@ -2006,148 +2006,37 @@ struct TestConnection: public TestBase
u32 size = server.Receive(peer_id, recvdata);
infostream << "** Server received: peer_id=" << peer_id
<< ", size=" << size
<<", data="<<*data
<< ", data=" << (const char*)pkt->getU8Ptr(0)
<< std::endl;
UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
}
#endif
u16 peer_id_client = 2;
#if 0
/*
Send consequent packets in different order
Not compatible with new Connection, thus commented out.
*/
{
//u8 data1[] = "hello1";
//u8 data2[] = "hello2";
SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
Address client_address =
server.GetPeerAddress(peer_id_client);
infostream<<"*** Sending packets in wrong order (2,1,2)"
<<std::endl;
u8 chn = 0;
con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
u16 sn = ch->next_outgoing_seqnum;
ch->next_outgoing_seqnum = sn+1;
server.Send(peer_id_client, chn, data2, true);
ch->next_outgoing_seqnum = sn;
server.Send(peer_id_client, chn, data1, true);
ch->next_outgoing_seqnum = sn+1;
server.Send(peer_id_client, chn, data2, true);
sleep_ms(50);
infostream<<"*** Receiving the packets"<<std::endl;
u16 peer_id;
SharedBuffer<u8> recvdata;
u32 size;
infostream<<"** running client.Receive()"<<std::endl;
peer_id = 132;
size = client.Receive(peer_id, recvdata);
infostream<<"** Client received: peer_id="<<peer_id
<<", size="<<size
<<", data="<<*recvdata
<<std::endl;
UASSERT(size == data1.getSize());
UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER);
infostream<<"** running client.Receive()"<<std::endl;
peer_id = 132;
size = client.Receive(peer_id, recvdata);
infostream<<"** Client received: peer_id="<<peer_id
<<", size="<<size
<<", data="<<*recvdata
<<std::endl;
UASSERT(size == data2.getSize());
UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER);
bool got_exception = false;
try
{
infostream<<"** running client.Receive()"<<std::endl;
peer_id = 132;
size = client.Receive(peer_id, recvdata);
infostream<<"** Client received: peer_id="<<peer_id
<<", size="<<size
<<", data="<<*recvdata
<<std::endl;
}
catch(con::NoIncomingDataException &e)
{
infostream<<"** No incoming data for client"<<std::endl;
got_exception = true;
}
UASSERT(got_exception);
}
#endif
#if 0
/*
Send large amounts of packets (infinite test)
Commented out because of infinity.
*/
{
infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
int sendcount = 0;
for(;;){
int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
infostream<<"datasize="<<datasize<<std::endl;
SharedBuffer<u8> data1(datasize);
for(u16 i=0; i<datasize; i++)
data1[i] = i/4;
int sendtimes = myrand_range(1,10);
for(int i=0; i<sendtimes; i++){
server.Send(peer_id_client, 0, data1, true);
sendcount++;
}
infostream<<"sendcount="<<sendcount<<std::endl;
//int receivetimes = myrand_range(1,20);
int receivetimes = 20;
for(int i=0; i<receivetimes; i++){
SharedBuffer<u8> recvdata;
u16 peer_id = 132;
u16 size = 0;
bool received = false;
try{
size = client.Receive(peer_id, recvdata);
received = true;
}catch(con::NoIncomingDataException &e){
}
}
}
}
#endif
/*
Send a large packet
*/
{
const int datasize = 30000;
SharedBuffer<u8> data1(datasize);
NetworkPacket* pkt = new NetworkPacket(0, datasize);
for(u16 i=0; i<datasize; i++){
data1[i] = i/4;
*pkt << (u8) i/4;
}
infostream<<"Sending data (size="<<datasize<<"):";
for(int i=0; i<datasize && i<20; i++){
if(i%2==0) infostream<<" ";
char buf[10];
snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
snprintf(buf, 10, "%.2X", ((int)((const char*)pkt->getU8Ptr(0))[i])&0xff);
infostream<<buf;
}
if(datasize>20)
infostream<<"...";
infostream<<std::endl;
server.Send(peer_id_client, 0, data1, true);
SharedBuffer<u8> sentdata = pkt->oldForgePacket();
server.Send(peer_id_client, 0, pkt, true);
//sleep_ms(3000);
@ -2183,7 +2072,7 @@ struct TestConnection: public TestBase
infostream<<"...";
infostream<<std::endl;
UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER);
}