More coding style fixes.

This commit is contained in:
hiker 2015-10-21 08:07:00 +11:00
parent 94e87796f5
commit 9cdf7058a7
16 changed files with 86 additions and 77 deletions

View File

@ -31,19 +31,19 @@ Event::Event(ENetEvent* event)
switch (event->type)
{
case ENET_EVENT_TYPE_CONNECT:
type = EVENT_TYPE_CONNECTED;
m_type = EVENT_TYPE_CONNECTED;
break;
case ENET_EVENT_TYPE_DISCONNECT:
type = EVENT_TYPE_DISCONNECTED;
m_type = EVENT_TYPE_DISCONNECTED;
break;
case ENET_EVENT_TYPE_RECEIVE:
type = EVENT_TYPE_MESSAGE;
m_type = EVENT_TYPE_MESSAGE;
break;
case ENET_EVENT_TYPE_NONE:
return;
break;
}
if (type == EVENT_TYPE_MESSAGE)
if (m_type == EVENT_TYPE_MESSAGE)
{
m_data = NetworkString(std::string((char*)(event->packet->data),
event->packet->dataLength-1));
@ -59,23 +59,23 @@ Event::Event(ENetEvent* event)
m_packet = NULL;
std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
peer = new STKPeer*;
*peer = NULL;
m_peer = new STKPeer*;
*m_peer = NULL;
for (unsigned int i = 0; i < peers.size(); i++)
{
if (peers[i]->m_peer == event->peer)
{
*peer = peers[i];
*m_peer = peers[i];
Log::verbose("Event", "The peer you sought has been found on %p",
peer);
m_peer);
return;
}
}
if (*peer == NULL) // peer does not exist, create him
if (*m_peer == NULL) // peer does not exist, create him
{
STKPeer* new_peer = new STKPeer();
new_peer->m_peer = event->peer;
*peer = new_peer;
*m_peer = new_peer;
Log::debug("Event",
"Creating a new peer, address are STKPeer:%p, Peer:%p",
new_peer, event->peer);
@ -88,11 +88,10 @@ Event::Event(ENetEvent* event)
*/
Event::Event(const Event& event)
{
type = event.type;
m_type = event.m_type;
m_packet = NULL;
m_data = event.m_data;
// copy the peer
peer = event.peer;
m_peer = event.m_peer;
} // Event(Event)
// ----------------------------------------------------------------------------
@ -100,12 +99,15 @@ Event::Event(const Event& event)
*/
Event::~Event()
{
delete peer;
peer = NULL;
delete m_peer;
m_peer = NULL;
m_packet = NULL;
} // ~Event
// ----------------------------------------------------------------------------
/** \brief Remove bytes at the beginning of data.
* \param size : The number of bytes to remove.
*/
void Event::removeFront(int size)
{
m_data.removeFront(size);

View File

@ -58,24 +58,31 @@ private:
/** A pointer on the ENetPacket to be deleted. */
ENetPacket* m_packet;
public:
Event(ENetEvent* event);
Event(const Event& event);
~Event();
/** Type of the event. */
EVENT_TYPE m_type;
/*! \brief Remove bytes at the beginning of data.
* \param size : The number of bytes to remove.
*/
/** Pointer to the peer that triggered that event. */
STKPeer** m_peer;
public:
Event(ENetEvent* event);
Event(const Event& event);
~Event();
void removeFront(int size);
/*! \brief Get a copy of the data.
// ------------------------------------------------------------------------
/** Returns the type of this event. */
EVENT_TYPE getType() const { return m_type; }
// ------------------------------------------------------------------------
/** Returns the peer of this event. */
STKPeer ** getPeer() const { return m_peer; }
// ------------------------------------------------------------------------
/** \brief Get a copy of the data.
* \return A copy of the message data. This is empty for events like
* connection or disconnections.
*/
* connection or disconnections. */
NetworkString data() const { return m_data; }
EVENT_TYPE type; //!< Type of the event.
STKPeer** peer; //!< Pointer to the peer that triggered that event.
}; // class Event

View File

@ -111,18 +111,18 @@ void NetworkManager::setManualSocketsMode(bool manual)
void NetworkManager::notifyEvent(Event* event)
{
Log::verbose("NetworkManager", "EVENT received of type %d",
(int)(event->type));
STKPeer* peer = *event->peer;
if (event->type == EVENT_TYPE_CONNECTED)
(int)(event->getType()));
STKPeer* peer = *event->getPeer();
if (event->getType() == EVENT_TYPE_CONNECTED)
{
Log::info("NetworkManager", "A client has just connected. There are "
"now %lu peers.", m_peers.size() + 1);
Log::debug("NetworkManager", "Addresses are : %lx, %lx, %lx",
event->peer, *event->peer, peer);
event->getPeer(), *event->getPeer(), peer);
// create the new peer:
m_peers.push_back(peer);
}
if (event->type == EVENT_TYPE_MESSAGE)
if (event->getType() == EVENT_TYPE_MESSAGE)
{
uint32_t addr = peer->getAddress();
Log::verbose("NetworkManager",

View File

@ -64,7 +64,7 @@ bool Protocol::checkDataSizeAndToken(Event* event, int minimum_size)
data.size(), data[0]);
return false;
}
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
uint32_t token = data.gui32(1);
if (token != peer->getClientServerToken())
{

View File

@ -102,7 +102,7 @@ void ProtocolManager::notifyEvent(Event* event)
// register protocols that will receive this event
std::vector<unsigned int> protocols_ids;
PROTOCOL_TYPE searched_protocol = PROTOCOL_NONE;
if (event2->type == EVENT_TYPE_MESSAGE)
if (event->getType() == EVENT_TYPE_MESSAGE)
{
if (event2->data().size() > 0)
{
@ -114,7 +114,7 @@ void ProtocolManager::notifyEvent(Event* event)
Log::warn("ProtocolManager", "Not enough data.");
}
}
if (event2->type == EVENT_TYPE_CONNECTED)
if (event->getType() == EVENT_TYPE_CONNECTED)
{
searched_protocol = PROTOCOL_CONNECTION;
}
@ -125,7 +125,7 @@ void ProtocolManager::notifyEvent(Event* event)
{
// Pass data to protocols even when paused
if (m_protocols.getData()[i].protocol->getProtocolType() == searched_protocol ||
event2->type == EVENT_TYPE_DISCONNECTED)
event->getType() == EVENT_TYPE_DISCONNECTED)
{
protocols_ids.push_back(m_protocols.getData()[i].id);
}
@ -377,7 +377,6 @@ bool ProtocolManager::propagateEvent(EventProcessingInfo* event, bool synchronou
(StkTime::getTimeSinceEpoch()-event->arrival_time) >= TIME_TO_KEEP_EVENTS)
{
// because we made a copy of the event
delete event->event->peer; // no more need of that
delete event->event;
return true;
}

View File

@ -133,7 +133,7 @@ void ClientLobbyRoomProtocol::leave()
bool ClientLobbyRoomProtocol::notifyEvent(Event* event)
{
assert(m_setup); // assert that the setup exists
if (event->type == EVENT_TYPE_MESSAGE)
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
assert(data.size()); // assert that data isn't empty
@ -159,7 +159,7 @@ bool ClientLobbyRoomProtocol::notifyEvent(Event* event)
bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
{
assert(m_setup); // assert that the setup exists
if (event->type == EVENT_TYPE_MESSAGE)
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
assert(data.size()); // assert that data isn't empty
@ -199,18 +199,18 @@ bool ClientLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
return true;
} // message
else if (event->type == EVENT_TYPE_CONNECTED)
else if (event->getType() == EVENT_TYPE_CONNECTED)
{
return true;
} // connection
else if (event->type == EVENT_TYPE_DISCONNECTED) // means we left essentially
else if (event->getType() == EVENT_TYPE_DISCONNECTED) // means we left essentially
{
NetworkManager::getInstance()->removePeer(m_server);
m_server = NULL;
NetworkManager::getInstance()->disconnected();
m_listener->requestTerminate(this);
NetworkManager::getInstance()->reset();
NetworkManager::getInstance()->removePeer(*event->peer); // prolly the same as m_server
NetworkManager::getInstance()->removePeer(*event->getPeer()); // prolly the same as m_server
return true;
} // disconnection
return false;
@ -364,7 +364,7 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
Log::error("ClientLobbyRoomProtocol", "A message notifying an accepted connection wasn't formated as expected.");
return;
}
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
uint32_t global_id = data.gui32(8);
if (global_id == PlayerManager::getCurrentOnlineId())
@ -406,7 +406,7 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
}
// add self
m_server = *(event->peer);
m_server = *(event->getPeer());
m_state = CONNECTED;
}
else
@ -604,7 +604,7 @@ void ClientLobbyRoomProtocol::raceFinished(Event* event)
return;
}
NetworkString data = event->data();
if ((*event->peer)->getClientServerToken() != data.gui32(1))
if ((*event->getPeer())->getClientServerToken() != data.gui32(1))
{
Log::error("ClientLobbyRoomProtocol", "Bad token");
return;

View File

@ -47,7 +47,7 @@ ConnectToPeer::~ConnectToPeer()
bool ConnectToPeer::notifyEventAsynchronous(Event* event)
{
if (event->type == EVENT_TYPE_CONNECTED)
if (event->getType() == EVENT_TYPE_CONNECTED)
{
Log::debug("ConnectToPeer", "Received event notifying peer connection.");
m_state = CONNECTED; // we received a message, we are connected

View File

@ -67,7 +67,7 @@ ConnectToServer::~ConnectToServer()
bool ConnectToServer::notifyEventAsynchronous(Event* event)
{
if (event->type == EVENT_TYPE_CONNECTED)
if (event->getType() == EVENT_TYPE_CONNECTED)
{
Log::info("ConnectToServer", "The Connect To Server protocol has "
"received an event notifying that he's connected to the peer.");

View File

@ -72,7 +72,7 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
uint32_t token = data.gui32();
NetworkString pure_message = data;
pure_message.removeFront(4);
if (token != (*event->peer)->getClientServerToken())
if (token != (*event->getPeer())->getClientServerToken())
{
Log::error("ControllerEventsProtocol", "Bad token from peer.");
return true;

View File

@ -19,7 +19,7 @@ GameEventsProtocol::~GameEventsProtocol()
bool GameEventsProtocol::notifyEvent(Event* event)
{
if (event->type != EVENT_TYPE_MESSAGE)
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString data = event->data();
if (data.size() < 5) // for token and type
@ -27,7 +27,7 @@ bool GameEventsProtocol::notifyEvent(Event* event)
Log::warn("GameEventsProtocol", "Too short message.");
return true;
}
if ( (*event->peer)->getClientServerToken() != data.gui32())
if ( (*event->getPeer())->getClientServerToken() != data.gui32())
{
Log::warn("GameEventsProtocol", "Bad token.");
return true;

View File

@ -31,7 +31,7 @@ KartUpdateProtocol::~KartUpdateProtocol()
bool KartUpdateProtocol::notifyEventAsynchronous(Event* event)
{
if (event->type != EVENT_TYPE_MESSAGE)
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString ns = event->data();
if (ns.size() < 36)

View File

@ -63,7 +63,7 @@ void ServerLobbyRoomProtocol::setup()
bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
{
assert(m_setup); // assert that the setup exists
if (event->type == EVENT_TYPE_MESSAGE)
if (event->getType() == EVENT_TYPE_MESSAGE)
{
NetworkString data = event->data();
assert(data.size()); // message not empty
@ -87,14 +87,14 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
playerReversedVote(event);
else if (message_type == 0xc5) // vote for laps
playerLapsVote(event);
} // if (event->type == EVENT_TYPE_MESSAGE)
else if (event->type == EVENT_TYPE_CONNECTED)
} // if (event->getType() == EVENT_TYPE_MESSAGE)
else if (event->getType() == EVENT_TYPE_CONNECTED)
{
} // if (event->type == EVENT_TYPE_CONNECTED)
else if (event->type == EVENT_TYPE_DISCONNECTED)
} // if (event->getType() == EVENT_TYPE_CONNECTED)
else if (event->getType() == EVENT_TYPE_DISCONNECTED)
{
kartDisconnected(event);
} // if (event->type == EVENT_TYPE_DISCONNECTED)
} // if (event->getType() == EVENT_TYPE_DISCONNECTED)
return true;
}
@ -305,7 +305,7 @@ void ServerLobbyRoomProtocol::checkRaceFinished()
void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
{
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (peer->getPlayerProfile() != NULL) // others knew him
{
NetworkString msg(3);
@ -334,7 +334,7 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
*/
void ServerLobbyRoomProtocol::connectionRequested(Event* event)
{
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
NetworkString data = event->data();
if (data.size() != 5 || data[0] != 4)
{
@ -415,7 +415,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 6))
return;
@ -476,7 +476,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event)
void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 7))
return;
if (!isByteCorrect(event, 5, 1))
@ -507,7 +507,7 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event)
void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer)();
if (!checkDataSizeAndToken(event, 7))
return;
if (!isByteCorrect(event, 5, 1))
@ -538,7 +538,7 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event)
void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 7))
return;
if (!isByteCorrect(event, 5, 1))
@ -569,7 +569,7 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event)
void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 8))
return;
int N = data[5];
@ -602,7 +602,7 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event)
void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))
@ -635,7 +635,7 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event)
void ServerLobbyRoomProtocol::playerLapsVote(Event* event)
{
NetworkString data = event->data();
STKPeer* peer = *(event->peer);
STKPeer* peer = *(event->getPeer());
if (!checkDataSizeAndToken(event, 9))
return;
if (!isByteCorrect(event, 5, 1))

View File

@ -44,7 +44,7 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event)
}
uint32_t token = data.gui32();
uint8_t ready = data.gui8(4);
STKPeer* peer = (*(event->peer));
STKPeer* peer = (*(event->getPeer()));
if (peer->getClientServerToken() != token)
{
Log::error("StartGameProtocol", "Bad token received.");

View File

@ -33,7 +33,7 @@ SynchronizationProtocol::~SynchronizationProtocol()
bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
{
if (event->type != EVENT_TYPE_MESSAGE)
if (event->getType() != EVENT_TYPE_MESSAGE)
return true;
NetworkString data = event->data();
if (data.size() < 10)
@ -61,7 +61,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
uint8_t peer_id = 0;
for (unsigned int i = 0; i < peers.size(); i++)
{
if (peers[i]->isSamePeer(*event->peer))
if (peers[i]->isSamePeer(*event->getPeer()))
{
peer_id = i;
}

View File

@ -179,7 +179,7 @@ void* STKHost::mainLoop(void* self)
while (enet_host_service(host, &event, 20) != 0)
{
Event* stk_event = new Event(&event);
if (stk_event->type == EVENT_TYPE_MESSAGE)
if (stk_event->getType() == EVENT_TYPE_MESSAGE)
logPacket(stk_event->data(), true);
if (event.type != ENET_EVENT_TYPE_NONE)
NetworkManager::getInstance()->notifyEvent(stk_event);

View File

@ -35,7 +35,13 @@
class STKPeer
{
friend class Event;
public:
protected:
ENetPeer* m_peer;
NetworkPlayerProfile** m_player_profile;
uint32_t *m_client_server_token;
bool *m_token_set;
public:
STKPeer();
STKPeer(const STKPeer& peer);
virtual ~STKPeer();
@ -60,11 +66,6 @@ class STKPeer
bool isSamePeer(const STKPeer* peer) const;
protected:
ENetPeer* m_peer;
NetworkPlayerProfile** m_player_profile;
uint32_t *m_client_server_token;
bool *m_token_set;
};
}; // STKPeer
#endif // STK_PEER_HPP