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