OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This application forwards the port that the rsp debug stub opens on the | |
6 // loopback interface, and forwards it to the public interface, so that a | |
7 // remote debugger can connect to it. | |
8 | |
9 #include <conio.h> | |
10 | |
11 #include "rsp_proxy/proxy_socket.h" | |
12 | |
13 namespace { | |
14 enum ConnectionState {CONNECTING, CONNECTED}; | |
15 const int kBufferSize = 512; | |
16 const int kListenMs = 20; | |
17 const int kReadWaitMs = 20; | |
18 } | |
19 | |
20 void Disconnect(debug::ListeningSocket client_listening_socket, | |
21 debug::Socket client_socket, | |
22 debug::Socket server_socket) { | |
23 client_listening_socket.Close(); | |
24 client_socket.Close(); | |
25 server_socket.Close(); | |
26 } | |
27 | |
28 int main(int argc, char **argv) { | |
29 debug::Socket server_side_socket; | |
30 debug::ListeningSocket client_side_listening_socket; | |
31 debug::Socket client_side_socket; | |
32 ConnectionState state = CONNECTING; | |
33 | |
34 client_side_listening_socket.Setup(4014); | |
35 | |
36 while (true) { | |
37 if (CONNECTING == state) { | |
38 if (!server_side_socket.IsConnected()) { | |
39 server_side_socket.ConnectTo("127.0.0.1", 4014); | |
40 if (server_side_socket.IsConnected()) | |
41 printf("Connected to the debug_stub.\n"); | |
42 } else if (!client_side_socket.IsConnected()) { | |
43 client_side_listening_socket.Accept(&client_side_socket, kListenMs); | |
44 if (client_side_socket.IsConnected()) | |
45 printf("Got connection from host\n"); | |
46 } else { | |
47 state = CONNECTED; | |
48 } | |
49 continue; | |
50 } | |
51 | |
52 if (CONNECTED == state) { | |
53 if (!server_side_socket.IsConnected() || | |
54 !client_side_socket.IsConnected()) { | |
55 Disconnect(client_side_listening_socket, | |
56 client_side_socket, | |
57 server_side_socket); | |
58 state = CONNECTING; | |
59 continue; | |
60 } | |
61 } | |
62 | |
63 char buff[kBufferSize]; | |
64 size_t read_bytes = client_side_socket.Read(buff, | |
65 sizeof(buff) - 1, | |
66 kReadWaitMs); | |
67 if (read_bytes > 0) { | |
68 buff[read_bytes] = 0; | |
69 printf("h>%s\n", buff); | |
70 server_side_socket.WriteAll(buff, read_bytes); | |
71 } | |
72 | |
73 read_bytes = server_side_socket.Read(buff, sizeof(buff) - 1, kReadWaitMs); | |
74 if (read_bytes > 0) { | |
75 buff[read_bytes] = 0; | |
76 printf("t>%s\n", buff); | |
77 client_side_socket.WriteAll(buff, read_bytes); | |
78 } | |
79 | |
80 if (_kbhit()) { | |
81 char c = getchar(); | |
82 if ('q' == c) { | |
83 break; | |
84 } | |
85 } | |
86 } | |
87 | |
88 if (CONNECTED == state) { | |
89 Disconnect(client_side_listening_socket, | |
90 client_side_socket, | |
91 server_side_socket); | |
92 } | |
93 | |
94 return 0; | |
95 } | |
OLD | NEW |