| 167 | } |
| 168 | |
| 169 | bool RemoteClient::connect(int port) |
| 170 | { |
| 171 | assert(!active); |
| 172 | |
| 173 | if (port <= 0) |
| 174 | port = GetDefaultPort(); |
| 175 | |
| 176 | if (!socket->Initialize()) |
| 177 | { |
| 178 | default_output().printerr("Socket init failed.\n"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if (!socket->Open("localhost", port)) |
| 183 | { |
| 184 | default_output().printerr("Could not connect to localhost:{}\n", port); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | active = true; |
| 189 | |
| 190 | RPCHandshakeHeader header; |
| 191 | memcpy(header.magic, RPCHandshakeHeader::REQUEST_MAGIC, sizeof(header.magic)); |
| 192 | header.version = 1; |
| 193 | |
| 194 | if (socket->Send((uint8*)&header, sizeof(header)) != sizeof(header)) |
| 195 | { |
| 196 | default_output().printerr("Could not send handshake header.\n"); |
| 197 | socket->Close(); |
| 198 | return active = false; |
| 199 | } |
| 200 | |
| 201 | if (!readFullBuffer(socket, &header, sizeof(header))) |
| 202 | { |
| 203 | default_output().printerr("Could not read handshake header.\n"); |
| 204 | socket->Close(); |
| 205 | return active = false; |
| 206 | } |
| 207 | |
| 208 | if (memcmp(header.magic, RPCHandshakeHeader::RESPONSE_MAGIC, sizeof(header.magic)) || |
| 209 | header.version != 1) |
| 210 | { |
| 211 | default_output().printerr("Invalid handshake response.\n"); |
| 212 | socket->Close(); |
| 213 | return active = false; |
| 214 | } |
| 215 | |
| 216 | bind_call.name = "BindMethod"; |
| 217 | bind_call.p_client = this; |
| 218 | bind_call.id = 0; |
| 219 | |
| 220 | runcmd_call.name = "RunCommand"; |
| 221 | runcmd_call.p_client = this; |
| 222 | runcmd_call.id = 1; |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |