Add networking client
This commit is contained in:
		
							parent
							
								
									9b802afe65
								
							
						
					
					
						commit
						17e6b2ef2b
					
				
							
								
								
									
										8
									
								
								Jenkinsfile
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Jenkinsfile
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
pipeline {
 | 
			
		||||
    agent any
 | 
			
		||||
    stage ("Build Wii") {
 | 
			
		||||
        steps {
 | 
			
		||||
            load "wii/Jenkinsfile"
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								wii/Jenkinsfile
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								wii/Jenkinsfile
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,19 +1,33 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
#include <gccore.h>
 | 
			
		||||
#include <wiiuse/wpad.h>
 | 
			
		||||
 | 
			
		||||
#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)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										77
									
								
								wii/source/network/networking.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								wii/source/network/networking.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
#include "networking.h"
 | 
			
		||||
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <stdexcept>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <string_view>
 | 
			
		||||
#include <gccore.h>
 | 
			
		||||
#include <network.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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<sockaddr_in> Networking::create_host(uint16_t port) noexcept
 | 
			
		||||
{
 | 
			
		||||
    auto server = std::make_unique<sockaddr_in>();
 | 
			
		||||
    server->sin_family = AF_INET;
 | 
			
		||||
    server->sin_port = htons(port);
 | 
			
		||||
    server->sin_addr.s_addr = INADDR_ANY;
 | 
			
		||||
    return server;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<sockaddr_in> Networking::create_dest(std::string_view ip_address, uint16_t port) noexcept
 | 
			
		||||
{
 | 
			
		||||
    auto server = std::make_unique<sockaddr_in>();
 | 
			
		||||
    server->sin_family = AF_INET;
 | 
			
		||||
    server->sin_port = htons(port);
 | 
			
		||||
    inet_aton(ip_address.data(), &(server->sin_addr));
 | 
			
		||||
    return server;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::optional<std::string> Networking::get_ip_address() noexcept
 | 
			
		||||
{
 | 
			
		||||
    return Networking::_connected ? std::optional(std::string(_ip_address)) : std::nullopt;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::optional<std::string> Networking::get_netmask() noexcept
 | 
			
		||||
{
 | 
			
		||||
    return Networking::_connected ? std::optional(std::string(_netmask)) : std::nullopt;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::optional<std::string> Networking::get_gateway() noexcept
 | 
			
		||||
{
 | 
			
		||||
    return Networking::_connected ? std::optional(std::string(_gateway)) : std::nullopt;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								wii/source/network/networking.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								wii/source/network/networking.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <string_view>
 | 
			
		||||
#include <network.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Networking final
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    bool init() noexcept;
 | 
			
		||||
    static int32_t create_socket(uint8_t type);
 | 
			
		||||
    static std::unique_ptr<sockaddr_in> create_host(uint16_t port) noexcept;
 | 
			
		||||
    static std::unique_ptr<sockaddr_in> create_dest(std::string_view ip_address, uint16_t port) noexcept;
 | 
			
		||||
    std::optional<std::string> get_ip_address() noexcept;
 | 
			
		||||
    std::optional<std::string> get_netmask() noexcept;
 | 
			
		||||
    std::optional<std::string> get_gateway() noexcept;
 | 
			
		||||
private:
 | 
			
		||||
    static bool _connected;
 | 
			
		||||
    char _ip_address[16];
 | 
			
		||||
    char _netmask[16];
 | 
			
		||||
    char _gateway[16];
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										26
									
								
								wii/source/network/udp_client.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								wii/source/network/udp_client.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
#include "udp_client.h"
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <network.h>
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								wii/source/network/udp_client.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								wii/source/network/udp_client.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <string_view>
 | 
			
		||||
#include <network.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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<sockaddr_in> _client;
 | 
			
		||||
    std::unique_ptr<sockaddr_in> _server;
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user