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