Skip to content

Commit 7c531e7

Browse files
new socket state thing
1 parent 61ad6cc commit 7c531e7

8 files changed

Lines changed: 41 additions & 33 deletions

File tree

include/SocketBase.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SocketBase {
1616
virtual struct Packet* tryGetPacket() { return nullptr; };
1717

1818
const char* getStateChar();
19-
u8 getLogState();
19+
SockState getLogState();
2020
s32 getFd();
2121

2222
void set_sock_flags(int flags);
@@ -34,7 +34,7 @@ class SocketBase {
3434
const char* sock_ip;
3535

3636
u16 port;
37-
u8 socket_log_state = SOCKET_LOG_UNINITIALIZED;
37+
SockState socket_log_state = SockState::UNINITIALIZED;
3838
s32 socket_log_socket;
3939

4040
int sock_flags;

include/server/SocketClient.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class SocketClient : public SocketBase {
3434
void sendFunc();
3535
void recvFunc();
3636

37-
void setLogState(SocketLogState state) { this->socket_log_state = state; };
37+
void setLogState(SockState state) { this->socket_log_state = state; };
3838
void startEndThread() { this->mEndThread->start(); };
3939

4040
void printPacket(Packet* packet);
41-
bool isConnected() { return socket_log_state == SOCKET_LOG_CONNECTED; }
41+
bool isConnected() { return socket_log_state == SockState::CONNECTED; }
4242

4343
u32 getSendCount() { return mSendQueue.mMessageQueueInner._count; }
4444
u32 getSendMaxCount() { return mSendQueue.mMessageQueueInner._maxCount; }

include/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef unsigned long undefined8;
4646
const u8 MAX_HOSTNAME_LENGTH = 50;
4747
typedef sead::FixedSafeString<MAX_HOSTNAME_LENGTH + 1> hostname;
4848

49-
enum SocketLogState { SOCKET_LOG_UNINITIALIZED = 0, SOCKET_LOG_CONNECTED = 1, SOCKET_LOG_UNAVAILABLE = 2, SOCKET_LOG_DISCONNECTED = 3 };
49+
enum class SockState { UNINITIALIZED = 0, CONNECTED = 1, UNAVAILABLE = 2, DISCONNECTED = 3, NONET = 4, INVALIP = 5, CONNFAIL = 6 };
5050

5151
// typedef signed int ssize_t;
5252

src/server/Client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void Client::restartConnection() {
177177
curInfo->isInSameStage = false;
178178
}
179179

180-
sInstance->mSocket->setLogState(SOCKET_LOG_DISCONNECTED);
180+
sInstance->mSocket->setLogState(SockState::DISCONNECTED);
181181
sInstance->mSocket->startEndThread();
182182

183183
sInstance->mIsConnectionActive = sInstance->mSocket->init(sInstance->mServerIP.cstr(), sInstance->mServerPort).IsSuccess();

src/server/SocketBase.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ SocketBase::SocketBase(const char* name) {
1313

1414
const char* SocketBase::getStateChar() {
1515
switch (this->socket_log_state) {
16-
case SOCKET_LOG_CONNECTED:
16+
case SockState::CONNECTED:
1717
return "Socket Connected";
18-
case SOCKET_LOG_UNAVAILABLE:
18+
case SockState::UNAVAILABLE:
1919
return "Socket Unavailable";
20-
case SOCKET_LOG_UNINITIALIZED:
20+
case SockState::UNINITIALIZED:
2121
return "Socket Unitialized";
22-
case SOCKET_LOG_DISCONNECTED:
22+
case SockState::DISCONNECTED:
2323
return "Socket Disconnected";
24+
case SockState::CONNFAIL:
25+
return "Connection Failed";
26+
case SockState::INVALIP:
27+
return "Invalid IP or hostname";
28+
case SockState::NONET:
29+
return "Network not available";
2430
default:
2531
return "Unknown State";
2632
}
2733
}
2834

29-
u8 SocketBase::getLogState() {
35+
SockState SocketBase::getLogState() {
3036
return this->socket_log_state;
3137
}
3238

@@ -35,14 +41,14 @@ void SocketBase::set_sock_flags(int flags) {
3541
}
3642

3743
s32 SocketBase::socket_log(const char* str) {
38-
if (this->socket_log_state != SOCKET_LOG_CONNECTED)
44+
if (this->socket_log_state != SockState::CONNECTED)
3945
return -1;
4046

4147
return nn::socket::Send(this->socket_log_socket, str, strlen(str), 0);
4248
}
4349

4450
s32 SocketBase::socket_read_char(char* out) {
45-
if (this->socket_log_state != SOCKET_LOG_CONNECTED)
51+
if (this->socket_log_state != SockState::CONNECTED)
4652
return -2;
4753

4854
char buf[0x1000];
@@ -57,15 +63,15 @@ s32 SocketBase::socket_read_char(char* out) {
5763
}
5864

5965
s32 SocketBase::getFd() {
60-
if (this->socket_log_state == SOCKET_LOG_CONNECTED) {
66+
if (this->socket_log_state == SockState::CONNECTED) {
6167
return this->socket_log_socket;
6268
} else {
6369
return -1;
6470
}
6571
}
6672

6773
bool SocketBase::closeSocket() {
68-
this->socket_log_state = SOCKET_LOG_DISCONNECTED; // probably not safe to assume socket will be closed
74+
this->socket_log_state = SockState::DISCONNECTED; // probably not safe to assume socket will be closed
6975

7076
nn::Result result = nn::socket::Close(this->socket_log_socket);
7177

src/server/SocketClient.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
5353
nn::err::ShowApplicationError(mAppErr);
5454

5555
Logger::log("Network Unavailable.\n");
56-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
56+
this->socket_log_state = SockState::NONET;
5757
this->socket_errno = nn::socket::GetLastErrno();
5858

5959
return nn::Result(-1);
@@ -75,7 +75,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
7575

7676
Logger::log("Socket Unavailable.\n");
7777
this->socket_errno = nn::socket::GetLastErrno();
78-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
78+
this->socket_log_state = SockState::UNAVAILABLE;
7979

8080
return nn::Result(-1);
8181
}
@@ -87,7 +87,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
8787

8888
Logger::log("IP address is invalid or hostname not resolveable.\n");
8989
this->socket_errno = nn::socket::GetLastErrno();
90-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
90+
this->socket_log_state = SockState::INVALIP;
9191

9292
return nn::Result(-1);
9393
}
@@ -105,7 +105,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
105105
if ((result = nn::socket::Connect(this->socket_log_socket, (sockaddr*)&serverAddress, sizeof(serverAddress))).IsFailure()) {
106106
Logger::log("Socket Connection Failed!\n");
107107
this->socket_errno = nn::socket::GetLastErrno();
108-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
108+
this->socket_log_state = SockState::CONNFAIL;
109109

110110
strcpy(mAppErr.dialog_message, "Connection Failed");
111111
strcpy(mAppErr.fullscreen_message, "Failed to connect to server");
@@ -114,7 +114,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
114114
return result;
115115
}
116116

117-
this->socket_log_state = SOCKET_LOG_CONNECTED;
117+
this->socket_log_state = SockState::CONNECTED;
118118

119119
Logger::log("Socket fd: %d\n", socket_log_socket);
120120

@@ -144,7 +144,7 @@ nn::Result SocketClient::init(const char* ip, u16 port) {
144144
}
145145

146146
bool SocketClient::send(Packet* packet) {
147-
if (this->socket_log_state != SOCKET_LOG_CONNECTED || packet == nullptr)
147+
if (this->socket_log_state != SockState::CONNECTED || packet == nullptr)
148148
return false;
149149

150150
char* buffer = reinterpret_cast<char*>(packet);
@@ -166,7 +166,7 @@ bool SocketClient::send(Packet* packet) {
166166
}
167167

168168
bool SocketClient::recv() {
169-
if (this->socket_log_state != SOCKET_LOG_CONNECTED) {
169+
if (this->socket_log_state != SockState::CONNECTED) {
170170
Logger::log("Unable To Receive! Socket Not Connected.\n");
171171
this->socket_errno = nn::socket::GetLastErrno();
172172
return this->tryReconnect();
@@ -345,7 +345,7 @@ void SocketClient::endThreads() {
345345
void SocketClient::sendFunc() {
346346
Logger::log("Starting Send Thread.\n");
347347

348-
while (trySendQueue() || socket_log_state != SOCKET_LOG_DISCONNECTED) {
348+
while (trySendQueue() || socket_log_state != SockState::DISCONNECTED) {
349349
}
350350

351351
Logger::log("Sending packet failed!\n");
@@ -357,7 +357,7 @@ void SocketClient::recvFunc() {
357357

358358
Logger::log("Starting Recv Thread.\n");
359359

360-
while (recv() || socket_log_state != SOCKET_LOG_DISCONNECTED) {
360+
while (recv() || socket_log_state != SockState::DISCONNECTED) {
361361
}
362362

363363
// Free up all blocked threads
@@ -369,7 +369,7 @@ void SocketClient::recvFunc() {
369369
}
370370

371371
bool SocketClient::queuePacket(Packet* packet) {
372-
if (socket_log_state == SOCKET_LOG_CONNECTED && !(mSendQueue.mMessageQueueInner._count == mSendQueue.mMessageQueueInner._maxCount)) {
372+
if (socket_log_state == SockState::CONNECTED && !(mSendQueue.mMessageQueueInner._count == mSendQueue.mMessageQueueInner._maxCount)) {
373373
// as this is non-blocking, it will always return true.
374374
mSendQueue.push((s64)packet, sead::MessageQueue::BlockType::NonBlocking);
375375
return true;
@@ -390,5 +390,5 @@ bool SocketClient::trySendQueue() {
390390
}
391391

392392
Packet* SocketClient::tryGetPacket() {
393-
return socket_log_state == SOCKET_LOG_CONNECTED ? (Packet*)mRecvQueue.pop(sead::MessageQueue::BlockType::Blocking) : nullptr;
393+
return socket_log_state == SockState::CONNECTED ? (Packet*)mRecvQueue.pop(sead::MessageQueue::BlockType::Blocking) : nullptr;
394394
}

src/server/logger.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ nn::Result Logger::init(const char* ip, u16 port) {
3030
in_addr hostAddress = {0};
3131
sockaddr_in serverAddress = {0};
3232

33-
if (this->socket_log_state != SOCKET_LOG_UNINITIALIZED)
33+
if (this->socket_log_state != SockState::UNINITIALIZED)
3434
return nn::Result(-1);
3535

3636
nn::nifm::Initialize();
@@ -43,14 +43,14 @@ nn::Result Logger::init(const char* ip, u16 port) {
4343
#ifndef EMU
4444

4545
if (!nn::nifm::IsNetworkAvailable()) {
46-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
46+
this->socket_log_state = SockState::UNAVAILABLE;
4747
return nn::Result(-1);
4848
}
4949

5050
#endif
5151

5252
if ((this->socket_log_socket = nn::socket::Socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) {
53-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
53+
this->socket_log_state = SockState::UNAVAILABLE;
5454
return nn::Result(nn::socket::GetLastErrno());
5555
}
5656

@@ -73,11 +73,11 @@ nn::Result Logger::init(const char* ip, u16 port) {
7373
}
7474

7575
if (connected) {
76-
this->socket_log_state = SOCKET_LOG_CONNECTED;
76+
this->socket_log_state = SockState::CONNECTED;
7777
this->isDisableName = false;
7878
return nn::Result(0);
7979
} else {
80-
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
80+
this->socket_log_state = SockState::UNAVAILABLE;
8181
return result;
8282
}
8383
}
@@ -96,7 +96,7 @@ s32 Logger::read(char* out) {
9696
}
9797

9898
void Logger::log(const char* fmt, ...) {
99-
if (!sInstance || sInstance->socket_log_state != SOCKET_LOG_CONNECTED)
99+
if (!sInstance || sInstance->socket_log_state != SockState::CONNECTED)
100100
return;
101101
va_list args;
102102
va_start(args, fmt);

syms/main_sdk.sym

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ _ZN2nn4util9VSNPrintfEPcmPKcSt9__va_list
2222
_ZN2nn3err19ApplicationErrorArgC1EjPKcS3_RKNS_8settings12LanguageCodeE
2323
_ZN2nn3err19ApplicationErrorArgC2EjPKcS3_RKNS_8settings12LanguageCodeE
2424
_ZN2nn8settings12LanguageCode4MakeENS0_8LanguageE
25-
_ZN2nn3err20ShowApplicationErrorERKNS0_19ApplicationErrorArgE
25+
_ZN2nn3err20ShowApplicationErrorERKNS0_19ApplicationErrorArgE
26+
_ZN2nn3err19ApplicationErrorArgC1Ev
27+
_ZN2nn3err19ApplicationErrorArgC2Ev

0 commit comments

Comments
 (0)