forked from cafeberry/cafeberry
85 lines
1.9 KiB
C++
85 lines
1.9 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "NetworkSocketManager.h"
|
|
|
|
#include <net.h>
|
|
|
|
// PIEBOT: Adding this because Sony systems uses BSD apis
|
|
// which uses socketclose instead of closesocket.
|
|
int NetworkSocketManager::CloseSocket(SOCKET sock)
|
|
{
|
|
return socketclose(sock);
|
|
}
|
|
|
|
int NetworkSocketManager::getSocketError(SOCKET socket)
|
|
{
|
|
return socket;
|
|
}
|
|
|
|
bool NetworkSocketManager::IsSocketValid(SOCKET sock)
|
|
{
|
|
return sock >= 0;
|
|
}
|
|
|
|
bool NetworkSocketManager::IsSentValid(int sent)
|
|
{
|
|
return sent >= 0;
|
|
}
|
|
|
|
bool NetworkSocketManager::InitNetwork()
|
|
{
|
|
SceNetInitParam param;
|
|
static char memory[16 * 1024];
|
|
|
|
param.memory = memory;
|
|
param.size = sizeof(memory);
|
|
param.flags = 0;
|
|
|
|
int result = sceNetInit(¶m);
|
|
// libnet is brought up before us by the platform layer, which reports EBUSY here.
|
|
if (result < 0 && result != SCE_NET_ERROR_EBUSY) {
|
|
app.DebugPrintf("sceNetInit failed: 0x%08x\n", result);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
SOCKET NetworkSocketManager::MakeHostSocket(int port)
|
|
{
|
|
SOCKET s_listenSocket = NULL;
|
|
|
|
SceNetSockaddrIn addr = {};
|
|
addr.sin_len = sizeof(addr);
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
addr.sin_port = htons((uint16_t)port);
|
|
|
|
s_listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
if (s_listenSocket < 0)
|
|
{
|
|
app.DebugPrintf("socket() failed: %d\n", s_listenSocket);
|
|
return INVALID_SOCKET;
|
|
}
|
|
|
|
// PIEBOT: time for part 2, binding and listening.
|
|
int opt = 1;
|
|
setsockopt(s_listenSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
|
|
|
|
int iResult = ::bind(s_listenSocket, (const sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (iResult < 0)
|
|
{
|
|
app.DebugPrintf("bind() failed: %d\n", iResult);
|
|
closesocket(s_listenSocket);
|
|
return INVALID_SOCKET;
|
|
}
|
|
|
|
iResult = listen(s_listenSocket, 5);
|
|
if (iResult < 0)
|
|
{
|
|
app.DebugPrintf("listen() failed: %d\n", iResult);
|
|
closesocket(s_listenSocket);
|
|
return INVALID_SOCKET;
|
|
}
|
|
|
|
return s_listenSocket;
|
|
} |