mirror of
https://github.com/wiiu-env/wut.git
synced 2024-12-13 09:22:38 +01:00
whb: Implement a basic TCP-based command server kit.
* This server will listen for connections, and then listen on that connection stream for, well, text. It will then close the connection and send the string back to the main program.
This commit is contained in:
parent
ef34f600c7
commit
ace7c0b364
28
src/libwhb/include/whb/commandserver.h
Normal file
28
src/libwhb/include/whb/commandserver.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \defgroup whb_commandserver Network Command Server
|
||||
* \ingroup whb
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define WHB_SERVER_BUFFER_SIZE 1024
|
||||
|
||||
BOOL
|
||||
WHBCommandServerInit();
|
||||
|
||||
void
|
||||
WHBCommandServerStop();
|
||||
|
||||
BOOL
|
||||
WHBCommandServerListen();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/** @} */
|
123
src/libwhb/src/commandserver.c
Normal file
123
src/libwhb/src/commandserver.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include <coreinit/baseheap.h>
|
||||
#include <coreinit/expandedheap.h>
|
||||
#include <nsysnet/socket.h>
|
||||
#include <whb/commandserver.h>
|
||||
#include <whb/libmanager.h>
|
||||
#include <whb/log.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define SERVER_PORT 4406
|
||||
|
||||
int
|
||||
sSocket = -1;
|
||||
|
||||
struct sockaddr_in
|
||||
sAddr;
|
||||
|
||||
void
|
||||
closeSocket(const char * funcName)
|
||||
{
|
||||
int ret = socketclose(sSocket);
|
||||
if(ret < 0) {
|
||||
WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
closeClient(const char * funcName, int fd)
|
||||
{
|
||||
int ret = socketclose(fd);
|
||||
if(ret < 0) {
|
||||
WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr());
|
||||
}
|
||||
}
|
||||
|
||||
BOOL
|
||||
WHBCommandServerInit()
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if(sSocket >= 0) {
|
||||
WHBLogPrintf("%s: Command server is already running.", __FUNCTION__);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
WHBInitializeSocketLibrary();
|
||||
|
||||
sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(sSocket < 0) {
|
||||
WHBLogPrintf("%s: Error occurred while creating socket: %d", __FUNCTION__, socketlasterr());
|
||||
sSocket = -1;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
memset(&sAddr, 0, sizeof(struct sockaddr_in));
|
||||
sAddr.sin_family = AF_INET;
|
||||
sAddr.sin_port = htons(SERVER_PORT);
|
||||
sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
ret = bind(sSocket, (struct sockaddr *)&sAddr, sizeof(struct sockaddr_in));
|
||||
if(ret < 0) {
|
||||
WHBLogPrintf("%s: Error occurred while binding to socket: %d", __FUNCTION__, socketlasterr());
|
||||
closeSocket(__FUNCTION__);
|
||||
sSocket = -1;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = listen(sSocket, 3);
|
||||
if(ret < 0) {
|
||||
WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr());
|
||||
closeSocket(__FUNCTION__);
|
||||
sSocket = -1;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
WHBCommandServerStop()
|
||||
{
|
||||
if(sSocket < 0) {
|
||||
WHBLogPrintf("%s: Socket is already closed.", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
|
||||
closeSocket(__FUNCTION__);
|
||||
sSocket = -1;
|
||||
WHBDeinitializeSocketLibrary();
|
||||
}
|
||||
|
||||
BOOL
|
||||
WHBCommandServerListen(char * stringLocation)
|
||||
{
|
||||
char buffer[WHB_SERVER_BUFFER_SIZE];
|
||||
memset(buffer, 0, WHB_SERVER_BUFFER_SIZE);
|
||||
|
||||
int ret, sClient;
|
||||
struct sockaddr_in sClientAddr;
|
||||
socklen_t sClientAddrLen = sizeof(struct sockaddr_in);
|
||||
|
||||
if(sSocket < 0) {
|
||||
WHBLogPrintf("%s: Socket is not open. Please run WHBCommandServerInit() first.", __FUNCTION__);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen);
|
||||
if(sClient < 0) {
|
||||
WHBLogPrintf("%s: Error occurred while accepting a client connection: %d", __FUNCTION__, socketlasterr());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = recv(sClient, buffer, WHB_SERVER_BUFFER_SIZE, 0);
|
||||
if(ret < 0) {
|
||||
WHBLogPrintf("%s: Error occurred while receiving data from client: %d", __FUNCTION__, socketlasterr());
|
||||
closeClient(__FUNCTION__, sClient);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
closeClient(__FUNCTION__, sClient);
|
||||
memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE);
|
||||
return TRUE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user