Refactored UDPNetworkUtility, and tied it to SerialPacket

This commit is contained in:
Kayne Ruse
2014-05-28 23:22:00 +10:00
parent de7da81102
commit 7b3bf24e5d
12 changed files with 166 additions and 132 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ void ClientApplication::Init(int argc, char** argv) {
if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net"));
}
network.Open(0, PACKET_BUFFER_SIZE);
network.Open(0);
}
void ClientApplication::Proc() {
-2
View File
@@ -24,8 +24,6 @@
//network
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//graphics
#include "image.hpp"
+9 -18
View File
@@ -96,9 +96,7 @@ void InWorld::Update(double delta) {
SerialPacket packet;
//suck in all waiting packets
while(network.Receive()) {
deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address;
while(network.Receive(&packet)) {
HandlePacket(packet);
}
@@ -354,7 +352,6 @@ void InWorld::HandleCharacterDelete(SerialPacket packet) {
void InWorld::RequestSynchronize() {
SerialPacket packet;
char buffer[PACKET_STRING_SIZE];
//request a sync
packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
@@ -362,13 +359,11 @@ void InWorld::RequestSynchronize() {
packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
network.SendTo(Channels::SERVER, &packet);
}
void InWorld::SendPlayerUpdate() {
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the packet
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
@@ -378,47 +373,43 @@ void InWorld::SendPlayerUpdate() {
packet.characterInfo.position = localCharacter->position;
packet.characterInfo.motion = localCharacter->motion;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
network.SendTo(Channels::SERVER, &packet);
}
void InWorld::RequestDisconnect() {
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//send a disconnect request
packet.meta.type = SerialPacket::Type::DISCONNECT;
packet.clientInfo.clientIndex = clientIndex;
packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
network.SendTo(Channels::SERVER, &packet);
}
void InWorld::RequestShutDown() {
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//send a shutdown request
packet.meta.type = SerialPacket::Type::SHUTDOWN;
packet.clientInfo.clientIndex = clientIndex;
packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
network.SendTo(Channels::SERVER, &packet);
}
void InWorld::RequestRegion(int mapIndex, int x, int y) {
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the region's data
packet.meta.type = SerialPacket::Type::REGION_REQUEST;
packet.regionInfo.mapIndex = mapIndex;
packet.regionInfo.x = x;
packet.regionInfo.y = y;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
network.SendTo(Channels::SERVER, &packet);
}
//-------------------------
-2
View File
@@ -29,8 +29,6 @@
//networking
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//graphics
#include "image.hpp"
+6 -16
View File
@@ -86,11 +86,9 @@ void LobbyMenu::FrameStart() {
}
void LobbyMenu::Update(double delta) {
//suck in all waiting packets
//suck in and process all waiting packets
SerialPacket packet;
while(network.Receive()) {
deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address;
while(network.Receive(&packet)) {
HandlePacket(packet);
}
}
@@ -149,14 +147,10 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (search.MouseButtonUp(button) == Button::State::HOVER) {
//the vars
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//broadcast to the network, or a specific server
SerialPacket packet;
packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST;
serialize(&packet, buffer);
network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE);
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
//reset the server list
serverInfo.clear();
@@ -164,19 +158,15 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
}
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
//the vars
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the packet
SerialPacket packet;
packet.meta.type = SerialPacket::Type::JOIN_REQUEST;
strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
//join the selected server
serialize(&packet, buffer);
network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE);
network.SendTo(&selection->address, &packet);
selection = nullptr;
}
-2
View File
@@ -30,8 +30,6 @@
//network
#include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//client
#include "base_scene.hpp"