Files
2026-11-25 05:09:31 -05:00

48 lines
1.6 KiB
C++

#ifndef NUI_ATTACH_LINUX_USBIP_SERVER_H
#define NUI_ATTACH_LINUX_USBIP_SERVER_H
#include <atomic>
#include <cstdint>
#include "attach_linux/kinect_export.h"
#include "protocol/protocol.h"
namespace attach::linux_attach {
// Default usbip TCP port (tools/usb/usbip's own default; the client's
// `usbip attach -r <host>` assumes it unless told otherwise).
constexpr uint16_t kDefaultPort = 3240;
// A userspace USB/IP server: answers OP_REQ_DEVLIST/OP_REQ_IMPORT over a
// listening TCP socket, then on the same connection relays
// USBIP_CMD_SUBMIT/USBIP_CMD_UNLINK traffic from the usbip kernel client
// (vhci-hcd) into `backend`, one connection at a time (usbip's own kernel
// module and the real `usbipd` both serve one importer per exported device
// at a time; this mirrors that).
class UsbipServer {
public:
UsbipServer(nui::protocol::UsbDeviceBackend& backend, ExportSet devices);
// Binds `port` and serves forever (blocks) until `stop()` is called from
// another thread or signal handler. Returns a process-exit-style code:
// 0 on a clean stop, nonzero if the listening socket could not be set up.
int run(uint16_t port);
void stop();
private:
int accept_loop(int listen_fd);
void serve_connection(int client_fd);
bool handle_control_phase(int client_fd, nui::protocol::UsbFunction& function, std::string& busid);
void session_loop(int client_fd, nui::protocol::UsbFunction function);
nui::protocol::UsbDeviceBackend& backend_;
ExportSet devices_;
std::atomic<bool> stop_requested_{false};
int listen_fd_ = -1;
};
} // namespace attach::linux_attach
#endif // NUI_ATTACH_LINUX_USBIP_SERVER_H