Skip to content

Commit

Permalink
More integration #4 #10
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 21, 2019
1 parent 19dfa36 commit 060fbc5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void Context::send(const Uint8 code, const std::string & message) {
network.send(code, message);
}

void Context::setServerIP(const Uint32 ip) {
network.setServerInfo(ip);
}

/**
* Create and replace the current Context in the application
* Closely related to the Factory design pattern
Expand Down
1 change: 1 addition & 0 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Context {
void addCenteredWidget(gcn::Widget *widget);
void setNextContext(const ContextName newContext);
void send(const Uint8 code, const std::string & message);
void setServerIP(const Uint32 ip);

private:
static gcn::Container *parent;
Expand Down
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "settings.hpp"
#include "sound.hpp"
#include "utils.hpp"
#include "network.hpp"
#include "context.hpp"
#include "views/menu.hpp"

Expand Down Expand Up @@ -155,8 +154,6 @@ int main(int argc, char* argv[]) {

SDL_setFramerate(&fpsMgr, 30);

NetworkManager network;

Context::setParent(&top);
currentContext = Context::getNextContext(ContextName::MAIN_MENU);

Expand Down Expand Up @@ -211,12 +208,13 @@ void loop(gcn::SDLInput &input) {
SDL_Event event;

for (;;) {
// Change context if necessary
ContextName next = currentContext->getNextContextName();
if (next != currentContext->getName()) {
currentContext = Context::getNextContext(next);
}

// do something
// handle user events
while (SDL_PollEvent(&event) == 1) {
if (event.type == SDL_QUIT)
return;
Expand All @@ -225,6 +223,8 @@ void loop(gcn::SDLInput &input) {

input.pushInput(event);
}
// Handle received messages
currentContext->receive();

gui->logic();

Expand Down
17 changes: 8 additions & 9 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,19 @@ void NetworkManager::setServerInfo(Uint32 ip) {
SDLNet_Write16(SERVER_PORT, &serverInfo.port);
}

/**
* Extract message code from a packet
* \param p packet to handle
*/
Uint8 NetworkManager::getCode(UDPpacket *p) {
return p->data[0];
}

/**
* Extract payload from a packet
* \param p packet to handle
*/
string NetworkManager::getMessage(UDPpacket *p) {
string message(reinterpret_cast<char*>(p->data+1));
return message;
}

/**
* Send the broadcast message to try to find a server on the local network
* \param ip server address (IPv4), defaults to broadcast if none supplied
*/
void NetworkManager::findServer(Uint32 ip) {
setServerInfo(ip);
send(HELLO_MSG, "HELLO");
}
1 change: 0 additions & 1 deletion src/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class NetworkManager final {
NetworkManager();
~NetworkManager();
void send(Uint8 code, const std::string & message);
void findServer(Uint32 ip = INADDR_BROADCAST);
void setServerInfo(Uint32 ip);
std::vector<UDPpacket*> & receive();

Expand Down
1 change: 0 additions & 1 deletion src/ui/serverlistelement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

struct ServerInfo {
Uint32 ip;
Uint16 port;
std::string name;
Uint8 levelMin;
Uint8 levelMax;
Expand Down
16 changes: 16 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ namespace linbound {
return result;
}

vector<string> split(const string & str, const char delim) {
vector<string> result;
size_t currentIndex = 0;
size_t end = str.find(delim);

while (end != string::npos) {
result.push_back(str.substr(currentIndex, end - currentIndex));

currentIndex = end + 1;
end = str.find(delim, currentIndex);
}
result.push_back(str.substr(currentIndex));

return result;
}

}
2 changes: 2 additions & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <SDL2/SDL.h>
#include <string>
#include <vector>
#include <guisan.hpp>

#include "config.hpp"
Expand All @@ -32,6 +33,7 @@ namespace linbound {
int getVersion();
std::string prettifyIP(Uint32 address);
Uint32 stringToIP(const std::string &input);
std::vector<std::string> split(const std::string &str, const char delim);
}

#endif
40 changes: 26 additions & 14 deletions src/views/serverlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include <SDL2/SDL_image.h>
#include <SDL2/SDL_net.h>
#include "../protocol.hpp"
#include "../utils.hpp"
#include "serverlist.hpp"
Expand Down Expand Up @@ -59,7 +58,7 @@ void ServerList::action(const gcn::ActionEvent & actionEvent) {
input_ip.setVisible(true);
}
else if (actionEvent.getId() == "rescan") {
scanNetwork();
sendRequest();
}
else if (actionEvent.getId() == "connect") {
currentIP = linbound::stringToIP(input_ip.getText());
Expand Down Expand Up @@ -101,6 +100,7 @@ void ServerList::processMessage(const Uint8 code, const string & message) {
switch (code) {
case SERVER_INFO:
// Analyze message and create button, store in vector
serversFound.push_back(makeInfo(message));
break;
case LOGIN_MSG: {
// Analyze message, warn of errors
Expand Down Expand Up @@ -135,31 +135,29 @@ void ServerList::addWidgets() {
addCenteredWidget(&input_ip);
}

/**
* Send a broadcast message to find other servers
*/
void ServerList::scanNetwork() {
// TBD

state = State::RECEIVING_BC;
lastChangeTime = SDL_GetTicks();
}

/**
* Try to see if a server exists at a given IP
* \param ip IPv4 as an Uint32, fetched from the user
*/
void ServerList::sendRequest(Uint32 ip) {
// Create message
// Set IP
setServerIP(ip);

// Send it
send(HELLO_MSG, "HELLO");

state = State::RECEIVING_IP;
if (ip == INADDR_BROADCAST) {
state = State::RECEIVING_BC;
}
else {
state = State::RECEIVING_IP;
}
lastChangeTime = SDL_GetTicks();
}

void ServerList::login(Uint32 ip, const string & login, const string & password) {
// Set IP in NetworkManager
setServerIP(ip);

string message = login + '\3' + password; // yup, in clear

Expand All @@ -169,3 +167,17 @@ void ServerList::login(Uint32 ip, const string & login, const string & password)
state = State::LOGIN;
lastChangeTime = SDL_GetTicks();
}

ServerInfo ServerList::makeInfo(const std::string & message) {
ServerInfo result;
vector<string> pieces = linbound::split(message, '\3');

if (pieces.size() >= 4) {
result.name = pieces[0];
result.levelMin = static_cast<Uint8>(pieces[1].at(0));
result.levelMax = static_cast<Uint8>(pieces[2].at(0));
result.busy = static_cast<Uint8>(pieces[3].at(0));
}

return result;
}
5 changes: 3 additions & 2 deletions src/views/serverlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>
#include <guisan.hpp>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#include "../context.hpp"
#include "../ui/loginwindow.hpp"
#include "../ui/serverlistelement.hpp"
Expand Down Expand Up @@ -63,9 +64,9 @@ class ServerList : public Context, public gcn::ActionListener {
std::vector<ServerInfo> serversFound;

void addWidgets();
void scanNetwork();
void sendRequest(Uint32 ip);
void sendRequest(Uint32 ip = INADDR_BROADCAST);
void login(Uint32 ip, const std::string &login, const std::string &password);
ServerInfo makeInfo(const std::string &message);
};

#endif // !_H_SERVERLIST_

0 comments on commit 060fbc5

Please sign in to comment.