Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matchcomms support #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(source_files
${PROJECT_SOURCE_DIR}/src/sockets_linux.cc
${PROJECT_SOURCE_DIR}/src/sockets_windows.cc
${PROJECT_SOURCE_DIR}/src/statesetting.cc
${PROJECT_SOURCE_DIR}/lib/src/easywsclient.cpp
)

set(header_files
Expand All @@ -39,6 +40,7 @@ set(header_files
${PROJECT_SOURCE_DIR}/inc/rlbot/server.h
${PROJECT_SOURCE_DIR}/inc/rlbot/sockets.h
${PROJECT_SOURCE_DIR}/inc/rlbot/statesetting.h
${PROJECT_SOURCE_DIR}/inc/rlbot/matchcomms.h
)

add_library(RLBotCPP STATIC ${source_files} ${header_files})
Expand Down
3 changes: 3 additions & 0 deletions inc/rlbot/bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "rlbot/interface.h"
#include "rlbot/packets.h"
#include "rlbot/rlbot_generated.h"
#include "matchcomms.h"

#include <string>

Expand All @@ -14,6 +15,8 @@ class Bot {
int index;
int team;
std::string name;

std::unique_ptr<MatchCommsClient> matchcomms;

Bot(int index, int team, std::string name);
virtual ~Bot() {}
Expand Down
2 changes: 1 addition & 1 deletion inc/rlbot/botmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BotManager {

void StartBotServer(uint16_t port);
void RecieveMessage(Message message);
void AddBot(int index, int team, std::string name);
void AddBot(int index, int team, std::string name, std::string matchcomms_url);
void RemoveBot(int index);
};
} // namespace rlbot
33 changes: 33 additions & 0 deletions inc/rlbot/matchcomms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "easywsclient.hpp"
#include "json.hpp"

namespace rlbot {
class MatchCommsClient {
easywsclient::WebSocket::pointer client;

public:
explicit MatchCommsClient(const std::string& url) {
client = easywsclient::WebSocket::from_url(url + "/broadcast");
}

~MatchCommsClient() {
client->close();
delete client;
}

std::vector<nlohmann::json> GetIncomingBroadcast() {
std::vector<nlohmann::json> messages;
client->poll();
client->dispatch([&messages](const std::string& message) {
messages.push_back(nlohmann::json::parse(message));
});
return messages;
}

void Send(const nlohmann::json & message) {
client->send(message.dump());
client->poll();
}
};
}
1 change: 1 addition & 0 deletions inc/rlbot/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Message {
int team;
std::string name;
std::string dll_dir;
std::string matchcomms_url;
};

class Server {
Expand Down
94 changes: 94 additions & 0 deletions lib/inc/easywsclient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// The MIT License (MIT)

// Copyright (c) 2012, 2013 <[email protected]>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD
#define EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD

// This code comes from:
// https://github.com/dhbaird/easywsclient
//
// To get the latest version:
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp
// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp

#include <string>
#include <vector>

namespace easywsclient {

struct Callback_Imp { virtual void operator()(const std::string& message) = 0; };
struct BytesCallback_Imp { virtual void operator()(const std::vector<uint8_t>& message) = 0; };

class WebSocket {
public:
typedef WebSocket * pointer;
typedef enum readyStateValues { CLOSING, CLOSED, CONNECTING, OPEN } readyStateValues;

// Factories:
static pointer create_dummy();
static pointer from_url(const std::string& url, const std::string& origin = std::string());
static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string());

// Interfaces:
virtual ~WebSocket() { }
virtual void poll(int timeout = 0) = 0; // timeout in milliseconds
virtual void send(const std::string& message) = 0;
virtual void sendBinary(const std::string& message) = 0;
virtual void sendBinary(const std::vector<uint8_t>& message) = 0;
virtual void sendPing() = 0;
virtual void close() = 0;
virtual readyStateValues getReadyState() const = 0;

template<class Callable>
void dispatch(Callable callable)
// For callbacks that accept a string argument.
{ // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
struct _Callback : public Callback_Imp {
Callable& callable;
_Callback(Callable& callable) : callable(callable) { }
void operator()(const std::string& message) { callable(message); }
};
_Callback callback(callable);
_dispatch(callback);
}

template<class Callable>
void dispatchBinary(Callable callable)
// For callbacks that accept a std::vector<uint8_t> argument.
{ // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
struct _Callback : public BytesCallback_Imp {
Callable& callable;
_Callback(Callable& callable) : callable(callable) { }
void operator()(const std::vector<uint8_t>& message) { callable(message); }
};
_Callback callback(callable);
_dispatchBinary(callback);
}

protected:
virtual void _dispatch(Callback_Imp& callable) = 0;
virtual void _dispatchBinary(BytesCallback_Imp& callable) = 0;
};

} // namespace easywsclient

#endif /* EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD */
Loading