Files
GDevelop/Extensions/Network/NetworkManagerFunctions.cpp
T
Florian Rival a8559bfbbc Add clang-format to format (C++) source files automatically (#491)
* Update all CMakeLists of extensions to use clang-format
* Run clang-format on all Extensions
* Update GDCore CMakeLists.txt to add clang-format
* Run clang-format on GDCore files
* Update GDJS and GDCpp CMakeLists.txt to add clang-format
* Run clang-format on GDCpp and GDJS files
2018-05-09 15:57:38 -07:00

94 lines
2.5 KiB
C++

/**
GDevelop - Network Extension
Copyright (c) 2010-2016 Florian Rival (Florian.Rival@gmail.com)
This project is released under the MIT License.
*/
#include <SFML/Network.hpp>
#include <fstream>
#include <iostream>
#include <list>
#include <string>
#include "ErrorManager.h"
#include "GDCpp/Runtime/RuntimeScene.h"
#include "NetworkManager.h"
#include "ReceivedDataManager.h"
namespace GDpriv {
namespace NetworkExtension {
void GD_EXTENSION_API ResetReceivedData() {
ReceivedDataManager::Get()->values.clear();
ReceivedDataManager::Get()->strings.clear();
}
void GD_EXTENSION_API ActStopListening() {
NetworkManager::Get()->StopListening();
}
void GD_EXTENSION_API AddRecipient(const gd::String& adressStr,
short unsigned int port) {
sf::IpAddress address = adressStr.ToLocale();
if (port == 0) port = 50001; // Default value
NetworkManager::Get()->AddRecipient(address, port);
}
void GD_EXTENSION_API RemoveAllRecipients() {
NetworkManager::Get()->RemoveAllRecipients();
}
void GD_EXTENSION_API ListenToPort(short unsigned int port) {
if (port == 0) port = 50001;
NetworkManager::Get()->ListenToPort(port);
}
void GD_EXTENSION_API SendValue(const gd::String& title, double data) {
sf::Packet packet;
packet << sf::Int32(0) // 0 indicate that the packet contains a double
<< title << static_cast<double>(data);
NetworkManager::Get()->Send(packet);
}
void GD_EXTENSION_API SendString(const gd::String& title,
const gd::String& data) {
sf::Packet packet;
packet << sf::Int32(1) // 1 indicate that the packet contains a string
<< title << data;
NetworkManager::Get()->Send(packet);
}
void GD_EXTENSION_API ReceivePackets() {
NetworkManager::Get()->ReceivePackets();
}
gd::String GD_EXTENSION_API GetReceivedDataString(const gd::String& title) {
return ReceivedDataManager::Get()->strings[title];
}
double GD_EXTENSION_API GetReceivedDataValue(const gd::String& title) {
return ReceivedDataManager::Get()->values[title];
}
gd::String GD_EXTENSION_API GetLastError() {
return ErrorManager::Get()->GetLastError();
}
gd::String GD_EXTENSION_API GetPublicAddress(float timeoutInSeconds) {
return gd::String::FromLocale(
sf::IpAddress::getPublicAddress(sf::seconds(timeoutInSeconds))
.toString());
}
gd::String GD_EXTENSION_API GetLocalAddress() {
return gd::String::FromLocale(sf::IpAddress::getLocalAddress().toString());
}
} // namespace NetworkExtension
} // namespace GDpriv