Skip to content

Commit

Permalink
Drafted network manager #4 [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 19, 2019
1 parent 3689e64 commit 926fd6c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Build Status](https://travis-ci.org/gbaudic/linbound2.svg?branch=master)](https://travis-ci.org/gbaudic/linbound2)
[![Appveyor Build status](https://ci.appveyor.com/api/projects/status/l7s64j4n2iv2cf2j?svg=true)](https://ci.appveyor.com/project/gbaudic/linbound2)
[![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=gbaudic-github%3Alinbound2&metric=alert_status)](https://sonarcloud.io/dashboard?id=gbaudic-github%3Alinbound2)
[![Licence](https://img.shields.io/github/license/gbaudic/linbound2.svg)](/blob/master/LICENSE)

LinBound aims to be an open-source clone of the game [GunBound](http://gunbound.softnyx.net), a multiplayer turn-based arcade game with a gameplay very close to Worms, Hedgewars or WarMUX. It is primarily targeted to Linux but it aims to be as cross-platform as possible.
This repository is an attempt to start again implementation from a clean state, using more state-of-the-art software engineering and coding practices.
Expand Down
89 changes: 89 additions & 0 deletions src/network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”,
* as defined by the Mozilla Public License, v. 2.0.
*/

#include "network.hpp"
#include "protocol.hpp"
#include "utils.hpp"
using namespace std;

NetworkManager::NetworkManager() {
int init = SDLNet_Init();

if(init == 0) {
clientSock = SDLNet_UDP_Open(0); // pick the pork you want
}
}

NetworkManager::~NetworkManager() {
SDLNet_UDP_Close(clientSock);

SDLNet_Quit();
}

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

packet->len = dataSize;
packet->address = serverInfo;
packet->data[0] = code;
packet->data[1] = message.c_str();

SDLNet_UDP_Send(clientSock, -1, packet);

SDLNet_FreePacket(packet);
}

void NetworkManager::receive(Context* currentContext) {
UDPpacket *packet = SDLNet_AllocPacket(1024);

int nbRecv = SDLNet_UDP_Recv(clientSock, packet);
while(nbRecv == 1) {
if(packet->len > 1) {
Uint8 code = packet->data[0];
string data(packet->data[1]);

// Based on the code, do something with the packet...
}

nbRecv = SDLNet_UDP_Recv(clientSock, packet);
}

SDLNet_FreePacket(packet);
}

/**
* Save server info for future use once chosen by the user
* \param ip server IPv4, in machine byte order
* \param port server port, in machine byte order
*/
void NetworkManager::setServerInfo(Uint32 ip, Uint16 port) {
SDLNet_Write32(ip, &serverInfo.host);
SDLNet_Write16(port, &serverInfo.port);
}

/**
* Send the broadcast message to try to find a server on the local network
*/
void NetworkManager::findServer() {
string msg = "HELLO";
IPaddress broadcast;
broadcast.host = INADDR_BROADCAST;
SDLNet_Write16(SERVER_PORT, &broadcast.port);

UDPpacket *packet = SDLNet_AllocPacket(5 + 1);

packet->len = 5 + 1;
packet->address = broadcast;
packet->data[0] = HELLO_MSG;
packet->data[1] = msg.c_str();

SDLNet_UDP_Send(clientSock, -1, packet);

SDLNet_FreePacket(packet);
}
33 changes: 33 additions & 0 deletions src/network.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”,
* as defined by the Mozilla Public License, v. 2.0.
*/

#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>
#include "context.hpp"

#ifndef _H_NETWORK_
#define _H_NETWORK_

class NetworkManager final {
public:
NetworkManager();
~NetworkManager();
void send(Uint8 code, const std::string & message);
void findServer();
void setServerInfo(Uint32 ip, Uint16 port);
void receive(Context* currentContext);

private:
const Uint16 SERVER_PORT = 6545;

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

#endif //! _H_NETWORK_
7 changes: 0 additions & 7 deletions src/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,5 @@ const char* CLIENT = "cl";
const char* TEAM = "te";
const char* ROOM = "ro";

struct LB_Packet {
const char* sender;
const char* recipient;
Uint8 type;
const char* content;
};


#endif /* _H_PROTOCOL_ */

0 comments on commit 926fd6c

Please sign in to comment.