Implemented the log on and log off systems

This is a pretty straight forward port of the old version, including the
incredibly hacky server list. But I just need to remember that this is a
prototype.
This commit is contained in:
Kayne Ruse
2013-11-23 17:53:36 +11:00
parent ca86dc5fb8
commit 6ccc874583
11 changed files with 220 additions and 68 deletions
+8
View File
@@ -0,0 +1,8 @@
#ifndef CHANNELS_HPP_
#define CHANNELS_HPP_
enum Channels {
SERVER = 0
};
#endif
+2 -2
View File
@@ -135,10 +135,10 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
activeScene = new OptionsMenu(&config); activeScene = new OptionsMenu(&config);
break; break;
case SceneList::LOBBYMENU: case SceneList::LOBBYMENU:
activeScene = new LobbyMenu(&config, &network); activeScene = new LobbyMenu(&config, &network, &clientIndex);
break; break;
case SceneList::INWORLD: case SceneList::INWORLD:
activeScene = new InWorld(); activeScene = new InWorld(&config, &network, &clientIndex);
break; break;
case SceneList::INCOMBAT: case SceneList::INCOMBAT:
activeScene = new InCombat(); activeScene = new InCombat();
+1
View File
@@ -51,6 +51,7 @@ private:
ConfigUtility config; ConfigUtility config;
UDPNetworkUtility network; UDPNetworkUtility network;
int clientIndex = -1; //replace with a struct?
}; };
#endif #endif
+78 -7
View File
@@ -21,12 +21,39 @@
*/ */
#include "in_world.hpp" #include "in_world.hpp"
#include "channels.hpp"
#include <stdexcept>
//------------------------- //-------------------------
//Public access members //Public access members
//------------------------- //-------------------------
InWorld::InWorld() { InWorld::InWorld(ConfigUtility* const arg1, UDPNetworkUtility* const arg2, int* const arg3):
// config(*arg1),
network(*arg2),
clientIndex(*arg3)
{
//setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
//pass the utility objects
disconnectButton.SetImage(&image);
disconnectButton.SetFont(&font);
shutDownButton.SetImage(&image);
shutDownButton.SetFont(&font);
//set the button positions
disconnectButton.SetX(50);
disconnectButton.SetY(50 + image.GetClipH() * 0);
shutDownButton.SetX(50);
shutDownButton.SetY(50 + image.GetClipH() * 1);
//set the button texts
disconnectButton.SetText("Disconnect");
shutDownButton.SetText("Shut Down");
} }
InWorld::~InWorld() { InWorld::~InWorld() {
@@ -42,7 +69,13 @@ void InWorld::FrameStart() {
} }
void InWorld::Update(double delta) { void InWorld::Update(double delta) {
// //suck in all waiting packets
NetworkPacket packet;
while(network.Receive()) {
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket));
packet.meta.srcAddress = network.GetInPacket()->address;
HandlePacket(packet);
}
} }
void InWorld::FrameEnd() { void InWorld::FrameEnd() {
@@ -50,7 +83,8 @@ void InWorld::FrameEnd() {
} }
void InWorld::Render(SDL_Surface* const screen) { void InWorld::Render(SDL_Surface* const screen) {
// disconnectButton.DrawTo(screen);
shutDownButton.DrawTo(screen);
} }
//------------------------- //-------------------------
@@ -58,20 +92,39 @@ void InWorld::Render(SDL_Surface* const screen) {
//------------------------- //-------------------------
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) { void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
// disconnectButton.MouseMotion(motion);
shutDownButton.MouseMotion(motion);
} }
void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) { void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
// disconnectButton.MouseButtonDown(button);
shutDownButton.MouseButtonDown(button);
} }
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) { void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
// if (disconnectButton.MouseButtonUp(button) == Button::State::HOVER) {
//send a disconnect request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
}
if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) {
//send a shutdown request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
}
} }
void InWorld::KeyDown(SDL_KeyboardEvent const& key) { void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) { switch(key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
//send a disconnect request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
QuitEvent(); QuitEvent();
break; break;
} }
@@ -80,3 +133,21 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
void InWorld::KeyUp(SDL_KeyboardEvent const& key) { void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
// //
} }
void InWorld::HandlePacket(NetworkPacket packet) {
switch(packet.meta.type) {
case NetworkPacket::Type::DISCONNECT:
network.Unbind(Channels::SERVER);
clientIndex = -1;
SetNextScene(SceneList::MAINMENU);
break;
case NetworkPacket::Type::SYNCHRONIZE:
//TODO
break;
//handle errors
default:
throw(std::runtime_error("Unknown NetworkPacket::Type encountered"));
break;
}
}
+21 -1
View File
@@ -24,10 +24,17 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "network_packet.hpp"
#include "image.hpp"
#include "raster_font.hpp"
#include "button.hpp"
class InWorld : public BaseScene { class InWorld : public BaseScene {
public: public:
//Public access members //Public access members
InWorld(); InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const);
~InWorld(); ~InWorld();
protected: protected:
@@ -43,6 +50,19 @@ protected:
void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
void HandlePacket(NetworkPacket);
//global
ConfigUtility& config;
UDPNetworkUtility& network;
int& clientIndex;
//members
Image image;
RasterFont font;
Button disconnectButton;
Button shutDownButton;
}; };
#endif #endif
+42 -8
View File
@@ -21,15 +21,18 @@
*/ */
#include "lobby_menu.hpp" #include "lobby_menu.hpp"
#include "channels.hpp"
#include <stdexcept> #include <stdexcept>
//------------------------- //-------------------------
//Public access members //Public access members
//------------------------- //-------------------------
LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2): LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2, int* const arg3):
config(*arg1), config(*arg1),
network(*arg2) network(*arg2),
clientIndex(*arg3)
{ {
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
@@ -56,6 +59,9 @@ LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2):
search.SetText("Search"); search.SetText("Search");
join.SetText("Join"); join.SetText("Join");
back.SetText("Back"); back.SetText("Back");
//set the server list's position
listBox = {300, 50, 200, font.GetCharH()};
} }
LobbyMenu::~LobbyMenu() { LobbyMenu::~LobbyMenu() {
@@ -89,7 +95,15 @@ void LobbyMenu::Render(SDL_Surface* const screen) {
join.DrawTo(screen); join.DrawTo(screen);
back.DrawTo(screen); back.DrawTo(screen);
for (int i = 0; i < serverInfo.size(); i++) { for (int i = 0; i < serverInfo.size(); i++) {
font.DrawStringTo(serverInfo[i].name, screen, 300, 50 + i*font.GetCharH()); //draw the selected server's highlight
if (selection == &serverInfo[i]) {
SDL_Rect r = listBox;
r.y += i * listBox.h;
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39));
}
//draw the server name
font.DrawStringTo(serverInfo[i].name, screen, listBox.x, listBox.y + i*listBox.h);
} }
} }
@@ -118,15 +132,33 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//reset the server list //reset the server list
serverInfo.clear(); serverInfo.clear();
selection = nullptr;
} }
if (join.MouseButtonUp(button) == Button::State::HOVER) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) {
//TODO: join the selected server //join the selected server
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST;
network.Send(&selection->address, &packet, sizeof(NetworkPacket));
selection = nullptr;
} }
if (back.MouseButtonUp(button) == Button::State::HOVER) { else if (back.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
else if (
//has the user selected a server on the list?
button.x > listBox.x &&
button.x < listBox.x + listBox.w &&
button.y > listBox.y &&
button.y < listBox.y + listBox.h * serverInfo.size()
) {
selection = &serverInfo[(button.y - listBox.y)/listBox.h];
}
else {
selection = nullptr;
}
} }
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
@@ -144,14 +176,16 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
void LobbyMenu::HandlePacket(NetworkPacket packet) { void LobbyMenu::HandlePacket(NetworkPacket packet) {
switch(packet.meta.type) { switch(packet.meta.type) {
case NetworkPacket::Type::BROADCAST_RESPONSE: { case NetworkPacket::Type::BROADCAST_RESPONSE: {
ServerInfo server; ServerInformation server;
server.name = packet.serverInfo.name; server.name = packet.serverInfo.name;
server.address = packet.meta.srcAddress; server.address = packet.meta.srcAddress;
serverInfo.push_back(server); serverInfo.push_back(server);
} }
break; break;
case NetworkPacket::Type::JOIN_RESPONSE: case NetworkPacket::Type::JOIN_RESPONSE:
// clientIndex = packet.clientInfo.index;
network.Bind(&packet.meta.srcAddress, Channels::SERVER);
SetNextScene(SceneList::INWORLD);
break; break;
//handle errors //handle errors
+10 -3
View File
@@ -37,7 +37,7 @@
class LobbyMenu : public BaseScene { class LobbyMenu : public BaseScene {
public: public:
//Public access members //Public access members
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const); LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const);
~LobbyMenu(); ~LobbyMenu();
protected: protected:
@@ -59,6 +59,7 @@ protected:
//global //global
ConfigUtility& config; ConfigUtility& config;
UDPNetworkUtility& network; UDPNetworkUtility& network;
int& clientIndex;
//members //members
Image image; Image image;
@@ -67,12 +68,18 @@ protected:
Button join; Button join;
Button back; Button back;
struct ServerInfo { //server list
struct ServerInformation {
std::string name; std::string name;
IPaddress address; IPaddress address;
}; };
std::vector<ServerInfo> serverInfo; std::vector<ServerInformation> serverInfo;
//a terrible hack, forgive me
//I'd love a proper gui system for this
SDL_Rect listBox;
ServerInformation* selection = nullptr;
}; };
#endif #endif
+9
View File
@@ -52,6 +52,9 @@ union NetworkPacket {
//mass update //mass update
SYNCHRONIZE = 8, SYNCHRONIZE = 8,
//shut down the server
SHUTDOWN = 9,
//Player movement, etc. //Player movement, etc.
}; };
@@ -69,6 +72,12 @@ union NetworkPacket {
//TODO: player count //TODO: player count
}serverInfo; }serverInfo;
//information about the client
struct ClientInformation {
Metadata meta;
int index;
}clientInfo;
//defaults //defaults
NetworkPacket() { NetworkPacket() {
meta.type = Type::NONE; meta.type = Type::NONE;
+1 -42
View File
@@ -42,45 +42,4 @@ int main(int argc, char** argv) {
} }
cout << "Clean exit" << endl; cout << "Clean exit" << endl;
return 0; return 0;
} }
/*/
#include "thread_safe_queue.hpp"
#include "SDL/SDL.h"
#include <iostream>
using namespace std;
struct Object {
int value = 0;
};
int func(void* arg) {
ThreadSafeQueue<Object>& queue = *reinterpret_cast<ThreadSafeQueue<Object>*>(arg);
while(1) {
Object o = queue.PopFront();
if (o.value != 0) {
cout << o.value;
SDL_Delay(500);
cout << endl;
}
}
}
int main(int, char**) {
ThreadSafeQueue<Object> queue;
SDL_Thread* thread1 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
SDL_Thread* thread2 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
SDL_Thread* thread3 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
while(1) {
SDL_Delay(1000);
Object o;
o.value = 3;
queue.PushBack(o);
}
return 0;
}
//*/
+37 -5
View File
@@ -34,7 +34,7 @@ using namespace std;
//Declarations //Declarations
//------------------------- //-------------------------
//int ServerApplication::ClientEntry::indexCounter = 0; int ClientInformation::counter = 0;
//------------------------- //-------------------------
//Define the network thread //Define the network thread
@@ -164,14 +164,46 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
networkUtil.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket)); networkUtil.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket));
break; break;
case NetworkPacket::Type::JOIN_REQUEST: case NetworkPacket::Type::JOIN_REQUEST: {
// //TODO: prevent duplicate logins from the same address?
//create the new client, filling it with the correct info
ClientInformation newClient;
newClient.index = ClientInformation::counter++;
newClient.address = packet.meta.srcAddress;
//push the new client
clientInfo[newClient.index] = newClient;
//send the client their info
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = newClient.index;
networkUtil.Send(&newClient.address, &packet, sizeof(NetworkPacket));
cout << "connect, total: " << clientInfo.size() << endl;
}
break; break;
case NetworkPacket::Type::DISCONNECT: case NetworkPacket::Type::DISCONNECT:
// //disconnect the specified client
networkUtil.Send(&clientInfo[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
clientInfo.erase(packet.clientInfo.index);
cout << "disconnect, total: " << clientInfo.size() << endl;
break; break;
case NetworkPacket::Type::SYNCHRONIZE: case NetworkPacket::Type::SYNCHRONIZE:
// //TODO
break;
case NetworkPacket::Type::SHUTDOWN:
//end the server
running = false;
//disconnect all clients
packet.meta.type = NetworkPacket::Type::DISCONNECT;
for (auto& it : clientInfo) {
networkUtil.Send(&it.second.address, &packet, sizeof(NetworkPacket));
}
cout << "shutting down" << endl;
break; break;
//handle errors //handle errors
+11
View File
@@ -36,6 +36,15 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "world_room.hpp" #include "world_room.hpp"
#include <map>
//hold the client info
struct ClientInformation {
static int counter;
int index;
IPaddress address;
};
//The main application class //The main application class
class ServerApplication { class ServerApplication {
public: public:
@@ -63,6 +72,8 @@ private:
bool running = true; bool running = true;
ConfigUtility config; ConfigUtility config;
WorldRoom worldRoom; WorldRoom worldRoom;
std::map<int, ClientInformation> clientInfo;
}; };
#endif #endif