From 17e6b2ef2ba4a2105ec99fce152de25815c04516 Mon Sep 17 00:00:00 2001 From: Alex Kerr Date: Sun, 15 Aug 2021 11:58:10 +0100 Subject: [PATCH] Add networking client --- Jenkinsfile | 8 ++++ wii/Jenkinsfile | 19 ++++++++ wii/source/main.cpp | 16 ++++++- wii/source/network/networking.cpp | 77 +++++++++++++++++++++++++++++++ wii/source/network/networking.h | 26 +++++++++++ wii/source/network/udp_client.cpp | 26 +++++++++++ wii/source/network/udp_client.h | 20 ++++++++ 7 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 Jenkinsfile create mode 100644 wii/Jenkinsfile create mode 100644 wii/source/network/networking.cpp create mode 100644 wii/source/network/networking.h create mode 100644 wii/source/network/udp_client.cpp create mode 100644 wii/source/network/udp_client.h diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..91ed91b --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,8 @@ +pipeline { + agent any + stage ("Build Wii") { + steps { + load "wii/Jenkinsfile" + } + } +} \ No newline at end of file diff --git a/wii/Jenkinsfile b/wii/Jenkinsfile new file mode 100644 index 0000000..973cc5f --- /dev/null +++ b/wii/Jenkinsfile @@ -0,0 +1,19 @@ +pipeline { + agent { + docker { image 'devkitpro/devkitppc' } + } + stages { + stage('Build') { + steps { + sh 'make' + } + } + stage('Archive') { + steps { + archiveArtifacts artifacts: 'boot.dol', fingerprint: true + archiveArtifacts artifacts: 'meta.xml', fingerprint: true + archiveArtifacts artifacts: 'icon.png', fingerprint: true + } + } + } +} \ No newline at end of file diff --git a/wii/source/main.cpp b/wii/source/main.cpp index 00972f9..7c5228f 100644 --- a/wii/source/main.cpp +++ b/wii/source/main.cpp @@ -1,19 +1,33 @@ #include #include +#include #include #include +#include "network/networking.h" +#include "network/udp_client.h" #include "screen.h" +/// The IP address of the server to connect to. +const std::string SERVER_IP_ADDRESS = "192.168.0.1"; + +/// The port number of the server to connect to. +constexpr uint16_t SERVER_PORT = 10000; + int main(int argc, char **argv) { + // Screen init auto screen = Screen{}; + // Controller init WPAD_Init(); - Paddle my_paddle{ screen.height(), 15, 5 }; + // Network init + Networking network; + network.init(); + auto udp_client = UdpClient{ SERVER_IP_ADDRESS, SERVER_PORT }; auto exit = false; while (!exit) diff --git a/wii/source/network/networking.cpp b/wii/source/network/networking.cpp new file mode 100644 index 0000000..b250819 --- /dev/null +++ b/wii/source/network/networking.cpp @@ -0,0 +1,77 @@ +#include "networking.h" + +#include +#include +#include +#include +#include +#include +#include +#include + + +bool Networking::_connected = false; + +bool Networking::init() noexcept +{ + if (!Networking::_connected) + { + int32_t ret = if_config( + _ip_address, + _netmask, + _gateway, + TRUE, + 20 + ); + + if (ret >= 0) + Networking::_connected = true; + } + + return Networking::_connected; +} + +int32_t Networking::create_socket(uint8_t type) +{ + if (type != SOCK_STREAM && type != SOCK_DGRAM) + throw std::invalid_argument("Unsupported socket type."); + + int32_t sock = net_socket(AF_INET, type, IPPROTO_IP); + if (sock == INVALID_SOCKET) + throw std::runtime_error("Could not create socket."); + + return sock; +} + +std::unique_ptr Networking::create_host(uint16_t port) noexcept +{ + auto server = std::make_unique(); + server->sin_family = AF_INET; + server->sin_port = htons(port); + server->sin_addr.s_addr = INADDR_ANY; + return server; +} + +std::unique_ptr Networking::create_dest(std::string_view ip_address, uint16_t port) noexcept +{ + auto server = std::make_unique(); + server->sin_family = AF_INET; + server->sin_port = htons(port); + inet_aton(ip_address.data(), &(server->sin_addr)); + return server; +} + +std::optional Networking::get_ip_address() noexcept +{ + return Networking::_connected ? std::optional(std::string(_ip_address)) : std::nullopt; +} + +std::optional Networking::get_netmask() noexcept +{ + return Networking::_connected ? std::optional(std::string(_netmask)) : std::nullopt; +} + +std::optional Networking::get_gateway() noexcept +{ + return Networking::_connected ? std::optional(std::string(_gateway)) : std::nullopt; +} diff --git a/wii/source/network/networking.h b/wii/source/network/networking.h new file mode 100644 index 0000000..a58f043 --- /dev/null +++ b/wii/source/network/networking.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + + +class Networking final +{ +public: + bool init() noexcept; + static int32_t create_socket(uint8_t type); + static std::unique_ptr create_host(uint16_t port) noexcept; + static std::unique_ptr create_dest(std::string_view ip_address, uint16_t port) noexcept; + std::optional get_ip_address() noexcept; + std::optional get_netmask() noexcept; + std::optional get_gateway() noexcept; +private: + static bool _connected; + char _ip_address[16]; + char _netmask[16]; + char _gateway[16]; +}; diff --git a/wii/source/network/udp_client.cpp b/wii/source/network/udp_client.cpp new file mode 100644 index 0000000..6d64841 --- /dev/null +++ b/wii/source/network/udp_client.cpp @@ -0,0 +1,26 @@ +#include "udp_client.h" + +#include +#include + +#include "networking.h" + + +UdpClient::UdpClient(std::string_view ip_address, uint16_t port): + _sock { Networking::create_socket(SOCK_DGRAM) }, + _client { Networking::create_host(0) }, + _server { Networking::create_dest(ip_address, port) } +{ +} + +void UdpClient::send() +{ + net_sendto(_sock, "Ping!", sizeof("Ping!"), 0, (sockaddr *)(_server.get()), sizeof(sockaddr_in)); +} + +std::string UdpClient::receive() +{ + char buffer[1500] = {}; + net_recv(_sock, &buffer[0], 1500, 0); + return std::string(buffer); +} \ No newline at end of file diff --git a/wii/source/network/udp_client.h b/wii/source/network/udp_client.h new file mode 100644 index 0000000..d19a3c2 --- /dev/null +++ b/wii/source/network/udp_client.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include +#include +#include + + +class UdpClient final +{ +public: + UdpClient(std::string_view ip_address, uint16_t port); + void send(); + std::string receive(); +private: + int32_t _sock; + std::unique_ptr _client; + std::unique_ptr _server; +};