http: Report local address to server.

This allows matching inside a network.
This commit is contained in:
Unknown W. Brackets 2016-07-03 12:38:55 -07:00
parent bde07bf9e7
commit 3eee81953a
3 changed files with 45 additions and 2 deletions

View File

@ -16,6 +16,8 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "i18n/i18n.h"
#include "file/fd_util.h"
#include "net/http_client.h"
#include "net/http_server.h"
#include "net/resolve.h"
#include "net/sinks.h"
@ -51,6 +53,26 @@ static ServerStatus RetrieveStatus() {
return serverStatus;
}
// This reports the local IP address to report.ppsspp.org, which can then
// relay that address to a mobile device searching for the server.
static void RegisterServer(int port) {
bool result = false;
net::AutoInit netInit;
http::Client http;
Buffer theVoid;
if (http.Resolve("report.ppsspp.org", 80)) {
http.Connect();
char resource[1024] = {};
std::string ip = fd_util::GetLocalIP(http.sock());
snprintf(resource, sizeof(resource) - 1, "/match/update?local=%s&port=%d", ip.c_str(), port);
http.GET(resource, &theVoid);
http.Disconnect();
}
}
static void ExecuteServer() {
setCurrentThreadName("HTTPServer");
@ -131,9 +153,10 @@ static void ExecuteServer() {
}
http->Listen(0);
// TODO: Report local IP and port.
UpdateStatus(ServerStatus::RUNNING);
RegisterServer(http->Port());
while (RetrieveStatus() == ServerStatus::RUNNING) {
http->RunSlice(5.0);
}

View File

@ -4,11 +4,16 @@
#include <math.h>
#include <stdio.h>
#ifndef _WIN32
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#else
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <fcntl.h>
@ -127,4 +132,17 @@ void SetNonBlocking(int sock, bool non_blocking) {
#endif
}
std::string GetLocalIP(int sock) {
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
socklen_t len = sizeof(server_addr);
if (getsockname(sock, (struct sockaddr *)&server_addr, &len) == 0) {
char *result = inet_ntoa(*(in_addr *)&server_addr.sin_addr);
if (result) {
return result;
}
}
return "";
}
} // fd_util

View File

@ -22,6 +22,8 @@ bool WaitUntilReady(int fd, double timeout, bool for_write = false);
void SetNonBlocking(int fd, bool non_blocking);
std::string GetLocalIP(int sock);
} // fd_util
#endif // _FD_UTIL