apiclient

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides a high-level interface to the VIIPER API, handling request formatting, response parsing, and error handling.

func New

func New(addr string) *Client

New constructs a high-level API client using the internal low-level Transport. The addr parameter specifies the TCP address (host:port) of the VIIPER API server.

func NewWithConfig

func NewWithConfig(addr string, cfg *Config) *Client

NewWithConfig constructs a client with custom transport timeouts.

func WithTransport

func WithTransport(t *Transport) *Client

WithTransport constructs a Client using a custom Transport implementation. This is primarily useful for testing or when advanced transport configuration is needed.

func (*Client) AddDeviceAndConnect

func (c *Client) AddDeviceAndConnect(ctx context.Context, busID uint32, deviceType string, o *device.CreateOptions) (*DeviceStream, *apitypes.Device, error)

AddDeviceAndConnect creates a device on the specified bus and immediately connects to its stream. This is a convenience wrapper that combines DeviceAdd + OpenStream in one call.

func (*Client) BusCreate

func (c *Client) BusCreate(busID uint32) (*apitypes.BusCreateResponse, error)

BusCreate creates a new virtual USB bus with the specified bus number. Returns the created bus ID or an error if the bus number is already allocated.

func (*Client) BusCreateCtx

func (c *Client) BusCreateCtx(ctx context.Context, busID uint32) (*apitypes.BusCreateResponse, error)

func (*Client) BusList

func (c *Client) BusList() (*apitypes.BusListResponse, error)

BusList retrieves a list of all active virtual USB bus numbers.

func (*Client) BusListCtx

func (c *Client) BusListCtx(ctx context.Context) (*apitypes.BusListResponse, error)

func (*Client) BusRemove

func (c *Client) BusRemove(busID uint32) (*apitypes.BusRemoveResponse, error)

BusRemove removes an existing virtual USB bus and all devices attached to it. Returns the removed bus ID or an error if the bus does not exist.

func (*Client) BusRemoveCtx

func (c *Client) BusRemoveCtx(ctx context.Context, busID uint32) (*apitypes.BusRemoveResponse, error)

func (*Client) DeviceAdd

func (c *Client) DeviceAdd(busID uint32, devType string, o *device.CreateOptions) (*apitypes.Device, error)

DeviceAdd adds a new device of the specified type to the given bus. The devType parameter specifies the device type (e.g., "xbox360"). Returns the assigned bus ID (e.g., "1-1") or an error if the bus does not exist or the device type is unknown.

func (*Client) DeviceAddCtx

func (c *Client) DeviceAddCtx(ctx context.Context, busID uint32, devType string, o *device.CreateOptions) (*apitypes.Device, error)

func (*Client) DeviceRemove

func (c *Client) DeviceRemove(busID uint32, busid string) (*apitypes.DeviceRemoveResponse, error)

DeviceRemove removes a device from the specified bus by its device ID. The busid parameter is the device number (e.g., "1") on the given bus. Active USB-IP connections to the device will be closed. Returns the removed device's bus and device ID or an error if not found.

func (*Client) DeviceRemoveCtx

func (c *Client) DeviceRemoveCtx(ctx context.Context, busID uint32, busid string) (*apitypes.DeviceRemoveResponse, error)

func (*Client) DevicesList

func (c *Client) DevicesList(busID uint32) (*apitypes.DevicesListResponse, error)

DevicesList retrieves a list of all devices attached to the specified bus. Each device entry includes bus ID, device ID, VID, PID, and device type.

func (*Client) DevicesListCtx

func (c *Client) DevicesListCtx(ctx context.Context, busID uint32) (*apitypes.DevicesListResponse, error)

func (*Client) OpenStream

func (c *Client) OpenStream(ctx context.Context, busID uint32, devID string) (*DeviceStream, error)

OpenStream connects to an existing device's stream channel. The device must already exist on the bus (use DeviceAdd first).

func (*Client) Ping added in v0.3.0

func (c *Client) Ping() (*apitypes.PingResponse, error)

