Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 5 | #include <cstdio> |
| 6 | |
| 7 | #include "fmt/format.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | #include "wpi/MathExtras.h" |
| 9 | #include "wpi/SmallVector.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 10 | #include "wpi/StringExtras.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #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 | |
| 19 | namespace uv = wpi::uv; |
| 20 | |
| 21 | static uint64_t startTime = wpi::Now(); |
| 22 | |
| 23 | static 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 27 | std::string_view str(buf.base, len); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | size_t idx = str.rfind('\n'); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 29 | if (idx == std::string_view::npos) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | // 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 37 | std::string_view toCopy = wpi::slice(str, 0, idx + 1); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 51 | out << wpi::span<const uint8_t>(header); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 52 | } |
| 53 | out << rem << toCopy; |
| 54 | |
| 55 | // reset remainder |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 56 | rem = wpi::slice(str, idx + 1, std::string_view::npos); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 60 | static void CopyUdp(uv::Stream& in, std::shared_ptr<uv::Udp> out, int port, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | bool broadcast) { |
| 62 | sockaddr_in addr; |
| 63 | if (broadcast) { |
| 64 | out->SetBroadcast(true); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 65 | uv::NameToAddr("0.0.0.0", port, &addr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | } else { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 67 | uv::NameToAddr("127.0.0.1", port, &addr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 68 | } |
| 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 75 | if (!NewlineBuffer(*rem, buf, len, bufs, false, 0)) { |
| 76 | return; |
| 77 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | |
| 79 | // send output |
| 80 | outPtr->Send(addr, bufs, [](auto bufs2, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 81 | for (auto buf : bufs2) { |
| 82 | buf.Deallocate(); |
| 83 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 84 | }); |
| 85 | }, |
| 86 | out); |
| 87 | } |
| 88 | |
| 89 | static 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 99 | if (!NewlineBuffer(data->rem, buf, len, bufs, true, data->seq++)) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 100 | return; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 101 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | |
| 103 | // send output |
| 104 | outPtr->Write(bufs, [](auto bufs2, uv::Error) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 105 | for (auto buf : bufs2) { |
| 106 | buf.Deallocate(); |
| 107 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | }); |
| 109 | }, |
| 110 | out); |
| 111 | } |
| 112 | |
| 113 | static 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 117 | out->Write({buf2}, [](auto bufs, uv::Error) { |
| 118 | for (auto buf : bufs) { |
| 119 | buf.Deallocate(); |
| 120 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 121 | }); |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | int 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 Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 131 | int port = -1; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | |
| 133 | while (arg < argc && argv[arg][0] == '-') { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 134 | if (std::string_view(argv[arg]) == "-u") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | useUdp = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 136 | } else if (std::string_view(argv[arg]) == "-b") { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 137 | useUdp = true; |
| 138 | broadcastUdp = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 139 | } else if (std::string_view(argv[arg]) == "-p") { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 140 | ++arg; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 141 | std::optional<int> portValue; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 142 | if (arg >= argc || argv[arg][0] == '-' || |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 143 | !(portValue = wpi::parse_integer<int>(argv[arg], 10))) { |
| 144 | std::fputs("-p must be followed by port number\n", stderr); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 145 | err = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 146 | } else if (portValue) { |
| 147 | port = portValue.value(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 148 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 149 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 150 | fmt::print(stderr, "unrecognized command line option {}\n", argv[arg]); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 151 | err = true; |
| 152 | } |
| 153 | ++arg; |
| 154 | } |
| 155 | |
| 156 | if (err) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 157 | 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 164 | return EXIT_FAILURE; |
| 165 | } |
| 166 | |
| 167 | auto loop = uv::Loop::Create(); |
| 168 | loop->error.connect( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 169 | [](uv::Error err) { fmt::print(stderr, "uv ERROR: {}\n", err.str()); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 170 | |
| 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 176 | if (!stdinTty) { |
| 177 | return EXIT_SUCCESS; |
| 178 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 179 | |
| 180 | // pass through our input to output |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 181 | if (stdoutTty) { |
| 182 | CopyStream(*stdinTty, stdoutTty); |
| 183 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 184 | |
| 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 Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 191 | CopyUdp(*stdinTty, udp, port < 0 ? 6666 : port, broadcastUdp); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 192 | } else { |
| 193 | auto tcp = uv::Tcp::Create(loop); |
| 194 | |
| 195 | // bind to listen address and port |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 196 | tcp->Bind("", port < 0 ? 1740 : port); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 197 | |
| 198 | // when we get a connection, accept it |
| 199 | tcp->connection.connect([srv = tcp.get(), stdinTty] { |
| 200 | auto tcp = srv->Accept(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 201 | if (!tcp) { |
| 202 | return; |
| 203 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 204 | |
| 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 217 | if (stdinTty) { |
| 218 | stdinTty->StartRead(); |
| 219 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 220 | |
| 221 | // run the loop! |
| 222 | loop->Run(); |
| 223 | } |