blob: 8abb458a039275fc38f1e8a3ba5a4935579af436 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
Austin Schuh812d0d12021-11-04 20:16:48 -07005#include <cstdio>
6
7#include "fmt/format.h"
Brian Silverman8fce7482020-01-05 13:18:21 -08008#include "wpi/MathExtras.h"
9#include "wpi/SmallVector.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070010#include "wpi/StringExtras.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include "wpi/raw_uv_ostream.h"
12#include "wpi/timestamp.h"
13#include "wpi/uv/Loop.h"
14#include "wpi/uv/Tcp.h"
15#include "wpi/uv/Tty.h"
16#include "wpi/uv/Udp.h"
17#include "wpi/uv/util.h"
18
19namespace uv = wpi::uv;
20
21static uint64_t startTime = wpi::Now();
22
23static bool NewlineBuffer(std::string& rem, uv::Buffer& buf, size_t len,
24 wpi::SmallVectorImpl<uv::Buffer>& bufs, bool tcp,
25 uint16_t tcpSeq) {
26 // scan for last newline
Austin Schuh812d0d12021-11-04 20:16:48 -070027 std::string_view str(buf.base, len);
Brian Silverman8fce7482020-01-05 13:18:21 -080028 size_t idx = str.rfind('\n');
Austin Schuh812d0d12021-11-04 20:16:48 -070029 if (idx == std::string_view::npos) {
Brian Silverman8fce7482020-01-05 13:18:21 -080030 // no newline yet, just keep appending to remainder
31 rem += str;
32 return false;
33 }
34
35 // build output
36 wpi::raw_uv_ostream out(bufs, 4096);
Austin Schuh812d0d12021-11-04 20:16:48 -070037 std::string_view toCopy = wpi::slice(str, 0, idx + 1);
Brian Silverman8fce7482020-01-05 13:18:21 -080038 if (tcp) {
39 // Header is 2 byte len, 1 byte type, 4 byte timestamp, 2 byte sequence num
40 uint32_t ts = wpi::FloatToBits((wpi::Now() - startTime) * 1.0e-6);
41 uint16_t len = rem.size() + toCopy.size() + 1 + 4 + 2;
42 const uint8_t header[] = {static_cast<uint8_t>((len >> 8) & 0xff),
43 static_cast<uint8_t>(len & 0xff),
44 12,
45 static_cast<uint8_t>((ts >> 24) & 0xff),
46 static_cast<uint8_t>((ts >> 16) & 0xff),
47 static_cast<uint8_t>((ts >> 8) & 0xff),
48 static_cast<uint8_t>(ts & 0xff),
49 static_cast<uint8_t>((tcpSeq >> 8) & 0xff),
50 static_cast<uint8_t>(tcpSeq & 0xff)};
Austin Schuh812d0d12021-11-04 20:16:48 -070051 out << wpi::span<const uint8_t>(header);
Brian Silverman8fce7482020-01-05 13:18:21 -080052 }
53 out << rem << toCopy;
54
55 // reset remainder
Austin Schuh812d0d12021-11-04 20:16:48 -070056 rem = wpi::slice(str, idx + 1, std::string_view::npos);
Brian Silverman8fce7482020-01-05 13:18:21 -080057 return true;
58}
59
Austin Schuh1e69f942020-11-14 15:06:14 -080060static void CopyUdp(uv::Stream& in, std::shared_ptr<uv::Udp> out, int port,
Brian Silverman8fce7482020-01-05 13:18:21 -080061 bool broadcast) {
62 sockaddr_in addr;
63 if (broadcast) {
64 out->SetBroadcast(true);
Austin Schuh1e69f942020-11-14 15:06:14 -080065 uv::NameToAddr("0.0.0.0", port, &addr);
Brian Silverman8fce7482020-01-05 13:18:21 -080066 } else {
Austin Schuh1e69f942020-11-14 15:06:14 -080067 uv::NameToAddr("127.0.0.1", port, &addr);
Brian Silverman8fce7482020-01-05 13:18:21 -080068 }
69
70 in.data.connect(
71 [rem = std::make_shared<std::string>(), outPtr = out.get(), addr](
72 uv::Buffer& buf, size_t len) {
73 // build buffers
74 wpi::SmallVector<uv::Buffer, 4> bufs;
Austin Schuh812d0d12021-11-04 20:16:48 -070075 if (!NewlineBuffer(*rem, buf, len, bufs, false, 0)) {
76 return;
77 }
Brian Silverman8fce7482020-01-05 13:18:21 -080078
79 // send output
80 outPtr->Send(addr, bufs, [](auto bufs2, uv::Error) {
Austin Schuh812d0d12021-11-04 20:16:48 -070081 for (auto buf : bufs2) {
82 buf.Deallocate();
83 }
Brian Silverman8fce7482020-01-05 13:18:21 -080084 });
85 },
86 out);
87}
88
89static void CopyTcp(uv::Stream& in, std::shared_ptr<uv::Stream> out) {
90 struct StreamData {
91 std::string rem;
92 uint16_t seq = 0;
93 };
94 in.data.connect(
95 [data = std::make_shared<StreamData>(), outPtr = out.get()](
96 uv::Buffer& buf, size_t len) {
97 // build buffers
98 wpi::SmallVector<uv::Buffer, 4> bufs;
Austin Schuh812d0d12021-11-04 20:16:48 -070099 if (!NewlineBuffer(data->rem, buf, len, bufs, true, data->seq++)) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800100 return;
Austin Schuh812d0d12021-11-04 20:16:48 -0700101 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800102
103 // send output
104 outPtr->Write(bufs, [](auto bufs2, uv::Error) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 for (auto buf : bufs2) {
106 buf.Deallocate();
107 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800108 });
109 },
110 out);
111}
112
113static void CopyStream(uv::Stream& in, std::shared_ptr<uv::Stream> out) {
114 in.data.connect([out](uv::Buffer& buf, size_t len) {
115 uv::Buffer buf2 = buf.Dup();
116 buf2.len = len;
Austin Schuh812d0d12021-11-04 20:16:48 -0700117 out->Write({buf2}, [](auto bufs, uv::Error) {
118 for (auto buf : bufs) {
119 buf.Deallocate();
120 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800121 });
122 });
123}
124
125int main(int argc, char* argv[]) {
126 // parse arguments
127 int arg = 1;
128 bool useUdp = false;
129 bool broadcastUdp = false;
130 bool err = false;
Austin Schuh1e69f942020-11-14 15:06:14 -0800131 int port = -1;
Brian Silverman8fce7482020-01-05 13:18:21 -0800132
133 while (arg < argc && argv[arg][0] == '-') {
Austin Schuh812d0d12021-11-04 20:16:48 -0700134 if (std::string_view(argv[arg]) == "-u") {
Brian Silverman8fce7482020-01-05 13:18:21 -0800135 useUdp = true;
Austin Schuh812d0d12021-11-04 20:16:48 -0700136 } else if (std::string_view(argv[arg]) == "-b") {
Brian Silverman8fce7482020-01-05 13:18:21 -0800137 useUdp = true;
138 broadcastUdp = true;
Austin Schuh812d0d12021-11-04 20:16:48 -0700139 } else if (std::string_view(argv[arg]) == "-p") {
Austin Schuh1e69f942020-11-14 15:06:14 -0800140 ++arg;
Austin Schuh812d0d12021-11-04 20:16:48 -0700141 std::optional<int> portValue;
Austin Schuh1e69f942020-11-14 15:06:14 -0800142 if (arg >= argc || argv[arg][0] == '-' ||
Austin Schuh812d0d12021-11-04 20:16:48 -0700143 !(portValue = wpi::parse_integer<int>(argv[arg], 10))) {
144 std::fputs("-p must be followed by port number\n", stderr);
Austin Schuh1e69f942020-11-14 15:06:14 -0800145 err = true;
Austin Schuh812d0d12021-11-04 20:16:48 -0700146 } else if (portValue) {
147 port = portValue.value();
Austin Schuh1e69f942020-11-14 15:06:14 -0800148 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800149 } else {
Austin Schuh812d0d12021-11-04 20:16:48 -0700150 fmt::print(stderr, "unrecognized command line option {}\n", argv[arg]);
Brian Silverman8fce7482020-01-05 13:18:21 -0800151 err = true;
152 }
153 ++arg;
154 }
155
156 if (err) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700157 std::fputs(argv[0], stderr);
158 std::fputs(
159 " [-ub] [-p PORT]\n"
160 " -u send udp to localhost port 6666 instead of using tcp\n"
161 " -b broadcast udp to port 6666 instead of using tcp\n"
162 " -p PORT use port PORT instead of 6666 (udp) or 1740 (tcp)\n",
163 stderr);
Brian Silverman8fce7482020-01-05 13:18:21 -0800164 return EXIT_FAILURE;
165 }
166
167 auto loop = uv::Loop::Create();
168 loop->error.connect(
Austin Schuh812d0d12021-11-04 20:16:48 -0700169 [](uv::Error err) { fmt::print(stderr, "uv ERROR: {}\n", err.str()); });
Brian Silverman8fce7482020-01-05 13:18:21 -0800170
171 // create ttys
172 auto stdinTty = uv::Tty::Create(loop, 0, true);
173 auto stdoutTty = uv::Tty::Create(loop, 1, false);
174
175 // don't bother continuing if we don't have a stdin
Austin Schuh812d0d12021-11-04 20:16:48 -0700176 if (!stdinTty) {
177 return EXIT_SUCCESS;
178 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800179
180 // pass through our input to output
Austin Schuh812d0d12021-11-04 20:16:48 -0700181 if (stdoutTty) {
182 CopyStream(*stdinTty, stdoutTty);
183 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800184
185 // when our stdin closes, exit
186 stdinTty->end.connect([] { std::exit(EXIT_SUCCESS); });
187
188 if (useUdp) {
189 auto udp = uv::Udp::Create(loop);
190 // tee
Austin Schuh1e69f942020-11-14 15:06:14 -0800191 CopyUdp(*stdinTty, udp, port < 0 ? 6666 : port, broadcastUdp);
Brian Silverman8fce7482020-01-05 13:18:21 -0800192 } else {
193 auto tcp = uv::Tcp::Create(loop);
194
195 // bind to listen address and port
Austin Schuh1e69f942020-11-14 15:06:14 -0800196 tcp->Bind("", port < 0 ? 1740 : port);
Brian Silverman8fce7482020-01-05 13:18:21 -0800197
198 // when we get a connection, accept it
199 tcp->connection.connect([srv = tcp.get(), stdinTty] {
200 auto tcp = srv->Accept();
Austin Schuh812d0d12021-11-04 20:16:48 -0700201 if (!tcp) {
202 return;
203 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800204
205 // close on error
206 tcp->error.connect([s = tcp.get()](wpi::uv::Error err) { s->Close(); });
207
208 // tee
209 CopyTcp(*stdinTty, tcp);
210 });
211
212 // start listening for incoming connections
213 tcp->Listen();
214 }
215
216 // start reading
Austin Schuh812d0d12021-11-04 20:16:48 -0700217 if (stdinTty) {
218 stdinTty->StartRead();
219 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800220
221 // run the loop!
222 loop->Run();
223}