Ping returns the version and identity of the VIIPER server.

func (*Client) PingCtx added in v0.3.0

func (c *Client) PingCtx(ctx context.Context) (*apitypes.PingResponse, error)

PingCtx is the context-aware version of Ping.

type Config

type Config struct {
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

Config controls low-level transport behavior such as timeouts.

type DeviceStream

type DeviceStream struct {
	BusID uint32
	DevID string
	// contains filtered or unexported fields
}

DeviceStream represents a bidirectional connection to a device stream.

func (*DeviceStream) Close

func (s *DeviceStream) Close() error

Close closes the stream connection and stops any background reading.

func (*DeviceStream) Read

func (s *DeviceStream) Read(buf []byte) (int, error)

Read receives raw bytes from the device stream (device → client feedback). For event-driven reading, use StartReading() instead to avoid blocking/polling.

func (*DeviceStream) SetReadDeadline

func (s *DeviceStream) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline for the underlying connection.

func (*DeviceStream) SetWriteDeadline

func (s *DeviceStream) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline for the underlying connection.

func (*DeviceStream) StartReading

func (s *DeviceStream) StartReading(ctx context.Context, chSize int, decode func(r *bufio.Reader) (encoding.BinaryUnmarshaler, error)) (<-chan encoding.BinaryUnmarshaler, <-chan error)

StartReading begins asynchronously reading from the device stream in a background goroutine. You provide a decode function that reads exactly one message from the given *bufio.Reader and returns any value that implements encoding.BinaryUnmarshaler (the interface is only used for typing; StartReading does not call UnmarshalBinary itself).

Example (xbox360 rumble, fixed 2 bytes):

rumbleCh, errCh := stream.StartReading(ctx, 10, func(r *bufio.Reader) (encoding.BinaryUnmarshaler, error) {
    var b [2]byte
    if _, err := io.ReadFull(r, b[:]); err != nil { return nil, err }
    msg := new(xbox360.XRumbleState)
    if err := msg.UnmarshalBinary(b[:]); err != nil { return nil, err }
    return msg, nil
})

func (*DeviceStream) Write

func (s *DeviceStream) Write(data []byte) (int, error)

Write sends raw bytes to the device stream (client → device input).

func (*DeviceStream) WriteBinary

func (s *DeviceStream) WriteBinary(v encoding.BinaryMarshaler) error

WriteBinary marshals and sends a BinaryMarshaler to the device stream. This is the preferred way to send device input (e.g., xbox360.InputState, keyboard.InputState).

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is the low-level VIIPER management protocol implementation used by higher-level API clients. Request framing: `<path>[ SP <payload>] \x00` (null terminator). The payload may contain any data including newlines (e.g. pretty JSON, binary) because only \x00 ends the request. Response framing: server writes a single JSON (or empty success) line terminated by `\n` and then closes the connection. We therefore read until EOF (connection close) and trim a single trailing newline if present. Embedded newlines in the response (future multi-line responses) are preserved.

func NewMockTransport

func NewMockTransport(responder func(path string, payload any, pathParams map[string]string) (string, error)) *Transport

NewMockTransport creates a transport that returns canned responses without real networking. The responder function receives the path, payload and path params and returns the raw line.

func NewTransport

func NewTransport(addr string) *Transport

NewTransport creates a new low-level transport.

func NewTransportWithConfig

func NewTransportWithConfig(addr string, cfg *Config) *Transport

NewTransportWithConfig creates a new low-level transport with optional timeouts configuration.

func (*Transport) Do

func (c *Transport) Do(path string, payload any, pathParams map[string]string) (string, error)

Do sends a request and returns the exact single-line response (without trailing newline). Payload handling rules:

[]byte -> sent as-is
string -> UTF-8 bytes
struct/other -> JSON marshaled bytes
nil -> no payload appended

func (*Transport) DoCtx

func (c *Transport) DoCtx(ctx context.Context, path string, payload any, pathParams map[string]string) (string, error)

DoCtx is like Do but honors the provided context and configured timeouts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL