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. |
| 4 | |
| 5 | #include <cstdio> |
| 6 | |
| 7 | #include "wpi/EventLoopRunner.h" |
| 8 | #include "wpi/Logger.h" |
| 9 | #include "wpi/ParallelTcpConnector.h" |
| 10 | #include "wpi/uv/Error.h" |
| 11 | #include "wpi/uv/Tcp.h" |
| 12 | |
| 13 | namespace uv = wpi::uv; |
| 14 | |
| 15 | static void logfunc(unsigned int level, const char* file, unsigned int line, |
| 16 | const char* msg) { |
| 17 | std::fprintf(stderr, "(%d) %s:%d: %s\n", level, file, line, msg); |
| 18 | } |
| 19 | |
| 20 | int main() { |
| 21 | wpi::Logger logger{logfunc, 0}; |
| 22 | |
| 23 | // Kick off the event loop on a separate thread |
| 24 | wpi::EventLoopRunner loop; |
| 25 | std::shared_ptr<wpi::ParallelTcpConnector> connect; |
| 26 | loop.ExecAsync([&](uv::Loop& loop) { |
| 27 | connect = wpi::ParallelTcpConnector::Create( |
| 28 | loop, uv::Timer::Time{2000}, logger, [&](uv::Tcp& tcp) { |
| 29 | std::fputs("Got connection, accepting!\n", stdout); |
| 30 | tcp.StartRead(); |
| 31 | connect->Succeeded(tcp); |
| 32 | tcp.end.connect([&] { |
| 33 | std::fputs("TCP connection ended, disconnecting!\n", stdout); |
| 34 | tcp.Close(); |
| 35 | connect->Disconnected(); |
| 36 | }); |
| 37 | tcp.error.connect([&](uv::Error) { |
| 38 | std::fputs("TCP error, disconnecting!\n", stdout); |
| 39 | connect->Disconnected(); |
| 40 | }); |
| 41 | }); |
| 42 | connect->SetServers({{{"roborio-294-frc.local", 8080}, |
| 43 | {"roborio-294-frc.frc-field.local", 8080}, |
| 44 | {"10.2.94.2", 8080}, |
| 45 | {"127.0.0.1", 8080}}}); |
| 46 | }); |
| 47 | |
| 48 | // wait for a keypress to terminate |
| 49 | std::getchar(); |
| 50 | } |