@@ -5,77 +5,62 @@ const int CLT_MAGIC = 0x33221100;
5
5
const int CONTROL_NACP_SIZE = 0x4000 ;
6
6
const int CONTROL_FULL_SIZE = 0x24000 ;
7
7
8
- const int SERVER_VERSION = 1 << 16 | 0 << 8 | 2 ;
8
+ const int SERVER_VERSION = 1 << 16 | 0 << 8 | 3 ;
9
9
10
10
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
+
12
34
ClientCommand ReceiveCommand (int socket)
13
35
{
14
36
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 );
26
38
if ((ret & 0xFFFFFF00 ) != CLT_MAGIC)
27
39
{
28
40
return ClientCommand::Disconnect;
29
41
}
30
-
31
- delete[] buff;
32
-
33
42
return (ClientCommand)(ret & 0xFF );
34
43
}
35
44
36
- void SendBuffer (int socket, ServerCommand cmd, void * data, size_t size)
45
+ void SendBuffer (int socket, void * data, size_t size)
37
46
{
47
+ int header = SRV_MAGIC | (u8)ServerCommand::Normal;
38
48
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);
56
52
}
57
53
58
54
void SendConfirm (int socket)
59
55
{
60
- SendBuffer (socket, ServerCommand::Confirm, nullptr , 0 );
56
+ int header = SRV_MAGIC | (u8)ServerCommand::Confirm;
57
+ SendRaw (socket, &header, 4 );
61
58
}
62
59
63
60
void ReceiveBuffer (int socket, void * out_buff, size_t size)
64
61
{
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 );
79
64
80
65
if ((ret & 0xFFFFFF00 ) != CLT_MAGIC)
81
66
{
@@ -87,20 +72,13 @@ void ReceiveBuffer(int socket, void* out_buff, size_t size)
87
72
fatalSimple (MAKERESULT (Module_Discord, Error_CmdIdNotSendBuff));
88
73
}
89
74
90
- memcpy (out_buff, buff + 4 , size);
91
-
92
- delete[] buff;
75
+ ReceiveRaw (socket, out_buff, size);
93
76
}
94
77
95
78
void ReceiveConfirm (int socket)
96
79
{
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 );
104
82
105
83
if ((ret & 0xFFFFFF00 ) != CLT_MAGIC)
106
84
{
@@ -113,6 +91,9 @@ void ReceiveConfirm(int socket)
113
91
}
114
92
}
115
93
94
+
95
+
96
+
116
97
void SendAppList (int socket)
117
98
{
118
99
Result rc;
@@ -125,9 +106,9 @@ void SendAppList(int socket)
125
106
fatalSimple (MAKERESULT (Module_Discord, Error_ListAppFailed));
126
107
}
127
108
128
- SendBuffer (socket, ServerCommand::Normal, &count, 4 );
109
+ SendBuffer (socket, &count, 4 );
129
110
ReceiveConfirm (socket);
130
- SendBuffer (socket, ServerCommand::Normal, list, sizeof (NsApplicationRecord) * count);
111
+ SendBuffer (socket, list, sizeof (NsApplicationRecord) * count);
131
112
132
113
delete[] list;
133
114
}
@@ -171,7 +152,6 @@ void SendCurrentApp(int socket)
171
152
// applications processes always have PIDs > 0x80
172
153
// but atmosphere's pm don't recalculates the pids when a process is removed from the boot list
173
154
// 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
175
155
if (pids[i] >= 0x70 )
176
156
{
177
157
// try debugging each application process
@@ -209,15 +189,15 @@ void SendCurrentApp(int socket)
209
189
}
210
190
211
191
exit_send_current:
212
- SendBuffer (socket, ServerCommand::Normal, &tid, 8 );
192
+ SendBuffer (socket, &tid, 8 );
213
193
214
194
delete[] pids;
215
195
}
216
196
217
197
void SendVersion (int socket)
218
198
{
219
199
int ver = SERVER_VERSION;
220
- SendBuffer (socket, ServerCommand::Normal, &ver, 4 );
200
+ SendBuffer (socket, &ver, 4 );
221
201
}
222
202
223
203
void SendActiveUser (int socket)
@@ -232,7 +212,7 @@ void SendActiveUser(int socket)
232
212
if (R_FAILED (rc))
233
213
fatalSimple (MAKERESULT (Module_Discord, Error_GetAciveUser));
234
214
235
- SendBuffer (socket, ServerCommand::Normal, &account_selected, 1 );
215
+ SendBuffer (socket, &account_selected, 1 );
236
216
237
217
if (account_selected)
238
218
{
@@ -248,7 +228,7 @@ void SendActiveUser(int socket)
248
228
249
229
accountProfileClose (&profile);
250
230
251
- SendBuffer (socket, ServerCommand::Normal, profilebase.username , 0x20 );
231
+ SendBuffer (socket, profilebase.username , 0x20 );
252
232
}
253
233
}
254
234
@@ -273,7 +253,7 @@ void SendControlData(int socket)
273
253
fatalSimple (MAKERESULT (Module_Discord, Error_InvalidControlSize));
274
254
}
275
255
276
- SendBuffer (socket, ServerCommand::Normal, control, sizeof (NsApplicationControlData));
256
+ SendBuffer (socket, control, sizeof (NsApplicationControlData));
277
257
278
258
delete control;
279
259
}
@@ -334,4 +314,4 @@ void StartReceiving(int client)
334
314
accountExit ();
335
315
pmdmntExit ();
336
316
pminfoExit ();
337
- }
317
+ }
0 commit comments