2022-06-16 21:33:18 +00:00
|
|
|
#include "server/SocketClient.hpp"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2022-09-04 09:23:02 +00:00
|
|
|
#include <basis/seadNew.h>
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-07-05 19:45:22 +00:00
|
|
|
#include "SocketBase.hpp"
|
2022-09-04 09:23:02 +00:00
|
|
|
#include "al/async/FunctorV0M.hpp"
|
2022-06-16 21:33:18 +00:00
|
|
|
#include "logger.hpp"
|
|
|
|
#include "nn/result.h"
|
|
|
|
#include "nn/socket.h"
|
|
|
|
#include "packets/Packet.h"
|
2022-09-03 08:57:57 +00:00
|
|
|
#include "packets/UdpPacket.h"
|
2022-09-04 09:23:02 +00:00
|
|
|
#include "server/Client.hpp"
|
|
|
|
#include "thread/seadMessageQueue.h"
|
2022-06-16 21:33:18 +00:00
|
|
|
#include "types.h"
|
|
|
|
|
2022-09-04 09:23:02 +00:00
|
|
|
SocketClient::SocketClient(const char* name, sead::Heap* heap) : mHeap(heap), SocketBase(name) {
|
|
|
|
|
|
|
|
mRecvThread = new al::AsyncFunctorThread("SocketRecvThread", al::FunctorV0M<SocketClient*, SocketThreadFunc>(this, &SocketClient::recvFunc), 0, 0x1000, {0});
|
|
|
|
mSendThread = new al::AsyncFunctorThread("SocketSendThread", al::FunctorV0M<SocketClient*, SocketThreadFunc>(this, &SocketClient::sendFunc), 0, 0x1000, {0});
|
|
|
|
|
|
|
|
mRecvQueue.allocate(maxBufSize, mHeap);
|
|
|
|
mSendQueue.allocate(maxBufSize, mHeap);
|
2022-10-26 10:33:42 +00:00
|
|
|
recvBuf = (char*)mHeap->alloc(MAXPACKSIZE+1);
|
2022-09-04 09:23:02 +00:00
|
|
|
};
|
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
nn::Result SocketClient::init(const char* ip, u16 port) {
|
|
|
|
|
2022-07-10 06:13:28 +00:00
|
|
|
this->sock_ip = ip;
|
|
|
|
this->port = port;
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-07-10 06:13:28 +00:00
|
|
|
in_addr hostAddress = { 0 };
|
2022-06-16 21:33:18 +00:00
|
|
|
sockaddr serverAddress = { 0 };
|
2022-10-26 09:44:09 +00:00
|
|
|
sockaddr udpAddress = { 0 };
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-07-05 19:45:22 +00:00
|
|
|
Logger::log("SocketClient::init: %s:%d sock %s\n", ip, port, getStateChar());
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
nn::nifm::Initialize();
|
|
|
|
nn::nifm::SubmitNetworkRequest();
|
|
|
|
|
|
|
|
while (nn::nifm::IsNetworkRequestOnHold()) { }
|
|
|
|
|
|
|
|
// emulators (ryujinx) make this return false always, so skip it during init
|
|
|
|
#ifndef EMU
|
|
|
|
if (!nn::nifm::IsNetworkAvailable()) {
|
|
|
|
Logger::log("Network Unavailable.\n");
|
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-06-16 21:33:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-10 06:13:28 +00:00
|
|
|
if ((this->socket_log_socket = nn::socket::Socket(2, 1, 6)) < 0) {
|
2022-06-16 21:33:18 +00:00
|
|
|
Logger::log("Socket Unavailable.\n");
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-06-16 21:33:18 +00:00
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:13:28 +00:00
|
|
|
if (! this->stringToIPAddress(this->sock_ip, &hostAddress)) {
|
|
|
|
Logger::log("IP address is invalid or hostname not resolveable.\n");
|
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
serverAddress.address = hostAddress;
|
|
|
|
serverAddress.port = nn::socket::InetHtons(this->port);
|
|
|
|
serverAddress.family = 2;
|
|
|
|
|
2022-06-19 01:37:46 +00:00
|
|
|
int sockOptValue = true;
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-06-19 01:37:46 +00:00
|
|
|
nn::socket::SetSockOpt(this->socket_log_socket, 6, TCP_NODELAY, &sockOptValue, sizeof(sockOptValue));
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
nn::Result result;
|
|
|
|
|
|
|
|
if((result = nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress))).isFailure()) {
|
2022-07-05 19:45:22 +00:00
|
|
|
Logger::log("Socket Connection Failed!\n");
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-06-16 21:33:18 +00:00
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:09:59 +00:00
|
|
|
if ((this->mUdpSocket = nn::socket::Socket(2, 2, 17)) < 0) {
|
2022-08-30 22:18:44 +00:00
|
|
|
Logger::log("Udp Socket failed to create");
|
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return -1;
|
2022-10-26 02:33:58 +00:00
|
|
|
}
|
2022-09-05 10:26:01 +00:00
|
|
|
|
2022-09-03 08:57:57 +00:00
|
|
|
udpAddress.address = hostAddress;
|
2022-10-26 02:41:09 +00:00
|
|
|
udpAddress.port = 0;
|
2022-08-30 22:18:44 +00:00
|
|
|
udpAddress.family = 2;
|
2022-10-27 07:25:37 +00:00
|
|
|
this->mUdpAddress = udpAddress;
|
|
|
|
this->mHasRecvUdp = false;
|
2022-10-27 08:02:32 +00:00
|
|
|
this->mPacketQueueOpen = true;
|
2022-09-05 10:19:33 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
this->socket_log_state = SOCKET_LOG_CONNECTED;
|
|
|
|
|
2022-07-10 01:24:00 +00:00
|
|
|
Logger::log("Socket fd: %d\n", socket_log_socket);
|
|
|
|
|
2022-09-04 09:23:02 +00:00
|
|
|
startThreads(); // start recv and send threads after sucessful connection
|
|
|
|
|
|
|
|
// send init packet to server once we connect (an issue with the server prevents this from working properly, waiting for a fix to implement)
|
|
|
|
|
|
|
|
PlayerConnect initPacket;
|
|
|
|
initPacket.mUserID = Client::getClientId();
|
|
|
|
strcpy(initPacket.clientName, Client::getUsername().cstr());
|
|
|
|
|
|
|
|
if (mIsFirstConnect) {
|
|
|
|
initPacket.conType = ConnectionTypes::INIT;
|
|
|
|
mIsFirstConnect = false;
|
|
|
|
} else {
|
|
|
|
initPacket.conType = ConnectionTypes::RECONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(&initPacket);
|
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:37:47 +00:00
|
|
|
u16 SocketClient::getLocalUdpPort() {
|
2022-09-05 10:19:33 +00:00
|
|
|
sockaddr udpAddress = { 0 };
|
|
|
|
u32 size = sizeof(udpAddress);
|
|
|
|
|
|
|
|
nn::Result result;
|
2022-10-27 09:09:59 +00:00
|
|
|
if((result = nn::socket::GetSockName(this->mUdpSocket, &udpAddress, &size)).isFailure()) {
|
2022-09-05 10:19:33 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-09-03 08:57:57 +00:00
|
|
|
return 0;
|
2022-09-05 10:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nn::socket::InetNtohs(udpAddress.port);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s32 SocketClient::setPeerUdpPort(u16 port) {
|
|
|
|
u16 net_port = nn::socket::InetHtons(port);
|
2022-10-27 07:25:37 +00:00
|
|
|
this->mUdpAddress.port = net_port;
|
2022-09-05 10:19:33 +00:00
|
|
|
|
|
|
|
nn::Result result;
|
2022-10-27 09:10:44 +00:00
|
|
|
if((result = nn::socket::Connect(this->mUdpSocket, &this->mUdpAddress, sizeof(this->mUdpAddress))).isFailure()) {
|
|
|
|
Logger::log("Udp socket connection failed to connect to port %d!\n", port);
|
2022-09-05 10:19:33 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-09-03 08:57:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:37:08 +00:00
|
|
|
const char* SocketClient::getUdpStateChar() {
|
2022-10-27 07:25:37 +00:00
|
|
|
if (this->mUdpAddress.port == 0) {
|
2022-10-26 02:33:58 +00:00
|
|
|
return "Waiting for handshake";
|
|
|
|
}
|
|
|
|
|
2022-10-27 07:25:37 +00:00
|
|
|
if (!this->mHasRecvUdp) {
|
2022-10-26 02:33:58 +00:00
|
|
|
return "Waiting for holepunch";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Utilizing UDP";
|
2022-10-24 01:37:08 +00:00
|
|
|
}
|
2022-09-04 09:23:02 +00:00
|
|
|
bool SocketClient::send(Packet *packet) {
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (this->socket_log_state != SOCKET_LOG_CONNECTED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char* buffer = reinterpret_cast<char*>(packet);
|
|
|
|
|
|
|
|
int valread = 0;
|
|
|
|
|
2022-10-26 02:33:58 +00:00
|
|
|
int fd = -1;
|
2022-10-27 07:25:37 +00:00
|
|
|
if ((packet->mType != PLAYERINF && packet->mType != HACKCAPINF && packet->mType != HOLEPUNCH) || !this->mHasRecvUdp) {
|
2022-10-26 02:33:58 +00:00
|
|
|
Logger::log("Sending packet: %s\n", packetNames[packet->mType]);
|
|
|
|
fd = this->socket_log_socket;
|
|
|
|
} else {
|
2022-08-30 22:18:44 +00:00
|
|
|
|
2022-10-27 09:09:59 +00:00
|
|
|
fd = this->mUdpSocket;
|
2022-10-26 02:33:58 +00:00
|
|
|
}
|
2022-08-30 22:18:44 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-10-26 02:33:58 +00:00
|
|
|
if ((valread = nn::socket::Send(fd, buffer, packet->mPacketSize + sizeof(Packet), 0) > 0)) {
|
2022-06-16 21:33:18 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Logger::log("Failed to Fully Send Packet! Result: %d Type: %s Packet Size: %d\n", valread, packetNames[packet->mType], packet->mPacketSize);
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-09-04 09:23:02 +00:00
|
|
|
this->tryReconnect();
|
2022-06-16 21:33:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-04 09:23:02 +00:00
|
|
|
bool SocketClient::recv() {
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (this->socket_log_state != SOCKET_LOG_CONNECTED) {
|
2022-06-26 03:13:05 +00:00
|
|
|
Logger::log("Unable To Receive! Socket Not Connected.\n");
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-09-04 09:23:02 +00:00
|
|
|
return this->tryReconnect();
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int headerSize = sizeof(Packet);
|
|
|
|
int valread = 0;
|
|
|
|
|
2022-10-26 02:33:58 +00:00
|
|
|
const int fd_count = 2;
|
|
|
|
struct pollfd pfds[fd_count] = {{0}, {0}};
|
|
|
|
pfds[0].fd = this->socket_log_socket;
|
|
|
|
pfds[0].events = 1;
|
|
|
|
pfds[0].revents = 0;
|
2022-10-27 09:09:59 +00:00
|
|
|
pfds[1].fd = this->mUdpSocket;
|
2022-10-26 02:33:58 +00:00
|
|
|
pfds[1].events = 1;
|
|
|
|
pfds[1].revents = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (nn::socket::Poll(pfds, fd_count, 0) <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 fd = -1;
|
|
|
|
s32 index = -1;
|
|
|
|
for (int i = 0; i < fd_count; i++){
|
|
|
|
if (pfds[i].revents & 1) {
|
|
|
|
fd = pfds[i].fd;
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == 1) {
|
2022-10-26 10:33:42 +00:00
|
|
|
int result = nn::socket::Recv(fd, recvBuf, sizeof(MAXPACKSIZE), this->sock_flags);
|
2022-09-05 10:19:33 +00:00
|
|
|
if (result < headerSize){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:33:42 +00:00
|
|
|
Packet* header = reinterpret_cast<Packet*>(recvBuf);
|
2022-09-05 10:19:33 +00:00
|
|
|
int fullSize = header->mPacketSize + sizeof(Packet);
|
2022-10-26 02:33:58 +00:00
|
|
|
// Verify packet size is appropriate
|
2022-09-05 10:19:33 +00:00
|
|
|
if (result < fullSize || result > MAXPACKSIZE || fullSize > MAXPACKSIZE){
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-26 02:33:58 +00:00
|
|
|
|
|
|
|
// Verify type of packet
|
2022-09-05 10:19:33 +00:00
|
|
|
if (!(header->mType > PacketType::UNKNOWN && header->mType < PacketType::End)) {
|
|
|
|
Logger::log("Failed to acquire valid packet type! Packet Type: %d Full Packet Size %d valread size: %d", header->mType, fullSize, result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-27 07:25:37 +00:00
|
|
|
this->mHasRecvUdp = true;
|
2022-10-26 02:33:58 +00:00
|
|
|
|
2022-10-23 23:37:47 +00:00
|
|
|
char* packetBuf = (char*)mHeap->alloc(fullSize);
|
|
|
|
if (packetBuf){
|
2022-10-26 10:33:42 +00:00
|
|
|
memcpy(packetBuf, recvBuf, fullSize);
|
2022-09-05 10:19:33 +00:00
|
|
|
|
|
|
|
|
2022-10-26 02:33:58 +00:00
|
|
|
Packet *packet = reinterpret_cast<Packet*>(packetBuf);
|
2022-09-05 10:19:33 +00:00
|
|
|
|
2022-10-26 02:33:58 +00:00
|
|
|
if(!mRecvQueue.isFull()) {
|
|
|
|
mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking);
|
|
|
|
} else {
|
|
|
|
mHeap->free(packetBuf);
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 10:19:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-30 22:18:44 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
// read only the size of a header
|
|
|
|
while(valread < headerSize) {
|
2022-09-03 08:57:57 +00:00
|
|
|
int result = 0;
|
2022-10-26 10:33:42 +00:00
|
|
|
result = nn::socket::Recv(fd, recvBuf + valread,
|
2022-10-26 02:33:58 +00:00
|
|
|
headerSize - valread, this->sock_flags);
|
2022-07-05 19:45:22 +00:00
|
|
|
|
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
if(result > 0) {
|
|
|
|
valread += result;
|
|
|
|
} else {
|
2022-07-06 21:42:41 +00:00
|
|
|
if(this->socket_errno==11){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Logger::log("Header Read Failed! Value: %d Total Read: %d\n", result, valread);
|
2022-09-04 09:23:02 +00:00
|
|
|
return this->tryReconnect(); // if we sucessfully reconnect, we dont want
|
2022-07-06 21:42:41 +00:00
|
|
|
}
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(valread > 0) {
|
2022-10-26 10:33:42 +00:00
|
|
|
Packet* header = reinterpret_cast<Packet*>(recvBuf);
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
int fullSize = header->mPacketSize + sizeof(Packet);
|
|
|
|
|
2022-08-27 08:47:52 +00:00
|
|
|
if (fullSize <= MAXPACKSIZE && fullSize > 0 && valread == sizeof(Packet)) {
|
2022-08-10 21:11:52 +00:00
|
|
|
|
|
|
|
if (header->mType != PLAYERINF && header->mType != HACKCAPINF) {
|
|
|
|
Logger::log("Received packet (from %02X%02X):", header->mUserID.data[0],
|
|
|
|
header->mUserID.data[1]);
|
|
|
|
Logger::disableName();
|
|
|
|
Logger::log(" Size: %d", header->mPacketSize);
|
|
|
|
Logger::log(" Type: %d", header->mType);
|
|
|
|
if(packetNames[header->mType])
|
|
|
|
Logger::log(" Type String: %s\n", packetNames[header->mType]);
|
|
|
|
Logger::enableName();
|
|
|
|
}
|
2022-06-26 03:13:05 +00:00
|
|
|
|
2022-09-04 09:23:02 +00:00
|
|
|
char* packetBuf = (char*)mHeap->alloc(fullSize);
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (packetBuf) {
|
|
|
|
|
2022-10-26 10:33:42 +00:00
|
|
|
memcpy(packetBuf, recvBuf, sizeof(Packet));
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
while (valread < fullSize) {
|
|
|
|
|
2022-08-30 22:18:44 +00:00
|
|
|
int result = nn::socket::Recv(fd, packetBuf + valread,
|
2022-07-05 19:45:22 +00:00
|
|
|
fullSize - valread, this->sock_flags);
|
|
|
|
|
2022-09-05 10:28:27 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
valread += result;
|
2022-07-05 19:45:22 +00:00
|
|
|
} else {
|
2022-09-04 09:23:02 +00:00
|
|
|
mHeap->free(packetBuf);
|
2022-06-16 21:33:18 +00:00
|
|
|
Logger::log("Packet Read Failed! Value: %d\nPacket Size: %d\nPacket Type: %s\n", result, header->mPacketSize, packetNames[header->mType]);
|
2022-09-04 09:23:02 +00:00
|
|
|
return this->tryReconnect();
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 08:47:52 +00:00
|
|
|
if (!(header->mType > PacketType::UNKNOWN && header->mType < PacketType::End)) {
|
|
|
|
Logger::log("Failed to acquire valid packet type! Packet Type: %d Full Packet Size %d valread size: %d", header->mType, fullSize, valread);
|
2022-10-23 21:41:16 +00:00
|
|
|
mHeap->free(packetBuf);
|
2022-08-27 08:47:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
Packet *packet = reinterpret_cast<Packet*>(packetBuf);
|
|
|
|
|
2022-10-27 08:02:32 +00:00
|
|
|
if (!mRecvQueue.isFull() && mPacketQueueOpen) {
|
2022-09-04 09:23:02 +00:00
|
|
|
mRecvQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking);
|
2022-06-16 21:33:18 +00:00
|
|
|
} else {
|
2022-09-04 09:23:02 +00:00
|
|
|
mHeap->free(packetBuf);
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-10 01:24:00 +00:00
|
|
|
} else {
|
2022-10-27 09:09:59 +00:00
|
|
|
Logger::log("Failed to acquire valid data! Packet Type: %d Full Packet Size %d valread size: %d\n", header->mType, fullSize, valread);
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else { // if we error'd, close the socket
|
|
|
|
Logger::log("valread was zero! Disconnecting.\n");
|
2022-06-19 01:36:00 +00:00
|
|
|
this->socket_errno = nn::socket::GetLastErrno();
|
2022-09-04 09:23:02 +00:00
|
|
|
return this->tryReconnect();
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prints packet to debug logger
|
|
|
|
void SocketClient::printPacket(Packet *packet) {
|
|
|
|
packet->mUserID.print();
|
|
|
|
Logger::log("Type: %s\n", packetNames[packet->mType]);
|
|
|
|
|
|
|
|
switch (packet->mType)
|
|
|
|
{
|
|
|
|
case PacketType::PLAYERINF:
|
|
|
|
Logger::log("Pos X: %f Pos Y: %f Pos Z: %f\n", ((PlayerInf*)packet)->playerPos.x, ((PlayerInf*)packet)->playerPos.y, ((PlayerInf*)packet)->playerPos.z);
|
|
|
|
Logger::log("Rot X: %f Rot Y: %f Rot Z: %f\nRot W: %f\n", ((PlayerInf*)packet)->playerRot.x, ((PlayerInf*)packet)->playerRot.y, ((PlayerInf*)packet)->playerRot.z, ((PlayerInf*)packet)->playerRot.w);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 09:23:02 +00:00
|
|
|
bool SocketClient::tryReconnect() {
|
|
|
|
|
|
|
|
Logger::log("Attempting to Reconnect.\n");
|
|
|
|
|
|
|
|
if (closeSocket()) { // unfortunately we cannot use the same fd from the previous connection, so close the socket entirely and attempt a new connection.
|
|
|
|
if (init(sock_ip, port).isSuccess()) { // call init again
|
|
|
|
Logger::log("Reconnect Successful.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:45:22 +00:00
|
|
|
bool SocketClient::closeSocket() {
|
|
|
|
|
|
|
|
Logger::log("Closing Socket.\n");
|
|
|
|
|
2022-10-27 07:25:37 +00:00
|
|
|
mHasRecvUdp = false;
|
|
|
|
mUdpAddress.port = 0;
|
2022-10-27 08:02:32 +00:00
|
|
|
mPacketQueueOpen = false;
|
|
|
|
|
2022-07-10 01:24:00 +00:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (!(result = SocketBase::closeSocket())) {
|
|
|
|
Logger::log("Failed to close socket!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-07-10 06:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketClient::stringToIPAddress(const char* str, in_addr* out) {
|
|
|
|
// string to IPv4
|
|
|
|
if (nn::socket::InetAton(str, out)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get IPs via DNS
|
|
|
|
struct hostent *he = nn::socket::GetHostByName(str);
|
|
|
|
if (! he) { return false; }
|
|
|
|
|
|
|
|
// might give us multiple IP addresses, so pick the first one
|
|
|
|
struct in_addr **addr_list = (struct in_addr **) he->h_addr_list;
|
|
|
|
for (int i = 0 ; addr_list[i] != NULL ; i++) {
|
|
|
|
*out = *addr_list[i];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-04 09:23:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief starts client read thread
|
|
|
|
*
|
|
|
|
* @return true if read thread was sucessfully started
|
|
|
|
* @return false if read thread was unable to start, or thread was already started.
|
|
|
|
*/
|
|
|
|
bool SocketClient::startThreads() {
|
|
|
|
|
|
|
|
Logger::log("Recv Thread isDone: %s\n", BTOC(this->mRecvThread->isDone()));
|
|
|
|
Logger::log("Send Thread isDone: %s\n", BTOC(this->mSendThread->isDone()));
|
|
|
|
|
|
|
|
if(this->mRecvThread->isDone() && this->mSendThread->isDone()) {
|
|
|
|
this->mRecvThread->start();
|
|
|
|
this->mSendThread->start();
|
|
|
|
Logger::log("Socket threads sucessfully started.\n");
|
|
|
|
return true;
|
|
|
|
}else {
|
|
|
|
Logger::log("Socket threads failed to start.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketClient::endThreads() {
|
|
|
|
mRecvThread->mDelegateThread->destroy();
|
|
|
|
mSendThread->mDelegateThread->destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketClient::sendFunc() {
|
|
|
|
|
|
|
|
Logger::log("Starting Send Thread.\n");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
trySendQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::log("Ending Send Thread.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketClient::recvFunc() {
|
|
|
|
|
|
|
|
nn::socket::Recv(this->socket_log_socket, nullptr, 0, 0);
|
|
|
|
|
|
|
|
Logger::log("Starting Recv Thread.\n");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!recv()) {
|
|
|
|
Logger::log("Receiving Packet Failed!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::log("Ending Recv Thread.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketClient::queuePacket(Packet* packet) {
|
2022-10-27 08:02:32 +00:00
|
|
|
if (socket_log_state == SOCKET_LOG_CONNECTED && mPacketQueueOpen) {
|
2022-09-04 09:23:02 +00:00
|
|
|
mSendQueue.push((s64)packet,
|
|
|
|
sead::MessageQueue::BlockType::NonBlocking); // as this is non-blocking, it
|
|
|
|
// will always return true.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
mHeap->free(packet);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketClient::trySendQueue() {
|
|
|
|
|
|
|
|
Packet* curPacket = (Packet*)mSendQueue.pop(sead::MessageQueue::BlockType::Blocking);
|
|
|
|
|
|
|
|
send(curPacket);
|
|
|
|
|
|
|
|
mHeap->free(curPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
Packet* SocketClient::tryGetPacket(sead::MessageQueue::BlockType blockType) {
|
|
|
|
return socket_log_state == SOCKET_LOG_CONNECTED ? (Packet*)mRecvQueue.pop(blockType) : nullptr;
|
2022-10-15 07:18:57 +00:00
|
|
|
}
|
2022-10-27 08:02:32 +00:00
|
|
|
|
|
|
|
void SocketClient::clearMessageQueues() {
|
|
|
|
bool prevQueueOpenness = this->mPacketQueueOpen;
|
|
|
|
this->mPacketQueueOpen = false;
|
|
|
|
|
|
|
|
while (mSendQueue.getCount() > 0) {
|
|
|
|
Packet* curPacket = (Packet*)mSendQueue.pop(sead::MessageQueue::BlockType::Blocking);
|
|
|
|
mHeap->free(curPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mRecvQueue.getCount() > 0) {
|
|
|
|
Packet* curPacket = (Packet*)mRecvQueue.pop(sead::MessageQueue::BlockType::Blocking);
|
|
|
|
mHeap->free(curPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->mPacketQueueOpen = prevQueueOpenness;
|
|
|
|
}
|