Merge pull request #57 from CreeperMario/udp-log-fixes

More Miscellaneous Fixes and Features
This commit is contained in:
James 2017-10-10 13:54:53 +01:00 committed by GitHub
commit 1ce68f6d58
22 changed files with 401 additions and 69 deletions

14
.gitmodules vendored
View File

@ -1,9 +1,9 @@
[submodule "tools/ext/zlib"]
path = externals/zlib
url = https://github.com/madler/zlib.git
[submodule "tools/ext/cppformat"]
path = externals/cppformat
url = https://github.com/cppformat/cppformat.git
[submodule "tools/ext/excmd"]
[submodule "externals/excmd"]
path = externals/excmd
url = https://github.com/exjam/excmd
[submodule "externals/fmt"]
path = externals/fmt
url = https://github.com/fmtlib/fmt
[submodule "externals/zlib"]
path = externals/zlib
url = https://github.com/madler/zlib.git

View File

@ -13,6 +13,8 @@ matrix:
- zlib1g-dev
env:
- MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"
- os: osx
osx_image: xcode8.3
cache:
directories:
@ -25,7 +27,8 @@ git:
submodules: true
before_script:
- wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-linux.tar.bz2 -O /tmp/devkitPPC.tar.bz2
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-linux.tar.bz2 -O /tmp/devkitPPC.tar.bz2; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then wget https://freefr.dl.sourceforge.net/project/devkitpro/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-osx.tar.bz2 -O /tmp/devkitPPC.tar.bz2; fi
- tar -xjf /tmp/devkitPPC.tar.bz2
- export DEVKITPPC=$PWD/devkitPPC

View File

