Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "wpi/MathExtras.h" |
| 9 | #include "wpi/SmallVector.h" |
| 10 | #include "wpi/raw_ostream.h" |
| 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 |
| 27 | wpi::StringRef str(buf.base, len); |
| 28 | size_t idx = str.rfind('\n'); |
| 29 | if (idx == wpi::StringRef::npos) { |
| 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); |
| 37 | wpi::StringRef toCopy = str.slice(0, idx + 1); |
| 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; |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 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)}; |
| 51 | out << wpi::ArrayRef<uint8_t>(header); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 52 | } |
| 53 | out << rem << toCopy; |
| 54 | |
| 55 | // reset remainder |
| 56 | rem = str.slice(idx + 1, wpi::StringRef::npos); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | static void CopyUdp(uv::Stream& in, std::shared_ptr<uv::Udp> out, |
| 61 | bool broadcast) { |
| 62 | sockaddr_in addr; |
| 63 | if (broadcast) { |
| 64 | out->SetBroadcast(true); |
| 65 | uv::NameToAddr("0.0.0.0", 6666, &addr); |
| 66 | } else { |
| 67 | uv::NameToAddr("127.0.0.1", 6666, &addr); |
| 68 | } |
| 69 | |
| 70 | in.data.connect( |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 71 | [rem = std::make_shared<std::string>(), outPtr = out.get(), addr]( |
| 72 | uv::Buffer& buf, size_t len) { |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 73 | // build buffers |
| 74 | wpi::SmallVector<uv::Buffer, 4> bufs; |
| 75 | if (!NewlineBuffer(*rem, buf, len, bufs, false, 0)) return; |
| 76 | |
| 77 | // send output |
| 78 | outPtr->Send(addr, bufs, [](auto bufs2, uv::Error) { |
| 79 | for (auto buf : bufs2) buf.Deallocate(); |
| 80 | }); |
| 81 | }, |
| 82 | out); |
| 83 | } |
| 84 | |
| 85 | static void CopyTcp(uv::Stream& in, std::shared_ptr<uv::Stream> out) { |
| 86 | struct StreamData { |
| 87 | std::string rem; |
| 88 | uint16_t seq = 0; |
| 89 | }; |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 90 | in.data.connect( |
| 91 | [data = std::make_shared<StreamData>(), outPtr = out.get()]( |
| 92 | uv::Buffer& buf, size_t len) { |
| 93 | // build buffers |
| 94 | wpi::SmallVector<uv::Buffer, 4> bufs; |
| 95 | if (!NewlineBuffer(data->rem, buf, len, bufs, true, data->seq++)) |
| 96 | return; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 97 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 98 | // send output |
| 99 | outPtr->Write(bufs, [](auto bufs2, uv::Error) { |
| 100 | for (auto buf : bufs2) buf.Deallocate(); |
| 101 | }); |
| 102 | }, |
| 103 | out); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void CopyStream(uv::Stream& in, std::shared_ptr<uv::Stream> out) { |
| 107 | in.data.connect([out](uv::Buffer& buf, size_t len) { |
| 108 | uv::Buffer buf2 = buf.Dup(); |
| 109 | buf2.len = len; |
| 110 | out->Write(buf2, [](auto bufs, uv::Error) { |
| 111 | for (auto buf : bufs) buf.Deallocate(); |
| 112 | }); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | int main(int argc, char* argv[]) { |
| 117 | // parse arguments |
| 118 | int arg = 1; |
| 119 | bool useUdp = false; |
| 120 | bool broadcastUdp = false; |
| 121 | bool err = false; |
| 122 | |
| 123 | while (arg < argc && argv[arg][0] == '-') { |
| 124 | if (wpi::StringRef(argv[arg]) == "-u") { |
| 125 | useUdp = true; |
| 126 | } else if (wpi::StringRef(argv[arg]) == "-b") { |
| 127 | useUdp = true; |
| 128 | broadcastUdp = true; |
| 129 | } else { |
| 130 | wpi::errs() << "unrecognized command line option " << argv[arg] << '\n'; |
| 131 | err = true; |
| 132 | } |
| 133 | ++arg; |
| 134 | } |
| 135 | |
| 136 | if (err) { |
| 137 | wpi::errs() |
| 138 | << argv[0] << " [-ub]\n" |
| 139 | << " -u send udp to localhost port 6666 instead of using tcp\n" |
| 140 | << " -b broadcast udp to port 6666 instead of using tcp\n"; |
| 141 | return EXIT_FAILURE; |
| 142 | } |
| 143 | |
| 144 | auto loop = uv::Loop::Create(); |
| 145 | loop->error.connect( |
| 146 | [](uv::Error err) { wpi::errs() << "uv ERROR: " << err.str() << '\n'; }); |
| 147 | |
| 148 | // create ttys |
| 149 | auto stdinTty = uv::Tty::Create(loop, 0, true); |
| 150 | auto stdoutTty = uv::Tty::Create(loop, 1, false); |
| 151 | |
| 152 | // don't bother continuing if we don't have a stdin |
| 153 | if (!stdinTty) return EXIT_SUCCESS; |
| 154 | |
| 155 | // pass through our input to output |
| 156 | if (stdoutTty) CopyStream(*stdinTty, stdoutTty); |
| 157 | |
| 158 | // when our stdin closes, exit |
| 159 | stdinTty->end.connect([] { std::exit(EXIT_SUCCESS); }); |
| 160 | |
| 161 | if (useUdp) { |
| 162 | auto udp = uv::Udp::Create(loop); |
| 163 | // tee |
| 164 | CopyUdp(*stdinTty, udp, broadcastUdp); |
| 165 | } else { |
| 166 | auto tcp = uv::Tcp::Create(loop); |
| 167 | |
| 168 | // bind to listen address and port |
| 169 | tcp->Bind("", 1740); |
| 170 | |
| 171 | // when we get a connection, accept it |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 172 | tcp->connection.connect([srv = tcp.get(), stdinTty] { |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 173 | auto tcp = srv->Accept(); |
| 174 | if (!tcp) return; |
| 175 | |
| 176 | // close on error |
| 177 | tcp->error.connect([s = tcp.get()](wpi::uv::Error err) { s->Close(); }); |
| 178 | |
| 179 | // tee |
| 180 | CopyTcp(*stdinTty, tcp); |
| 181 | }); |
| 182 | |
| 183 | // start listening for incoming connections |
| 184 | tcp->Listen(); |
| 185 | } |
| 186 | |
| 187 | // start reading |
| 188 | if (stdinTty) stdinTty->StartRead(); |
| 189 | |
| 190 | // run the loop! |
| 191 | loop->Run(); |
| 192 | } |