adding protocols for game events (still not written) and controller events (partially written)
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13298 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -149,6 +149,8 @@ src/network/protocol_manager.cpp
|
||||
src/network/protocols/client_lobby_room_protocol.cpp
|
||||
src/network/protocols/connect_to_peer.cpp
|
||||
src/network/protocols/connect_to_server.cpp
|
||||
src/network/protocols/controller_events_protocol.cpp
|
||||
src/network/protocols/game_events_protocol.cpp
|
||||
src/network/protocols/get_peer_address.cpp
|
||||
src/network/protocols/get_public_address.cpp
|
||||
src/network/protocols/hide_public_address.cpp
|
||||
@@ -427,8 +429,10 @@ src/network/protocol_manager.hpp
|
||||
src/network/protocols/client_lobby_room_protocol.hpp
|
||||
src/network/protocols/connect_to_peer.hpp
|
||||
src/network/protocols/connect_to_server.hpp
|
||||
src/network/protocols/controller_events_protocol.hpp
|
||||
src/network/protocols/get_peer_address.hpp
|
||||
src/network/protocols/get_public_address.hpp
|
||||
src/network/protocols/game_events_protocol.hpp
|
||||
src/network/protocols/hide_public_address.hpp
|
||||
src/network/protocols/kart_update_protocol.hpp
|
||||
src/network/protocols/lobby_room_protocol.hpp
|
||||
|
||||
@@ -101,6 +101,9 @@ public:
|
||||
/** Called whan this controller's kart finishes the last lap. */
|
||||
virtual void finishedRace(float time) = 0;
|
||||
// ------------------------------------------------------------------------
|
||||
/** Get a pointer on the kart controls. */
|
||||
virtual KartControl* getControls() { return m_controls; }
|
||||
// ------------------------------------------------------------------------
|
||||
}; // Controller
|
||||
|
||||
#endif
|
||||
|
||||
@@ -64,7 +64,7 @@ Event::Event(ENetEvent* event)
|
||||
if (peers[i]->m_peer == event->peer)
|
||||
{
|
||||
*peer = peers[i];
|
||||
Log::info("Event", "The peer you sought has been found on %lx", (long int)(peer));
|
||||
Log::verbose("Event", "The peer you sought has been found on %lx", (long int)(peer));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ Event::Event(ENetEvent* event)
|
||||
STKPeer* new_peer = new STKPeer();
|
||||
new_peer->m_peer = event->peer;
|
||||
*peer = new_peer;
|
||||
Log::info("Event", "Creating a new peer, address are STKPeer:%lx, Peer:%lx", (long int)(new_peer), (long int)(event->peer));
|
||||
Log::debug("Event", "Creating a new peer, address are STKPeer:%lx, Peer:%lx", (long int)(new_peer), (long int)(event->peer));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ void NetworkManager::notifyEvent(Event* event)
|
||||
if (event->type == EVENT_TYPE_CONNECTED)
|
||||
{
|
||||
Log::info("NetworkManager", "A client has just connected. There are now %lu peers.", m_peers.size() + 1);
|
||||
Log::info("NetworkManager", "Addresses are : %lx, %lx, %lx", event->peer, *event->peer, peer);
|
||||
Log::debug("NetworkManager", "Addresses are : %lx, %lx, %lx", event->peer, *event->peer, peer);
|
||||
// create the new peer:
|
||||
m_peers.push_back(peer);
|
||||
}
|
||||
|
||||
@@ -99,6 +99,12 @@ class NetworkString
|
||||
return *this;
|
||||
}
|
||||
inline NetworkString& ad(const double& value) { return addDouble(value); }
|
||||
NetworkString& addChar(const char& value)
|
||||
{
|
||||
m_string.push_back((uint8_t)(value));
|
||||
return *this;
|
||||
}
|
||||
inline NetworkString& ac(const char& value) { return addChar(value); }
|
||||
|
||||
NetworkString& addString(const std::string& value)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "network/protocol_manager.hpp"
|
||||
#include "network/protocols/synchronization_protocol.hpp"
|
||||
#include "network/protocols/controller_events_protocol.hpp"
|
||||
#include "modes/world.hpp"
|
||||
|
||||
#include "karts/controller/controller.hpp"
|
||||
@@ -32,5 +33,8 @@ void NetworkWorld::update(float dt)
|
||||
|
||||
void NetworkWorld::controllerAction(Controller* controller, PlayerAction action, int value)
|
||||
{
|
||||
|
||||
ControllerEventsProtocol* protocol = static_cast<ControllerEventsProtocol*>(
|
||||
ProtocolManager::getInstance()->getProtocol(PROTOCOL_CONTROLLER_EVENTS));
|
||||
if (protocol)
|
||||
protocol->controllerAction(controller, action, value);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include <map>
|
||||
|
||||
class Controller;
|
||||
//class InputEventProtocol;
|
||||
class KartUpdateProtocol;
|
||||
class AbstractKart;
|
||||
|
||||
@@ -29,7 +28,6 @@ class NetworkWorld : public Singleton<NetworkWorld>
|
||||
protected:
|
||||
bool m_running;
|
||||
float m_race_time;
|
||||
//std::map<Controller*, InputEventProtocol*> m_events_map;
|
||||
|
||||
private:
|
||||
NetworkWorld();
|
||||
|
||||
@@ -37,6 +37,8 @@ enum PROTOCOL_TYPE
|
||||
PROTOCOL_START_GAME = 3, //!< Protocol used when starting the game.
|
||||
PROTOCOL_SYNCHRONIZATION = 4,//!<Protocol used to synchronize clocks.
|
||||
PROTOCOL_KART_UPDATE = 5, //!< Protocol to update karts position, rotation etc...
|
||||
PROTOCOL_GAME_EVENTS = 6, //!< Protocol to communicate the game events.
|
||||
PROTOCOL_CONTROLLER_EVENTS = 7,//!< Protocol to transfer controller modifications
|
||||
PROTOCOL_SILENT = 0xffff //!< Used for protocols that do not subscribe to any network event.
|
||||
};
|
||||
|
||||
|
||||
@@ -63,8 +63,8 @@ void ConnectToServer::notifyEvent(Event* event)
|
||||
{
|
||||
if (event->type == EVENT_TYPE_CONNECTED)
|
||||
{
|
||||
Log::info("ConnectToServer", "The Connect To Server protocol has \
|
||||
received an event notifying that he's connected to the peer.");
|
||||
Log::info("ConnectToServer", "The Connect To Server protocol has "
|
||||
"received an event notifying that he's connected to the peer.");
|
||||
m_state = CONNECTED; // we received a message, we are connected
|
||||
}
|
||||
}
|
||||
|
||||
149
src/network/protocols/controller_events_protocol.cpp
Normal file
149
src/network/protocols/controller_events_protocol.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "network/protocols/controller_events_protocol.hpp"
|
||||
|
||||
#include "modes/world.hpp"
|
||||
#include "karts/abstract_kart.hpp"
|
||||
#include "network/network_manager.hpp"
|
||||
#include "network/network_world.hpp"
|
||||
#include "utils/log.hpp"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ControllerEventsProtocol::ControllerEventsProtocol() :
|
||||
Protocol(NULL, PROTOCOL_CONTROLLER_EVENTS)
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ControllerEventsProtocol::~ControllerEventsProtocol()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void ControllerEventsProtocol::setup()
|
||||
{
|
||||
m_self_controller_index = 0;
|
||||
std::vector<AbstractKart*> karts = World::getWorld()->getKarts();
|
||||
std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
|
||||
for (unsigned int i = 0; i < karts.size(); i++)
|
||||
{
|
||||
if (karts[i]->getIdent() == NetworkWorld::getInstance()->m_self_kart)
|
||||
{
|
||||
Log::info("ControllerEventsProtocol", "My id is %d", i);
|
||||
m_self_controller_index = i;
|
||||
}
|
||||
STKPeer* peer = NULL;
|
||||
if (m_listener->isServer())
|
||||
{
|
||||
for (unsigned int i = 0; i < peers.size(); i++)
|
||||
{
|
||||
if (peers[i]->getPlayerProfile()->kart_name == karts[i]->getIdent())
|
||||
{
|
||||
peer = peers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
m_controllers.push_back(std::pair<Controller*, STKPeer*>(karts[i]->getController(), peer));
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void ControllerEventsProtocol::notifyEvent(Event* event)
|
||||
{
|
||||
if (event->data.size() < 13)
|
||||
{
|
||||
Log::error("ControllerEventsProtocol", "The data supplied was not complete.");
|
||||
return;
|
||||
}
|
||||
uint32_t token = event->data.gui32();
|
||||
NetworkString pure_message = event->data;
|
||||
pure_message.removeFront(4);
|
||||
if (token != (*event->peer)->getClientServerToken())
|
||||
{
|
||||
Log::error("ControllerEventsProtocol", "Bad token from peer.");
|
||||
return;
|
||||
}
|
||||
NetworkString ns = pure_message;
|
||||
float event_timestamp = ns.getFloat();
|
||||
ns.removeFront(4);
|
||||
uint8_t client_index = 0;
|
||||
while (ns.size() == 5)
|
||||
{
|
||||
uint8_t controller_index = ns.gui8(0);
|
||||
client_index = controller_index;
|
||||
uint8_t serialized_1 = ns.gui8(1), serialized_2 = ns.gui8(2), serialized_3 = ns.gui8(3);
|
||||
PlayerAction action = (PlayerAction)(ns.gui8(4));
|
||||
int action_value = ns.gui32(5);
|
||||
|
||||
KartControl* controls = m_controllers[controller_index].first->getControls();
|
||||
controls->m_brake = serialized_1 & 0b01000000;
|
||||
controls->m_nitro = serialized_1 & 0b00100000;
|
||||
controls->m_rescue = serialized_1 & 0b00010000;
|
||||
controls->m_fire = serialized_1 & 0b00001000;
|
||||
controls->m_look_back = serialized_1 & 0b00000100;
|
||||
controls->m_skid = KartControl::SkidControl(serialized_1 & 0b00000011);
|
||||
|
||||
m_controllers[controller_index].first->action(action, action_value);
|
||||
ns.removeFront(5);
|
||||
}
|
||||
if (ns.size() > 0 && ns.size() != 5)
|
||||
{
|
||||
Log::warn("ControllerEventProtocol", "The data seems corrupted.");
|
||||
}
|
||||
if (m_listener->isServer())
|
||||
{
|
||||
// notify everybody of the event :
|
||||
for (unsigned int i = 0; i < m_controllers.size(); i++)
|
||||
{
|
||||
if (i == client_index) // don't send that message to the sender
|
||||
continue;
|
||||
NetworkString ns2;
|
||||
ns2.ai32(m_controllers[i].second->getClientServerToken());
|
||||
ns2 += pure_message;
|
||||
m_listener->sendMessage(this, m_controllers[i].second, ns2, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void ControllerEventsProtocol::update()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void ControllerEventsProtocol::controllerAction(Controller* controller,
|
||||
PlayerAction action, int value)
|
||||
{
|
||||
assert(!m_listener->isServer());
|
||||
|
||||
NetworkString ns;
|
||||
ns.ai32(m_controllers[m_self_controller_index].second->getClientServerToken());
|
||||
ns.af(World::getWorld()->getTime());
|
||||
ns.ai8(m_self_controller_index);
|
||||
KartControl* controls = controller->getControls();
|
||||
uint8_t serialized_1 = 0;
|
||||
serialized_1 |= (controls->m_brake==true);
|
||||
serialized_1 <<= 1;
|
||||
serialized_1 |= (controls->m_nitro==true);
|
||||
serialized_1 <<= 1;
|
||||
serialized_1 |= (controls->m_rescue==true);
|
||||
serialized_1 <<= 1;
|
||||
serialized_1 |= (controls->m_fire==true);
|
||||
serialized_1 <<= 1;
|
||||
serialized_1 |= (controls->m_look_back==true);
|
||||
serialized_1 <<= 2;
|
||||
serialized_1 += controls->m_skid;
|
||||
uint8_t serialized_2 = (uint8_t)(controls->m_accel*255.0);
|
||||
uint8_t serialized_3 = (uint8_t)(controls->m_steer*127.0);
|
||||
ns.ai8(m_self_controller_index);
|
||||
ns.ai8(serialized_1).ai8(serialized_2).ai8(serialized_3);
|
||||
|
||||
ns.ai8((uint8_t)(action)).ai32(value);
|
||||
m_listener->sendMessage(this, ns, false); // send message to server
|
||||
}
|
||||
|
||||
|
||||
28
src/network/protocols/controller_events_protocol.hpp
Normal file
28
src/network/protocols/controller_events_protocol.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef CONTROLLER_EVENTS_PROTOCOL_HPP
|
||||
#define CONTROLLER_EVENTS_PROTOCOL_HPP
|
||||
|
||||
#include "network/protocol.hpp"
|
||||
|
||||
#include "input/input.hpp"
|
||||
#include "karts/controller/controller.hpp"
|
||||
|
||||
class ControllerEventsProtocol : public Protocol
|
||||
{
|
||||
protected:
|
||||
std::vector<std::pair<Controller*, STKPeer*> > m_controllers;
|
||||
uint32_t m_self_controller_index;
|
||||
|
||||
public:
|
||||
ControllerEventsProtocol();
|
||||
virtual ~ControllerEventsProtocol();
|
||||
|
||||
virtual void setup();
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void update();
|
||||
virtual void asynchronousUpdate() {}
|
||||
|
||||
void controllerAction(Controller* controller, PlayerAction action, int value);
|
||||
|
||||
};
|
||||
|
||||
#endif // CONTROLLER_EVENTS_PROTOCOL_HPP
|
||||
21
src/network/protocols/game_events_protocol.cpp
Normal file
21
src/network/protocols/game_events_protocol.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "network/protocols/game_events_protocol.hpp"
|
||||
|
||||
GameEventsProtocol::GameEventsProtocol() : Protocol(NULL, PROTOCOL_GAME_EVENTS)
|
||||
{
|
||||
}
|
||||
|
||||
GameEventsProtocol::~GameEventsProtocol()
|
||||
{
|
||||
}
|
||||
|
||||
void GameEventsProtocol::notifyEvent(Event* event)
|
||||
{
|
||||
}
|
||||
|
||||
void GameEventsProtocol::setup()
|
||||
{
|
||||
}
|
||||
|
||||
void GameEventsProtocol::update()
|
||||
{
|
||||
}
|
||||
21
src/network/protocols/game_events_protocol.hpp
Normal file
21
src/network/protocols/game_events_protocol.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef GAME_EVENTS_PROTOCOL_HPP
|
||||
#define GAME_EVENTS_PROTOCOL_HPP
|
||||
|
||||
#include "network/protocol.hpp"
|
||||
|
||||
|
||||
class GameEventsProtocol : public Protocol
|
||||
{
|
||||
public:
|
||||
GameEventsProtocol();
|
||||
virtual ~GameEventsProtocol();
|
||||
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
virtual void asynchronousUpdate() {}
|
||||
|
||||
protected:
|
||||
};
|
||||
|
||||
#endif // GAME_EVENTS_PROTOCOL_HPP
|
||||
@@ -39,7 +39,7 @@ void StartGameProtocol::notifyEvent(Event* event)
|
||||
Log::error("StartGameProtocol", "Too short message.");
|
||||
return;
|
||||
}
|
||||
uint32_t token = event->data.gui32(0);
|
||||
uint32_t token = event->data.gui32();
|
||||
uint8_t ready = event->data.gui8(4);
|
||||
STKPeer* peer = (*(event->peer));
|
||||
if (peer->getClientServerToken() != token)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "network/network_manager.hpp"
|
||||
#include "network/protocols/kart_update_protocol.hpp"
|
||||
#include "network/protocols/controller_events_protocol.hpp"
|
||||
#include "utils/time.hpp"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -38,7 +39,7 @@ void SynchronizationProtocol::notifyEvent(Event* event)
|
||||
Log::warn("SynchronizationProtocol", "Received a message too short.");
|
||||
return;
|
||||
}
|
||||
uint8_t talk_id = event->data.gui8(0);
|
||||
uint8_t talk_id = event->data.gui8();
|
||||
uint32_t token = event->data.gui32(1);
|
||||
uint32_t request = event->data.gui8(5);
|
||||
uint32_t sequence = event->data.gui32(6);
|
||||
@@ -129,6 +130,7 @@ void SynchronizationProtocol::asynchronousUpdate()
|
||||
m_has_quit = true;
|
||||
Log::info("SynchronizationProtocol", "Countdown finished. Starting now.");
|
||||
m_listener->requestStart(new KartUpdateProtocol());
|
||||
m_listener->requestStart(new ControllerEventsProtocol());
|
||||
m_listener->requestTerminate(this);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,14 +97,14 @@ void STKPeer::sendPacket(NetworkString const& data, bool reliable)
|
||||
(m_peer->address.host>>24)&0xff,m_peer->address.port);
|
||||
ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1,
|
||||
(reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED));
|
||||
|
||||
/* to debug the packet output
|
||||
printf("STKPeer: ");
|
||||
for (unsigned int i = 0; i < data.size(); i++)
|
||||
{
|
||||
printf("%d ", (uint8_t)(data[i]));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
*/
|
||||
enet_peer_send(m_peer, 0, packet);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user