@ -1,10 +1,10 @@
project(libraries)
include(ExternalProject)
# cppformat
set(CPPFORMAT_DIR "cppformat")
externalproject_add(cppformat
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${CPPFORMAT_DIR}"
# fmtlib
set(FMTLIB_DIR "fmt")
externalproject_add(fmtlib
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FMTLIB_DIR}"
CMAKE_CACHE_ARGS
-DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS:string=${CMAKE_C_FLAGS}
@ -14,28 +14,28 @@ externalproject_add(cppformat
-DFMT_INSTALL:string=off
-DFMT_TEST:string=off
INSTALL_COMMAND "")
externalproject_get_property(cppformat BINARY_DIR)
set_target_properties(cppformat PROPERTIES FOLDER libraries)
externalproject_get_property(fmtlib BINARY_DIR)
set_target_properties(fmtlib PROPERTIES FOLDER libraries)
if(MSVC)
set(CPPFORMAT_IMPORTED_LOCATION
IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat${CMAKE_FIND_LIBRARY_SUFFIXES}")
set(FMTLIB_IMPORTED_LOCATION
IMPORTED_LOCATION_DEBUG "${BINARY_DIR}/fmt/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_RELEASE "${BINARY_DIR}/fmt/Release/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_RELWITHDEBINFO "${BINARY_DIR}/fmt/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}"
IMPORTED_LOCATION_MINSIZEREL "${BINARY_DIR}/fmt/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}fmt${CMAKE_FIND_LIBRARY_SUFFIXES}")
else()
set(CPPFORMAT_IMPORTED_LOCATION
IMPORTED_LOCATION "${BINARY_DIR}/${CMAKE_FIND_LIBRARY_PREFIXES}cppformat.a")
set(FMTLIB_IMPORTED_LOCATION
IMPORTED_LOCATION "${BINARY_DIR}/fmt/${CMAKE_FIND_LIBRARY_PREFIXES}fmt.a")
endif()
add_library(cppformat_import STATIC IMPORTED GLOBAL)
add_dependencies(cppformat_import cppformat)
set_target_properties(cppformat_import PROPERTIES ${CPPFORMAT_IMPORTED_LOCATION})
add_library(fmtlib_import STATIC IMPORTED GLOBAL)
add_dependencies(fmtlib_import fmtlib)
set_target_properties(fmtlib_import PROPERTIES ${FMTLIB_IMPORTED_LOCATION})
add_library(cppformat_final INTERFACE)
target_include_directories(cppformat_final INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${CPPFORMAT_DIR}")
target_link_libraries(cppformat_final INTERFACE cppformat_import)
set(CPPFORMAT_LINK cppformat_final PARENT_SCOPE)
add_library(fmtlib_final INTERFACE)
target_include_directories(fmtlib_final INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${FMTLIB_DIR}")
target_link_libraries(fmtlib_final INTERFACE fmtlib_import)
set(FMTLIB_LINK fmtlib_final PARENT_SCOPE)
# excmd
set(EXCMD_DIR "excmd")

1
externals/cppformat vendored

@ -1 +0,0 @@
Subproject commit 804ad8f4df7e01259374a1d2f458775f1b2d1164

1
externals/fmt vendored Submodule

@ -0,0 +1 @@
Subproject commit 398343897f98b88ade80bbebdcbe82a36c65a980

View File

@ -116,5 +116,6 @@ exit:
WHBUnmountSdCard();
WHBGfxShutdown();
WHBProcShutdown();
WHBLogUdpDeinit();
return result;
}

View File

@ -16,12 +16,12 @@
static int fs_translate_error(FSStatus error);
static int fs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode);
static int fs_close(struct _reent *r, int fd);
static ssize_t fs_write(struct _reent *r, int fd, const char *ptr, size_t len);
static ssize_t fs_write_safe(struct _reent *r, int fd, const char *ptr, size_t len);
static ssize_t fs_read(struct _reent *r, int fd, char *ptr, size_t len);
static off_t fs_seek(struct _reent *r, int fd, off_t pos, int dir);
static int fs_fstat(struct _reent *r, int fd, struct stat *st);
static int fs_close(struct _reent *r, void *fd);
static ssize_t fs_write(struct _reent *r, void *fd, const char *ptr, size_t len);
static ssize_t fs_write_safe(struct _reent *r, void *fd, const char *ptr, size_t len);
static ssize_t fs_read(struct _reent *r, void *fd, char *ptr, size_t len);
static off_t fs_seek(struct _reent *r, void *fd, off_t pos, int dir);
static int fs_fstat(struct _reent *r, void *fd, struct stat *st);
static int fs_stat(struct _reent *r, const char *file, struct stat *st);
static int fs_link(struct _reent *r, const char *existing, const char *newLink);
static int fs_unlink(struct _reent *r, const char *name);
@ -33,10 +33,10 @@ static int fs_dirreset(struct _reent *r, DIR_ITER *dirState);
static int fs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat);
static int fs_dirclose(struct _reent *r, DIR_ITER *dirState);
static int fs_statvfs(struct _reent *r, const char *path, struct statvfs *buf);
static int fs_ftruncate(struct _reent *r, int fd, off_t len);
static int fs_fsync(struct _reent *r, int fd);
static int fs_ftruncate(struct _reent *r, void *fd, off_t len);
static int fs_fsync(struct _reent *r, void *fd);
static int fs_chmod(struct _reent *r, const char *path, mode_t mode);
static int fs_fchmod(struct _reent *r, int fd, mode_t mode);
static int fs_fchmod(struct _reent *r, void *fd, mode_t mode);
static int fs_rmdir(struct _reent *r, const char *name);
/**
@ -292,7 +292,7 @@ fs_open(struct _reent *r,
static int
fs_close(struct _reent *r,
int fd)
void *fd)
{
FSStatus rc;
fs_file_t *file = (fs_file_t*)fd;
@ -313,7 +313,7 @@ fs_close(struct _reent *r,
static ssize_t
fs_write(struct _reent *r,
int fd,
void *fd,
const char *ptr,
size_t len)
{
@ -373,7 +373,7 @@ fs_write(struct _reent *r,
static ssize_t
fs_write_safe(struct _reent *r,
int fd,
void *fd,
const char *ptr,
size_t len)
{
@ -433,7 +433,7 @@ fs_write_safe(struct _reent *r,
static ssize_t
fs_read(struct _reent *r,
int fd,
void *fd,
char *ptr,
size_t len)
{
@ -502,7 +502,7 @@ fs_read(struct _reent *r,
static off_t
fs_seek(struct _reent *r,
int fd,
void *fd,
off_t pos,
int whence)
{
@ -565,7 +565,7 @@ fs_seek(struct _reent *r,
static int
fs_fstat(struct _reent *r,
int fd,
void *fd,
struct stat *st)
{
FSStatus rc;
@ -613,7 +613,7 @@ fs_stat(struct _reent *r,
if (rc >= 0) {
fs_file_t tmpfd = { .fd = fd };
rc = fs_fstat(r, (int)&tmpfd, st);
rc = fs_fstat(r, &tmpfd, st);
FSCloseFile(fsClient, &fsCmd, fd, -1);
return rc;
}
@ -940,7 +940,7 @@ fs_statvfs(struct _reent *r,
static int
fs_ftruncate(struct _reent *r,
int fd,
void *fd,
off_t len)
{
FSStatus rc;
@ -975,7 +975,7 @@ fs_ftruncate(struct _reent *r,
static int
fs_fsync(struct _reent *r,
int fd)
void *fd)
{
FSStatus rc;
fs_file_t *file = (fs_file_t*)fd;
@ -1024,7 +1024,7 @@ fs_chmod(struct _reent *r,
static int
fs_fchmod(struct _reent *r,
int fd,
void *fd,
mode_t mode)
{
//TODO: FSChangeMode and FSStatFile?

View File

@ -0,0 +1,29 @@
#pragma once
#include <wut.h>
/**
* \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(char * stringLocation);
#ifdef __cplusplus
}
#endif
/** @} */

View File

@ -0,0 +1,27 @@
#pragma once
/**
* \defgroup whb_socketinit Socket Library Initialization Manager
* \ingroup whb
* @{
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Socket Library (nsysnet.rpl)
*/
void
WHBInitializeSocketLibrary();
void
WHBDeinitializeSocketLibrary();
#ifdef __cplusplus
}
#endif
/** @} */

