Drafted a basic networking protocol, and hooked a button up to the network broadcast
This commit is contained in:
+21
-11
@@ -20,7 +20,8 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
|
||||
|
||||
//members
|
||||
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.SetY(50);
|
||||
|
||||
@@ -57,10 +58,12 @@ void Lobby::Receive() {
|
||||
while(netUtil->Receive()) {
|
||||
memcpy(&packet, netUtil->GetInData(), sizeof(Packet));
|
||||
|
||||
cout << "receiving" << endl;
|
||||
|
||||
switch(packet.type) {
|
||||
case PacketList::PONG:
|
||||
cout << "dumping..." << endl;
|
||||
cout << packet.pong.buffer << endl;
|
||||
cout << packet.pong.serverName << endl;
|
||||
break;
|
||||
//...
|
||||
}
|
||||
@@ -76,15 +79,17 @@ void Lobby::Render(SDL_Surface* const screen) {
|
||||
//-------------------------
|
||||
|
||||
void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
pingButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
pingButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
if (pingButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
PingNetwork();
|
||||
}
|
||||
}
|
||||
|
||||
void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
@@ -92,15 +97,20 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
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) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//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));
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class Lobby : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
@@ -32,6 +35,10 @@ protected:
|
||||
virtual void KeyDown(SDL_KeyboardEvent const&);
|
||||
virtual void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//utilities
|
||||
void PingNetwork();
|
||||
// void JoinRequest();
|
||||
|
||||
//members
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
|
||||
+14
-7
@@ -33,10 +33,7 @@ Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontS
|
||||
y = j;
|
||||
state = State::NORMAL;
|
||||
|
||||
//graphical stuff
|
||||
image.SetSurface(imageSurface);
|
||||
image.SetClipH(image.GetClipH() / 3); //3 phases
|
||||
font.SetSurface(fontSurface);
|
||||
SetSurfaces(imageSurface, fontSurface);
|
||||
|
||||
SetText(s);
|
||||
}
|
||||
@@ -67,14 +64,24 @@ Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
|
||||
void Button::DrawTo(SDL_Surface* const dest) {
|
||||
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) {
|
||||
//one line
|
||||
text = s;
|
||||
textX = (image.GetClipW() / 2 + x) - (font.GetClipW() * text.size() / 2);
|
||||
textY = (image.GetClipH() / 2 + y) - (font.GetClipH() / 2);
|
||||
textX = (image.GetClipW() / 2) - (font.GetClipW() * text.size() / 2);
|
||||
textY = (image.GetClipH() / 2) - (font.GetClipH() / 2);
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,8 @@ public:
|
||||
Sint16 GetX() const { return x; }
|
||||
Sint16 GetY() const { return y; }
|
||||
|
||||
void SetSurfaces(SDL_Surface* image, SDL_Surface* font);
|
||||
|
||||
std::string SetText(std::string s);
|
||||
std::string GetText() const { return text; }
|
||||
|
||||
|
||||
+30
-25
@@ -5,34 +5,35 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#define PACKET_STRING_SIZE 256
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
enum class PacketList {
|
||||
//networking systems
|
||||
PING, PONG,
|
||||
JOINREQUEST,
|
||||
JOINCONFIRM,
|
||||
NONE,
|
||||
|
||||
//connections
|
||||
PING,
|
||||
PONG,
|
||||
JOINREQUEST,
|
||||
JOINCONFIRM,
|
||||
DISCONNECT,
|
||||
|
||||
//player controls
|
||||
NEWPLAYER,
|
||||
DELETEPLAYER,
|
||||
|
||||
//play updates
|
||||
MOTIONUPDATE,
|
||||
MOVEMENT,
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//networking systems
|
||||
//connections
|
||||
//-------------------------
|
||||
|
||||
struct Ping {
|
||||
PacketList type = PacketList::PING;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
};
|
||||
|
||||
struct Pong {
|
||||
PacketList type = PacketList::PONG;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
char serverName[PACKET_STRING_SIZE];
|
||||
};
|
||||
|
||||
struct JoinRequest {
|
||||
@@ -46,8 +47,12 @@ struct JoinConfirm {
|
||||
int playerID;
|
||||
};
|
||||
|
||||
struct Disconnect {
|
||||
PacketList type = PacketList::DISCONNECT;
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//connections
|
||||
//player controls
|
||||
//-------------------------
|
||||
|
||||
struct NewPlayer {
|
||||
@@ -55,6 +60,7 @@ struct NewPlayer {
|
||||
int playerID;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
Vector2 position;
|
||||
};
|
||||
|
||||
struct DeletePlayer {
|
||||
@@ -62,12 +68,8 @@ struct DeletePlayer {
|
||||
int playerID;
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//play updates
|
||||
//-------------------------
|
||||
|
||||
struct MotionUpdate {
|
||||
PacketList type = PacketList::MOTIONUPDATE;
|
||||
struct Movement {
|
||||
PacketList type = PacketList::MOVEMENT;
|
||||
int playerID;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
@@ -78,19 +80,22 @@ struct MotionUpdate {
|
||||
//-------------------------
|
||||
|
||||
union Packet {
|
||||
Packet() {};
|
||||
~Packet() {};
|
||||
PacketList type;
|
||||
//networking systems
|
||||
Packet () {}
|
||||
~Packet() {}
|
||||
|
||||
PacketList type = PacketList::NONE;
|
||||
|
||||
//connections
|
||||
Ping ping;
|
||||
Pong pong;
|
||||
JoinRequest joinRequest;
|
||||
JoinConfirm joinConfirm;
|
||||
//connections
|
||||
Disconnect disconnect;
|
||||
|
||||
//player controls
|
||||
NewPlayer newPlayer;
|
||||
DeletePlayer deletePlayer;
|
||||
//play updates
|
||||
MotionUpdate motionUpdate;
|
||||
Movement movement;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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
@@ -1,6 +1,6 @@
|
||||
#configuration of the program
|
||||
ip = 127.0.0.1
|
||||
port = 2000
|
||||
server.host = 127.0.0.1
|
||||
server.port = 2000
|
||||
screen.w = 800
|
||||
screen.h = 600
|
||||
screen.f = false
|
||||
|
||||
+31
-14
@@ -10,7 +10,7 @@ void Server::Init() {
|
||||
throw(runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
configUtil.Load("config.cfg");
|
||||
netUtil.Open(configUtil.Integer("port"), sizeof(Packet));
|
||||
netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet));
|
||||
running = true;
|
||||
}
|
||||
|
||||
@@ -39,23 +39,16 @@ void Server::HandleInput() {
|
||||
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet));
|
||||
switch(packet.type) {
|
||||
case PacketList::PING:
|
||||
//respond to pings with the server name
|
||||
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));
|
||||
Ping(&packet);
|
||||
break;
|
||||
case PacketList::JOINREQUEST:
|
||||
//
|
||||
JoinRequest(&packet);
|
||||
break;
|
||||
case PacketList::NEWPLAYER:
|
||||
//
|
||||
case PacketList::DISCONNECT:
|
||||
Disconnect(&packet);
|
||||
break;
|
||||
case PacketList::DELETEPLAYER:
|
||||
//
|
||||
break;
|
||||
case PacketList::MOTIONUPDATE:
|
||||
//
|
||||
case PacketList::MOVEMENT:
|
||||
Movement(&packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -73,3 +66,27 @@ void Server::HandleOutput() {
|
||||
//send all information to new connections
|
||||
//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
@@ -17,10 +17,17 @@ public:
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
private:
|
||||
void HandleInput();
|
||||
void UpdateWorld();
|
||||
void HandleOutput();
|
||||
private:
|
||||
|
||||
//network commands
|
||||
void Ping(Packet*);
|
||||
void JoinRequest(Packet*);
|
||||
void Disconnect(Packet*);
|
||||
void Movement(Packet*);
|
||||
|
||||
bool running = false;
|
||||
Delta delta;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user