Skip to content

Commit cd61d44

Browse files
committed
forgot to check that one lol
1 parent 0ee17cf commit cd61d44

File tree

1 file changed

+48
-68
lines changed

1 file changed

+48
-68
lines changed

sysmodule/source/TcpCommand.cpp

+48-68
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,62 @@ const int CLT_MAGIC = 0x33221100;
55
const int CONTROL_NACP_SIZE = 0x4000;
66
const int CONTROL_FULL_SIZE = 0x24000;
77

8-
const int SERVER_VERSION = 1 << 16 | 0 << 8 | 2;
8+
const int SERVER_VERSION = 1 << 16 | 0 << 8 | 3;
99

1010

11-
//returns cmd id
11+
void SendRaw(int socket, void* buff, size_t size)
12+
{
13+
size_t total = 0;
14+
while (total < size)
15+
{
16+
size_t count = send(socket, (char*)buff + total, size - total, 0);
17+
if (count <= 0)
18+
fatalSimple(MAKERESULT(Module_Discord, Error_SendData));
19+
total += count;
20+
}
21+
}
22+
void ReceiveRaw(int socket, void* buff, size_t size)
23+
{
24+
size_t total = 0;
25+
while (total < size)
26+
{
27+
size_t count = recv(socket, (char*)buff + total, size - total, 0);
28+
if (count <= 0)
29+
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
30+
total += count;
31+
}
32+
}
33+
1234
ClientCommand ReceiveCommand(int socket)
1335
{
1436
int ret;
15-
char* buff = new char[256];
16-
17-
int len = recv(socket , buff, 256, 0);
18-
19-
if(len < 0)
20-
{
21-
return ClientCommand::Disconnect;
22-
}
23-
24-
ret = *((int*)buff);
25-
37+
ReceiveRaw(socket, &ret, 4);
2638
if ((ret & 0xFFFFFF00) != CLT_MAGIC)
2739
{
2840
return ClientCommand::Disconnect;
2941
}
30-
31-
delete[] buff;
32-
3342
return (ClientCommand)(ret & 0xFF);
3443
}
3544

36-
void SendBuffer(int socket, ServerCommand cmd, void* data, size_t size)
45+
void SendBuffer(int socket, void* data, size_t size)
3746
{
47+
int header = SRV_MAGIC | (u8)ServerCommand::Normal;
3848

39-
u8* buff = new u8[size + 4];
40-
41-
*((int*)buff) = SRV_MAGIC | (u8)cmd;
42-
43-
if (data != nullptr)
44-
memcpy(buff + 4, data, size);
45-
46-
47-
size_t total = 0;
48-
while (total < size + 4) {
49-
size_t count = send(socket, buff + total, (size + 4) - total, 0);
50-
if (count <= 0)
51-
fatalSimple(MAKERESULT(Module_Discord, Error_SendData));
52-
total += count;
53-
}
54-
55-
delete[] buff;
49+
SendRaw(socket, &header, 4);
50+
if (data != NULL)
51+
SendRaw(socket, data, size);
5652
}
5753

5854
void SendConfirm(int socket)
5955
{
60-
SendBuffer(socket, ServerCommand::Confirm, nullptr, 0);
56+
int header = SRV_MAGIC | (u8)ServerCommand::Confirm;
57+
SendRaw(socket, &header, 4);
6158
}
6259

6360
void ReceiveBuffer(int socket, void* out_buff, size_t size)
6461
{
65-
int ret = 0;
66-
u8* buff = new u8[size+4];
67-
68-
size_t total = 0;
69-
70-
while (total < size+4)
71-
{
72-
size_t count = recv(socket, buff + total, (size + 4) - total, 0);
73-
if (count <= 0)
74-
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
75-
total += count;
76-
}
77-
78-
ret = *((int*)buff);
62+
int ret;
63+
ReceiveRaw(socket, &ret, 4);
7964

8065
if ((ret & 0xFFFFFF00) != CLT_MAGIC)
8166
{
@@ -87,20 +72,13 @@ void ReceiveBuffer(int socket, void* out_buff, size_t size)
8772
fatalSimple(MAKERESULT(Module_Discord, Error_CmdIdNotSendBuff));
8873
}
8974

90-
memcpy(out_buff, buff + 4, size);
91-
92-
delete[] buff;
75+
ReceiveRaw(socket, out_buff, size);
9376
}
9477