View File

@ -16,9 +16,18 @@ typedef void (*LogHandlerFn)(const char *msg);
BOOL
WHBAddLogHandler(LogHandlerFn fn);
BOOL
WHBRemoveLogHandler(LogHandlerFn fn);
BOOL
WHBLogWrite(const char *str);
BOOL
WHBLogPrint(const char *str);
BOOL
WHBLogWritef(const char *fmt, ...);
BOOL
WHBLogPrintf(const char *fmt, ...);

View File

@ -14,6 +14,9 @@ extern "C" {
BOOL
WHBLogCafeInit();
BOOL
WHBLogCafeDeinit();
#ifdef __cplusplus
}
#endif

View File

@ -14,6 +14,9 @@ extern "C" {
BOOL
WHBLogUdpInit();
BOOL
WHBLogUdpDeinit();
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,138 @@
#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;
int
sClient = -1;
struct sockaddr_in
sAddr;
static inline void
closeSocket(const char * funcName)
{
int ret = socketclose(sSocket);
if(ret < 0) {
WHBLogPrintf("%s: Error occurred closing socket: %d", funcName, socketlasterr());
}
else {
sSocket = -1;
}
}
static inline void
closeClient(const char * funcName)
{
int ret = socketclose(sClient);
if(ret < 0) {
WHBLogPrintf("%s: Error occurred closing client socket: %d", funcName, socketlasterr());
}
else {
sClient = -1;
}
}
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__);
return FALSE;
}
ret = listen(sSocket, 3);
if(ret < 0) {
WHBLogPrintf("%s: Error occurred while setting socket to listen mode: %d", __FUNCTION__, socketlasterr());
closeSocket(__FUNCTION__);
return FALSE;
}
return TRUE;
}
void
WHBCommandServerStop()
{
if(sSocket < 0) {
WHBLogPrintf("%s: Socket is already closed.", __FUNCTION__);
return;
}
if(sClient >= 0) {
closeClient(__FUNCTION__);
}
closeSocket(__FUNCTION__);
WHBDeinitializeSocketLibrary();
}
BOOL
WHBCommandServerListen(char * stringLocation)
{
char buffer[WHB_SERVER_BUFFER_SIZE];
memset(buffer, 0, WHB_SERVER_BUFFER_SIZE);
int ret;
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;
}
if(sClient < 0) {
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__);
return FALSE;
}
if(ret == 0) {
WHBLogPrintf("%s: Remote socket was closed. Closing client connection...", __FUNCTION__);
closeClient(__FUNCTION__);
return FALSE;
}
memcpy(stringLocation, buffer, WHB_SERVER_BUFFER_SIZE);
return TRUE;
}

View File

@ -0,0 +1,33 @@
#include <nsysnet/socket.h>
/**
* Socket Library (nsysnet.rpl)
*/
static BOOL
isSocketInitialized = FALSE;
static unsigned int
numberOfSocketClients = 0;
void
WHBInitializeSocketLibrary()
{
if(!isSocketInitialized) {
isSocketInitialized = TRUE;
socket_lib_init();
}
numberOfSocketClients++;
}
void
WHBDeinitializeSocketLibrary()
{
if(numberOfSocketClients > 0) {
numberOfSocketClients--;
}
if(isSocketInitialized && numberOfSocketClients == 0) {
isSocketInitialized = FALSE;
socket_lib_finish();
}
}

View File

