Skip to content

Commit

Permalink
Compilation issues #4
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 19, 2019
1 parent badfd6d commit 5dd9021
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "settings.hpp"
#include "sound.hpp"
#include "utils.hpp"
#include "network.hpp"
#include "context.hpp"
#include "views/menu.hpp"

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

SDL_setFramerate(&fpsMgr, 30);

NetworkManager network;

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

Expand Down
16 changes: 10 additions & 6 deletions src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ NetworkManager::NetworkManager() {
if(init == 0) {
clientSock = SDLNet_UDP_Open(0); // pick the pork you want
}

serverInfo.host = INADDR_ANY;
serverInfo.port = 0;
}

NetworkManager::~NetworkManager() {
Expand All @@ -26,13 +29,13 @@ NetworkManager::~NetworkManager() {
}

void NetworkManager::send(Uint8 code, const std::string & message) {
int dataSize = message.size() + 1;
int dataSize = static_cast<int>(message.size()) + 1 + 1;
UDPpacket *packet = SDLNet_AllocPacket(dataSize);

packet->len = dataSize;
packet->address = serverInfo;
packet->data[0] = code;
packet->data[1] = message.c_str();
strncpy((char*)packet->data+1, message.c_str(), message.size() + 1);

SDLNet_UDP_Send(clientSock, -1, packet);

Expand All @@ -46,7 +49,7 @@ void NetworkManager::receive(Context* currentContext) {
while(nbRecv == 1) {
if(packet->len > 1) {
Uint8 code = packet->data[0];
string data(packet->data[1]);
string data((char*)packet->data+1);

// Based on the code, do something with the packet...
}
Expand Down Expand Up @@ -76,12 +79,13 @@ void NetworkManager::findServer() {
broadcast.host = INADDR_BROADCAST;
SDLNet_Write16(SERVER_PORT, &broadcast.port);

UDPpacket *packet = SDLNet_AllocPacket(5 + 1);
int size = static_cast<int>(msg.size()) + 1 + 1;
UDPpacket *packet = SDLNet_AllocPacket(size);

packet->len = 5 + 1;
packet->len = size;
packet->address = broadcast;
packet->data[0] = HELLO_MSG;
packet->data[1] = msg.c_str();
strncpy((char*)packet->data + 1, msg.c_str(), 6);

SDLNet_UDP_Send(clientSock, -1, packet);

Expand Down
2 changes: 1 addition & 1 deletion src/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NetworkManager final {
private:
const Uint16 SERVER_PORT = 6545;

UDPSocket clientSock; //! to send data
UDPsocket clientSock; //! to send data
IPaddress serverInfo;
};

Expand Down
10 changes: 0 additions & 10 deletions src/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#ifndef _H_PROTOCOL_
#define _H_PROTOCOL_

#include <SDL2/SDL.h>

/******* Message types *******/
const Uint8 STATUS_INTERNAL = 1; //! Used for server-side errors...
const Uint8 STATUS_OK = 0; //! Like 200 for HTTP
Expand Down Expand Up @@ -87,12 +85,4 @@ const Uint8 RESULTS_MSG = 150; //Results of the battle : wins/loses, rewards
const Uint8 LB_PROTOCOL_VERSION = 1; //! Used to denote incompatibilities between versions
const Uint8 HELLO_MSG = 123; //! 1, 2, 3, because it's that easy

/******* Special senders/recipients *******/
const char* CHANNEL = "ch";
const char* SERVER = "sr";
const char* CLIENT = "cl";
const char* TEAM = "te";
const char* ROOM = "ro";


#endif /* _H_PROTOCOL_ */

0 comments on commit 5dd9021

Please sign in to comment.