-
Notifications
You must be signed in to change notification settings - Fork 5
/
commandconnection.cpp
210 lines (170 loc) · 4.57 KB
/
commandconnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "commandconnection.h"
#include "log.h"
#include <vdb.h>
#include <psp2/net/net.h>
#include <psp2/kernel/error.h>
#include <psp2/appmgr.h>
#include <psp2/power.h>
#include <stdio.h>
#include <string.h>
namespace
{
using Handler = void (*)(const char *argument, char *output, size_t outSize);
void cmd_destroy(const char *argument, char *output, size_t outSize)
{
sceAppMgrDestroyOtherApp();
}
void cmd_launch(const char *argument, char *output, size_t outSize)
{
LOG("about to launch app\n");
auto res = vdb_launch_debug(argument);
if (res < 0)
{
snprintf(output, outSize, "Error 0x%08X launching \"%s\"\n", res, argument);
}
}
void cmd_reboot(const char *argument, char *output, size_t outSize)
{
scePowerRequestColdReset();
}
Handler lookupCommandHandler(const char *name)
{
constexpr struct {
const char *name;
Handler executor;
} handlers[] =
{
{ "destroy", cmd_destroy },
{ "launch", cmd_launch },
{ "reboot", cmd_reboot },
};
constexpr size_t handlerCount = sizeof(handlers)/sizeof(handlers[0]);
for (auto i = 0u; i < handlerCount; ++i)
{
if (strcmp(handlers[i].name, name) == 0)
{
return handlers[i].executor;
}
}
return nullptr;
}
}
CommandConnection::CommandConnection(int socket)
: m_socket(socket)
, m_valid(true)
, m_commandThread(this)
{
LOG("we got this far COMMADN\n");
auto nonblocking = 0;
auto res = sceNetSetsockopt(socket, SCE_NET_SOL_SOCKET, SCE_NET_SO_NBIO, &nonblocking, sizeof(nonblocking));
if (res < 0)
{
LOG("error setting socket to blocking I/O (0x%08X)\n", res);
return;
}
auto set = 1;
res = sceNetSetsockopt(socket, SCE_NET_SOL_SOCKET, SCE_NET_TCP_NODELAY, &set, sizeof(set));
if (res < 0)
{
LOG("error setting TCP no-delay on socket (0x%08X)\n", res);
}
m_commandThread.start();
}
CommandConnection::~CommandConnection()
{
if (!m_valid)
{
return;
}
close();
}
bool CommandConnection::valid() const
{
return m_valid;
}
int CommandConnection::recv(char *data, unsigned int length) const
{
return sceNetRecv(m_socket, data, length, 0);
}
int CommandConnection::send(const char *data, unsigned int length) const
{
return sceNetSend(m_socket, data, length, 0);
}
int CommandConnection::close()
{
auto res = sceNetSocketClose(m_socket);
m_socket = -1;
m_valid = false;
return res;
}
CommandConnection::CommandThread::CommandThread(CommandConnection *connection)
: m_connection(connection)
{
setStackSize(8*1024);
}
void CommandConnection::CommandThread::run()
{
LOG("we got this far\n");
constexpr auto maxBufferSize = 0x800u;
char data[maxBufferSize+1];
if (readCommand(data, maxBufferSize))
{
LOG("Got command \"%s\"\n", data);
char output[maxBufferSize+1];
*output = '\0';
auto space = strchr(data, ' ');
auto command = data;
const char *argument = "";
if (space)
{
*space = '\0';
argument = space+1;
}
auto handler = lookupCommandHandler(command);
if (handler)
{
LOG("Found command handler \"%s\"\n", command);
handler(argument, output, maxBufferSize);
}
else
{
snprintf(output, maxBufferSize, "Error unrecognised command \"%s\"\n", command);
}
LOG("Command output \"%s\"\n", output);
m_connection->send(output, strlen(output));
}
else
{
LOG("Error reading command\n");
}
m_connection->close();
}
bool CommandConnection::CommandThread::readCommand(char *output, size_t size) const
{
size_t endPosition = 0u;
while (m_connection->valid() && endPosition < size)
{
auto res = m_connection->recv(output+endPosition, size-endPosition);
LOG("Read 0x%08X from connection\n", res);
if (res < 0)
{
LOG("error receiving data from TCP: 0x%08X\n", res);
break;
}
else if (res == 0)
{
LOG("NO MORE DATA\n");
break;
}
for (auto i = 0; i < res; ++i)
{
if (output[i+endPosition] == '\n')
{
output[i+endPosition] = '\0';
return true;
}
}
endPosition += res;
}
return false;
}