@ -10,6 +10,17 @@
static LogHandlerFn
sHandlers[MAX_HANDLERS] = { 0 };
static inline void
dispatchMessage(const char * str)
{
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sHandlers[i]) {
sHandlers[i](str);
}
}
}
BOOL
WHBAddLogHandler(LogHandlerFn fn)
{
@ -26,36 +37,85 @@ WHBAddLogHandler(LogHandlerFn fn)
}
BOOL
WHBLogPrint(const char *str)
WHBRemoveLogHandler(LogHandlerFn fn)
{
int i;
for(i = 0; i < MAX_HANDLERS; ++i) {
if (sHandlers[i]) {
sHandlers[i](str);
if(sHandlers[i] == fn) {
sHandlers[i] = NULL;
return TRUE;
}
}
return FALSE;
}
BOOL
WHBLogWrite(const char *str)
{
dispatchMessage(str);
return TRUE;
}
BOOL
WHBLogPrintf(const char *fmt, ...)
WHBLogPrint(const char *str)
{
char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4);
if(!buf) {
return FALSE;
}
snprintf(buf, PRINTF_BUFFER_LENGTH, "%s\n", str);
dispatchMessage(buf);
MEMFreeToDefaultHeap(buf);
return TRUE;
}
BOOL
WHBLogWritef(const char *fmt, ...)
{
char *buf = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4);
va_list va;
BOOL result;
if (!buf) {
return FALSE;
}
va_start(va, fmt);
vsnprintf(buf, PRINTF_BUFFER_LENGTH, fmt, va);
result = WHBLogPrint(buf);
dispatchMessage(buf);
MEMFreeToDefaultHeap(buf);
va_end(va);
return result;
return TRUE;
}
BOOL
WHBLogPrintf(const char *fmt, ...)
{
char *buf1 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4);
char *buf2 = MEMAllocFromDefaultHeapEx(PRINTF_BUFFER_LENGTH, 4);
va_list va;
if (!buf1) {
return FALSE;
}
if(!buf2) {
MEMFreeToDefaultHeap(buf1);
return FALSE;
}
va_start(va, fmt);
vsnprintf(buf1, PRINTF_BUFFER_LENGTH, fmt, va);
snprintf(buf2, PRINTF_BUFFER_LENGTH, "%s\n", buf1);
dispatchMessage(buf2);
MEMFreeToDefaultHeap(buf1);
MEMFreeToDefaultHeap(buf2);
va_end(va);
return TRUE;
}

View File

@ -1,15 +1,29 @@
#include <coreinit/debug.h>
#include <whb/log.h>
#include <string.h>
static void
cafeLogHandler(const char * msg)
{
int length = strlen(msg);
if(msg[length-1] != '\n') {
OSReport("%s\n", msg);
}
else {
OSReport(msg);
}
}
BOOL
WHBLogCafeInit()
{
WHBAddLogHandler(cafeLogHandler);
return TRUE;
return WHBAddLogHandler(cafeLogHandler);
}
BOOL
WHBLogCafeDeinit()
{
return WHBRemoveLogHandler(cafeLogHandler);
}

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <whb/libmanager.h>
static int
sSocket = -1;
@ -28,7 +29,7 @@ BOOL
WHBLogUdpInit()
{
int broadcastEnable = 1;
socket_lib_init();
WHBInitializeSocketLibrary();
sSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sSocket < 0) {
@ -42,6 +43,16 @@ WHBLogUdpInit()
sSendAddr.sin_port = htons(SERVER_PORT);
sSendAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
WHBAddLogHandler(udpLogHandler);
return TRUE;
return WHBAddLogHandler(udpLogHandler);
}
BOOL
WHBLogUdpDeinit()
{
if(shutdown(sSocket, SHUT_WR) != 0) {
return FALSE;
}
WHBDeinitializeSocketLibrary();
return WHBRemoveLogHandler(udpLogHandler);
}

View File

@ -7,8 +7,8 @@ add_executable(implcheck ${SOURCE_FILES} ${HEADER_FILES})
set_target_properties(implcheck PROPERTIES FOLDER tools)
target_link_libraries(implcheck PRIVATE
${CPPFORMAT_LINK}
${EXCMD_LINK}
${FMTLIB_LINK}
${ZLIB_LINK})
install(TARGETS implcheck RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")

View File

@ -1,6 +1,6 @@
#include <algorithm>
#include <excmd.h>
#include <format.h>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <vector>

View File

@ -7,8 +7,8 @@ add_executable(readrpl ${SOURCE_FILES} ${HEADER_FILES})
set_target_properties(readrpl PROPERTIES FOLDER tools)
target_link_libraries(readrpl PRIVATE
${CPPFORMAT_LINK}
${EXCMD_LINK}
${FMTLIB_LINK}
${ZLIB_LINK})
install(TARGETS readrpl RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")

View File

@ -1,5 +1,5 @@
#include <excmd.h>
#include <format.h>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <vector>
@ -700,7 +700,7 @@ int main(int argc, char **argv)
if (header.magic != elf::HeaderMagic) {
std::cout << "Invalid ELF magic header" << std::endl;
return false;
return -1;
}
// Read sections

View File

@ -90,7 +90,8 @@ int main(int argc, char **argv)
if (recvd > 0) {
buffer[recvd] = 0;
std::cout << buffer << std::endl;
std::cout << buffer;
std::cout.flush();
}
}
}