9578
void ReceiveConfirm(int socket)
9679
{
97-
int ret = 0;
98-
int len = recv(socket , &ret, 4, 0);
99-
100-
if(len < 0)
101-
{
102-
fatalSimple(MAKERESULT(Module_Discord, Error_RecData));
103-
}
80+
int ret;
81+
ReceiveRaw(socket, &ret, 4);
10482

10583
if ((ret & 0xFFFFFF00) != CLT_MAGIC)
10684
{
@@ -113,6 +91,9 @@ void ReceiveConfirm(int socket)
11391
}
11492
}
11593

94+
95+
96+
11697
void SendAppList(int socket)
11798
{
11899
Result rc;
@@ -125,9 +106,9 @@ void SendAppList(int socket)
125106
fatalSimple(MAKERESULT(Module_Discord, Error_ListAppFailed));
126107
}
127108

128-
SendBuffer(socket, ServerCommand::Normal, &count, 4);
109+
SendBuffer(socket, &count, 4);
129110
ReceiveConfirm(socket);
130-
SendBuffer(socket, ServerCommand::Normal, list, sizeof(NsApplicationRecord) * count);
111+
SendBuffer(socket, list, sizeof(NsApplicationRecord) * count);
131112

132113
delete[] list;
133114
}
@@ -171,7 +152,6 @@ void SendCurrentApp(int socket)
171152
//applications processes always have PIDs > 0x80
172153
//but atmosphere's pm don't recalculates the pids when a process is removed from the boot list
173154
//so I put 70 to be safe (it now only might bne a problem is more than 10 processes are not booted or were killed)
174-
//this can be removed but i'll be slower
175155
if (pids[i] >= 0x70)
176156
{
177157
//try debugging each application process
@@ -209,15 +189,15 @@ void SendCurrentApp(int socket)
209189
}
210190

211191
exit_send_current:
212-
SendBuffer(socket, ServerCommand::Normal, &tid, 8);
192+
SendBuffer(socket, &tid, 8);
213193

214194
delete[] pids;
215195
}
216196

217197
void SendVersion(int socket)
218198
{
219199
int ver = SERVER_VERSION;
220-
SendBuffer(socket, ServerCommand::Normal, &ver, 4);
200+
SendBuffer(socket, &ver, 4);
221201
}
222202

223203
void SendActiveUser(int socket)
@@ -232,7 +212,7 @@ void SendActiveUser(int socket)
232212
if(R_FAILED(rc))
233213
fatalSimple(MAKERESULT(Module_Discord, Error_GetAciveUser));
234214

235-
SendBuffer(socket, ServerCommand::Normal, &account_selected, 1);
215+
SendBuffer(socket, &account_selected, 1);
236216

237217
if(account_selected)
238218
{
@@ -248,7 +228,7 @@ void SendActiveUser(int socket)
248228

249229
accountProfileClose(&profile);
250230

251-
SendBuffer(socket, ServerCommand::Normal, profilebase.username, 0x20);
231+
SendBuffer(socket, profilebase.username, 0x20);
252232
}
253233
}
254234

@@ -273,7 +253,7 @@ void SendControlData(int socket)
273253
fatalSimple(MAKERESULT(Module_Discord, Error_InvalidControlSize));
274254
}
275255

276-
SendBuffer(socket, ServerCommand::Normal, control, sizeof(NsApplicationControlData));
256+
SendBuffer(socket, control, sizeof(NsApplicationControlData));
277257

278258
delete control;
279259
}
@@ -334,4 +314,4 @@ void StartReceiving(int client)
334314
accountExit();
335315
pmdmntExit();
336316
pminfoExit();
337-
}
317+
}

0 commit comments

Comments
 (0)