Net
This commit is contained in:
@@ -6,4 +6,5 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
networkdolphin.c
|
||||
networksocketdolphin.c
|
||||
)
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "networksocketdolphin.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
#ifdef DUSK_WII
|
||||
#include <network.h>
|
||||
#endif
|
||||
|
||||
errorret_t networkSocketDolphinConnect(
|
||||
networksocketdolphin_t *sock,
|
||||
const char_t *host,
|
||||
const uint16_t port
|
||||
) {
|
||||
assertNotNull(sock, "sock must not be NULL");
|
||||
assertNotNull(host, "host must not be NULL");
|
||||
|
||||
#ifdef DUSK_WII
|
||||
struct hostent *entry = net_gethostbyname(host);
|
||||
if(entry == NULL || entry->h_addr_list[0] == NULL) {
|
||||
errorThrow("Failed to resolve host %s", host);
|
||||
}
|
||||
|
||||
const s32 fd = net_socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(fd < 0) errorThrow("Failed to create socket: %d", (int_t)fd);
|
||||
|
||||
struct sockaddr_in serverAddr;
|
||||
memoryZero(&serverAddr, sizeof(serverAddr));
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_port = htons(port);
|
||||
memoryCopy(
|
||||
&serverAddr.sin_addr, entry->h_addr_list[0], sizeof(serverAddr.sin_addr)
|
||||
);
|
||||
|
||||
const s32 ret = net_connect(fd, (void *)&serverAddr, sizeof(serverAddr));
|
||||
if(ret < 0) {
|
||||
net_close(fd);
|
||||
errorThrow("Failed to connect to %s:%u: %d", host, port, (int_t)ret);
|
||||
}
|
||||
|
||||
sock->fd = fd;
|
||||
errorOk();
|
||||
#else
|
||||
errorThrow(
|
||||
"Networking is not supported on this platform (%s:%u)", host, port
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
errorret_t networkSocketDolphinSend(
|
||||
networksocketdolphin_t *sock,
|
||||
const uint8_t *data,
|
||||
const size_t length,
|
||||
size_t *outSent
|
||||
) {
|
||||
assertNotNull(sock, "sock must not be NULL");
|
||||
assertNotNull(data, "data must not be NULL");
|
||||
assertNotNull(outSent, "outSent must not be NULL");
|
||||
|
||||
#ifdef DUSK_WII
|
||||
const s32 sent = net_send(sock->fd, data, (s32)length, 0);
|
||||
if(sent < 0) errorThrow("Failed to send data: %d", (int_t)sent);
|
||||
|
||||
*outSent = (size_t)sent;
|
||||
errorOk();
|
||||
#else
|
||||
errorThrow("Networking is not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
errorret_t networkSocketDolphinReceive(
|
||||
networksocketdolphin_t *sock,
|
||||
uint8_t *buffer,
|
||||
const size_t bufferSize,
|
||||
size_t *outReceived
|
||||
) {
|
||||
assertNotNull(sock, "sock must not be NULL");
|
||||
assertNotNull(buffer, "buffer must not be NULL");
|
||||
assertNotNull(outReceived, "outReceived must not be NULL");
|
||||
|
||||
#ifdef DUSK_WII
|
||||
const s32 received = net_recv(sock->fd, buffer, (s32)bufferSize, 0);
|
||||
if(received < 0) errorThrow("Failed to receive data: %d", (int_t)received);
|
||||
|
||||
*outReceived = (size_t)received;
|
||||
errorOk();
|
||||
#else
|
||||
errorThrow("Networking is not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
void networkSocketDolphinClose(networksocketdolphin_t *sock) {
|
||||
assertNotNull(sock, "sock must not be NULL");
|
||||
|
||||
#ifdef DUSK_WII
|
||||
if(sock->fd >= 0) net_close(sock->fd);
|
||||
#endif
|
||||
sock->fd = -1;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct {
|
||||
int_t fd;
|
||||
} networksocketdolphin_t;
|
||||
|
||||
/**
|
||||
* Resolves host and opens a blocking TCP connection to host:port.
|
||||
* Always fails under plain DUSK_GAMECUBE -- retail GameCube units have
|
||||
* no network hardware.
|
||||
*
|
||||
* @param sock The socket structure to initialize.
|
||||
* @param host The hostname or IP address to connect to.
|
||||
* @param port The port to connect to.
|
||||
* @return An error if resolution or connection failed.
|
||||
*/
|
||||
errorret_t networkSocketDolphinConnect(
|
||||
networksocketdolphin_t *sock,
|
||||
const char_t *host,
|
||||
const uint16_t port
|
||||
);
|
||||
|
||||
/**
|
||||
* Sends data over the socket. May write fewer bytes than requested; the
|
||||
* caller is responsible for looping until all data is sent.
|
||||
*
|
||||
* @param sock The connected socket.
|
||||
* @param data The data to send.
|
||||
* @param length The number of bytes in data.
|
||||
* @param outSent The number of bytes actually sent is written here.
|
||||
* @return An error if the send failed.
|
||||
*/
|
||||
errorret_t networkSocketDolphinSend(
|
||||
networksocketdolphin_t *sock,
|
||||
const uint8_t *data,
|
||||
const size_t length,
|
||||
size_t *outSent
|
||||
);
|
||||
|
||||
/**
|
||||
* Receives data from the socket.
|
||||
*
|
||||
* @param sock The connected socket.
|
||||
* @param buffer The buffer to receive into.
|
||||
* @param bufferSize The size of buffer.
|
||||
* @param outReceived The number of bytes actually received is written
|
||||
* here; 0 means the peer closed the connection.
|
||||
* @return An error if the receive failed.
|
||||
*/
|
||||
errorret_t networkSocketDolphinReceive(
|
||||
networksocketdolphin_t *sock,
|
||||
uint8_t *buffer,
|
||||
const size_t bufferSize,
|
||||
size_t *outReceived
|
||||
);
|
||||
|
||||
/**
|
||||
* Closes the socket.
|
||||
*
|
||||
* @param sock The socket to close.
|
||||
*/
|
||||
void networkSocketDolphinClose(networksocketdolphin_t *sock);
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "networksocketdolphin.h"
|
||||
|
||||
typedef networksocketdolphin_t networksocketplatform_t;
|
||||
|
||||
#define networkSocketPlatformConnect networkSocketDolphinConnect
|
||||
#define networkSocketPlatformSend networkSocketDolphinSend
|
||||
#define networkSocketPlatformReceive networkSocketDolphinReceive
|
||||
#define networkSocketPlatformClose networkSocketDolphinClose
|
||||
Reference in New Issue
Block a user