mirror of
https://github.com/wiiu-env/wut.git
synced 2025-03-02 22:55:26 +01:00
whb: Command Server connection stream now stays open.
* The stream will stay open until either an error occurs, WHBCommandServerStop is called, or a null string is returned (the client socket had been closed). * The header file now includes wut.h, so that BOOL is defined.
This commit is contained in:
parent
240514eff2
commit
39d4599896
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <wut.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \defgroup whb_commandserver Network Command Server
|
* \defgroup whb_commandserver Network Command Server
|
||||||
|
@ -12,25 +12,34 @@
|
|||||||
int
|
int
|
||||||
sSocket = -1;
|
sSocket = -1;
|
||||||
|
|
||||||
|
int
|
||||||
|
sClient = -1;
|
||||||
|
|
||||||
struct sockaddr_in
|
struct sockaddr_in
|
||||||
sAddr;
|
sAddr;
|
||||||
|
|
||||||
void
|
static inline void
|
||||||
closeSocket(const char * funcName)
|
closeSocket(const char * funcName)
|
||||||
{
|
{
|
||||||
int ret = socketclose(sSocket);
|
int ret = socketclose(sSocket);
|
||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr());
|
WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sSocket = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static inline void
|
||||||
closeClient(const char * funcName, int fd)
|
closeClient(const char * funcName)
|
||||||
{
|
{
|
||||||
int ret = socketclose(fd);
|
int ret = socketclose(sClient);
|
||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr());
|
WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sClient = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
BOOL
|
||||||
@ -61,7 +70,6 @@ WHBCommandServerInit()
|
|||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred while binding to socket: %d", __FUNCTION__, socketlasterr());
|
WHBLogPrintf("%s: Error occurred while binding to socket: %d", __FUNCTION__, socketlasterr());
|
||||||
closeSocket(__FUNCTION__);
|
closeSocket(__FUNCTION__);
|
||||||
sSocket = -1;
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +77,6 @@ WHBCommandServerInit()
|
|||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr());
|
WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr());
|
||||||
closeSocket(__FUNCTION__);
|
closeSocket(__FUNCTION__);
|
||||||
sSocket = -1;
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,8 +91,10 @@ WHBCommandServerStop()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(sClient >= 0) {
|
||||||
|
closeClient(__FUNCTION__);
|
||||||
|
}
|
||||||
closeSocket(__FUNCTION__);
|
closeSocket(__FUNCTION__);
|
||||||
sSocket = -1;
|
|
||||||
WHBDeinitializeSocketLibrary();
|
WHBDeinitializeSocketLibrary();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +104,7 @@ WHBCommandServerListen(char * stringLocation)
|
|||||||
char buffer[WHB_SERVER_BUFFER_SIZE];
|
char buffer[WHB_SERVER_BUFFER_SIZE];
|
||||||
memset(buffer, 0, WHB_SERVER_BUFFER_SIZE);
|
memset(buffer, 0, WHB_SERVER_BUFFER_SIZE);
|
||||||
|
|
||||||
int ret, sClient;
|
int ret;
|
||||||
struct sockaddr_in sClientAddr;
|
struct sockaddr_in sClientAddr;
|
||||||
socklen_t sClientAddrLen = sizeof(struct sockaddr_in);
|
socklen_t sClientAddrLen = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
@ -104,20 +113,26 @@ WHBCommandServerListen(char * stringLocation)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen);
|
|
||||||
if(sClient < 0) {
|
if(sClient < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred while accepting a client connection: %d", __FUNCTION__, socketlasterr());
|
sClient = accept(sSocket, (struct sockaddr *)&sClientAddr, &sClientAddrLen);
|
||||||
return FALSE;
|
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);
|
ret = recv(sClient, buffer, WHB_SERVER_BUFFER_SIZE, 0);
|
||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
WHBLogPrintf("%s: Error occurred while receiving data from client: %d", __FUNCTION__, socketlasterr());
|
WHBLogPrintf("%s: Error occurred while receiving data from client: %d", __FUNCTION__, socketlasterr());
|
||||||
closeClient(__FUNCTION__, sClient);
|
closeClient(__FUNCTION__);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if(ret == 0) {
|
||||||
|
WHBLogPrintf("%s: Remote socket was closed. Closing client connection...", __FUNCTION__);
|
||||||
|
closeClient(__FUNCTION__);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
closeClient(__FUNCTION__, sClient);
|
|
||||||
memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE);
|
memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user