Drafted a basic networking protocol, and hooked a button up to the network broadcast

This commit is contained in:
Kayne Ruse
2013-05-20 04:49:06 +10:00
parent 7866f46ed5
commit d5409d2006
9 changed files with 127 additions and 60 deletions
+21 -11
View File
@@ -20,7 +20,8 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
//members //members
font.SetSurface(surfaceMgr->Get("font")); font.SetSurface(surfaceMgr->Get("font"));
pingButton.GetImage()->SetSurface(surfaceMgr->Get("button")); pingButton.SetSurfaces(surfaceMgr->Get("button"), surfaceMgr->Get("font"));
pingButton.SetText("Refresh");
pingButton.SetX(50); pingButton.SetX(50);
pingButton.SetY(50); pingButton.SetY(50);
@@ -57,10 +58,12 @@ void Lobby::Receive() {
while(netUtil->Receive()) { while(netUtil->Receive()) {
memcpy(&packet, netUtil->GetInData(), sizeof(Packet)); memcpy(&packet, netUtil->GetInData(), sizeof(Packet));
cout << "receiving" << endl;
switch(packet.type) { switch(packet.type) {
case PacketList::PONG: case PacketList::PONG:
cout << "dumping..." << endl; cout << "dumping..." << endl;
cout << packet.pong.buffer << endl; cout << packet.pong.serverName << endl;
break; break;
//... //...
} }
@@ -76,15 +79,17 @@ void Lobby::Render(SDL_Surface* const screen) {
//------------------------- //-------------------------
void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) { void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) {
// pingButton.MouseMotion(motion);
} }
void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) { void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) {
// pingButton.MouseButtonDown(button);
} }
void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) { void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
// if (pingButton.MouseButtonUp(button) == Button::State::HOVER) {
PingNetwork();
}
} }
void Lobby::KeyDown(SDL_KeyboardEvent const& key) { void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
@@ -92,15 +97,20 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
QuitEvent(); QuitEvent();
break; break;
case SDLK_SPACE:
//debugging
Packet packet;
packet.type = PacketList::PING;
netUtil->Send("127.0.0.1", 2000, reinterpret_cast<void*>(&packet), sizeof(Packet));
break;
} }
} }
void Lobby::KeyUp(SDL_KeyboardEvent const& key) { void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
// //
} }
//-------------------------
//Utilities
//-------------------------
void Lobby::PingNetwork() {
//ing the network
Packet packet;
packet.type = PacketList::PING;
netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast<void*>(&packet), sizeof(Packet));
}
+7
View File
@@ -11,6 +11,9 @@
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
#include <vector>
#include <string>
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
@@ -32,6 +35,10 @@ protected:
virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//utilities
void PingNetwork();
// void JoinRequest();
//members //members
ConfigUtility* configUtil = nullptr; ConfigUtility* configUtil = nullptr;
SurfaceManager* surfaceMgr = nullptr; SurfaceManager* surfaceMgr = nullptr;
+14 -7
View File
@@ -33,10 +33,7 @@ Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontS
y = j; y = j;
state = State::NORMAL; state = State::NORMAL;
//graphical stuff SetSurfaces(imageSurface, fontSurface);
image.SetSurface(imageSurface);
image.SetClipH(image.GetClipH() / 3); //3 phases
font.SetSurface(fontSurface);
SetText(s); SetText(s);
} }
@@ -67,14 +64,24 @@ Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
void Button::DrawTo(SDL_Surface* const dest) { void Button::DrawTo(SDL_Surface* const dest) {
image.DrawTo(dest, x, y); image.DrawTo(dest, x, y);
font.DrawStringTo(text, dest, textX, textY); font.DrawStringTo(text, dest, textX + x, textY + y);
}
void Button::SetSurfaces(SDL_Surface* imageSurface, SDL_Surface* fontSurface) {
//graphical stuff
image.SetSurface(imageSurface);
image.SetClipH(image.GetClipH() / 3); //3 phases
font.SetSurface(fontSurface);
//reset textX & textY
SetText(text);
} }
std::string Button::SetText(std::string s) { std::string Button::SetText(std::string s) {
//one line //one line
text = s; text = s;
textX = (image.GetClipW() / 2 + x) - (font.GetClipW() * text.size() / 2); textX = (image.GetClipW() / 2) - (font.GetClipW() * text.size() / 2);
textY = (image.GetClipH() / 2 + y) - (font.GetClipH() / 2); textY = (image.GetClipH() / 2) - (font.GetClipH() / 2);
return text; return text;
} }
+2
View File
@@ -54,6 +54,8 @@ public:
Sint16 GetX() const { return x; } Sint16 GetX() const { return x; }
Sint16 GetY() const { return y; } Sint16 GetY() const { return y; }
void SetSurfaces(SDL_Surface* image, SDL_Surface* font);
std::string SetText(std::string s); std::string SetText(std::string s);
std::string GetText() const { return text; } std::string GetText() const { return text; }
+30 -25
View File
@@ -5,34 +5,35 @@
#include <string> #include <string>
#define PACKET_STRING_SIZE 256 #define PACKET_STRING_SIZE 100
enum class PacketList { enum class PacketList {
//networking systems NONE,
PING, PONG,
JOINREQUEST,
JOINCONFIRM,
//connections //connections
PING,
PONG,
JOINREQUEST,
JOINCONFIRM,
DISCONNECT,
//player controls
NEWPLAYER, NEWPLAYER,
DELETEPLAYER, DELETEPLAYER,
MOVEMENT,
//play updates
MOTIONUPDATE,
}; };
//------------------------- //-------------------------
//networking systems //connections
//------------------------- //-------------------------
struct Ping { struct Ping {
PacketList type = PacketList::PING; PacketList type = PacketList::PING;
char buffer[PACKET_STRING_SIZE];
}; };
struct Pong { struct Pong {
PacketList type = PacketList::PONG; PacketList type = PacketList::PONG;
char buffer[PACKET_STRING_SIZE]; char serverName[PACKET_STRING_SIZE];
}; };
struct JoinRequest { struct JoinRequest {
@@ -46,8 +47,12 @@ struct JoinConfirm {
int playerID; int playerID;
}; };
struct Disconnect {
PacketList type = PacketList::DISCONNECT;
};
//------------------------- //-------------------------
//connections //player controls
//------------------------- //-------------------------
struct NewPlayer { struct NewPlayer {
@@ -55,6 +60,7 @@ struct NewPlayer {
int playerID; int playerID;
char handle[PACKET_STRING_SIZE]; char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE];
Vector2 position;
}; };
struct DeletePlayer { struct DeletePlayer {
@@ -62,12 +68,8 @@ struct DeletePlayer {
int playerID; int playerID;
}; };
//------------------------- struct Movement {
//play updates PacketList type = PacketList::MOVEMENT;
//-------------------------
struct MotionUpdate {
PacketList type = PacketList::MOTIONUPDATE;
int playerID; int playerID;
Vector2 position; Vector2 position;
Vector2 motion; Vector2 motion;
@@ -78,19 +80,22 @@ struct MotionUpdate {
//------------------------- //-------------------------
union Packet { union Packet {
Packet() {}; Packet () {}
~Packet() {}; ~Packet() {}
PacketList type;
//networking systems PacketList type = PacketList::NONE;
//connections
Ping ping; Ping ping;
Pong pong; Pong pong;
JoinRequest joinRequest; JoinRequest joinRequest;
JoinConfirm joinConfirm; JoinConfirm joinConfirm;
//connections Disconnect disconnect;
//player controls
NewPlayer newPlayer; NewPlayer newPlayer;
DeletePlayer deletePlayer; DeletePlayer deletePlayer;
//play updates Movement movement;
MotionUpdate motionUpdate;
}; };
#endif #endif
+12
View File
@@ -78,3 +78,15 @@ end
------------------------- -------------------------
Networking protocol:
//connections
ping - ping the server
pong - a response to a ping, carries the server name
join request - from client to server, this is the initial contact the client makes. it carries all of the client's information, like the handle, avatar, etc.
join confirm - the response to a join request, this makes the client enter the game proper, and carries the client's playerID.
disconnect - from either the client or server, his officially ends communications between the two programs
//player controls
new player - a new player enters the world. carries all player info
delete player - a player leaves the world, carries the player ID
movement - this is the initial position and motion of a player in the world. it is relayed to all clients, and progresses using delta progression
+2 -2
View File
@@ -1,6 +1,6 @@
#configuration of the program #configuration of the program
ip = 127.0.0.1 server.host = 127.0.0.1
port = 2000 server.port = 2000
screen.w = 800 screen.w = 800
screen.h = 600 screen.h = 600
screen.f = false screen.f = false
+31 -14
View File
@@ -10,7 +10,7 @@ void Server::Init() {
throw(runtime_error("Failed to initialize SDL_net")); throw(runtime_error("Failed to initialize SDL_net"));
} }
configUtil.Load("config.cfg"); configUtil.Load("config.cfg");
netUtil.Open(configUtil.Integer("port"), sizeof(Packet)); netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet));
running = true; running = true;
} }
@@ -39,23 +39,16 @@ void Server::HandleInput() {
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet)); memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet));
switch(packet.type) { switch(packet.type) {
case PacketList::PING: case PacketList::PING:
//respond to pings with the server name Ping(&packet);
cout << "responding to ping..." << endl;
packet.type = PacketList::PONG;
sprintf(packet.pong.buffer, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(&packet), sizeof(Packet));
break; break;
case PacketList::JOINREQUEST: case PacketList::JOINREQUEST:
// JoinRequest(&packet);
break; break;
case PacketList::NEWPLAYER: case PacketList::DISCONNECT:
// Disconnect(&packet);
break; break;
case PacketList::DELETEPLAYER: case PacketList::MOVEMENT:
// Movement(&packet);
break;
case PacketList::MOTIONUPDATE:
//
break; break;
} }
} }
@@ -73,3 +66,27 @@ void Server::HandleOutput() {
//send all information to new connections //send all information to new connections
//selective updates to existing connectons //selective updates to existing connectons
} }
//-------------------------
//network commands
//-------------------------
void Server::Ping(Packet* packet) {
//respond to pings with the server name
cout << "responding to ping..." << endl;
packet->type = PacketList::PONG;
sprintf(packet->pong.serverName, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(Packet));
}
void Server::JoinRequest(Packet* packet) {
//
}
void Server::Disconnect(Packet* packet) {
//
}
void Server::Movement(Packet* packet) {
//
}
+8 -1
View File
@@ -17,10 +17,17 @@ public:
void Proc(); void Proc();
void Quit(); void Quit();
private:
void HandleInput(); void HandleInput();
void UpdateWorld(); void UpdateWorld();
void HandleOutput(); void HandleOutput();
private:
//network commands
void Ping(Packet*);
void JoinRequest(Packet*);
void Disconnect(Packet*);
void Movement(Packet*);
bool running = false; bool running = false;
Delta delta; Delta delta;