krpcgo

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 11 Imported by: 0

README

krpc-go

krpc-go is a Go client for kRPC, a Kerbal Space Program mod for controlling the game with an external program.

Installation

go get github.com/weimil/krpc-go

Getting Started

This sample program will launch a vessel sitting on the launchpad. Error handling is omitted for brevity.

package main

import (
    "context"

    krpcgo "github.com/weimil/krpc-go"
    "github.com/weimil/krpc-go/krpc"
    "github.com/weimil/krpc-go/spacecenter"
)

func main() {
    // Connect to the kRPC server with all default parameters.
    client := krpcgo.DefaultKRPCClient()
    client.Connect(context.Background())
    defer client.Close()

    sc := spacecenter.New(client)
    vessel, _ := sc.ActiveVessel()
    control, _ := vessel.Control()

    control.SetSAS(true)
    control.SetRCS(false)
    control.SetThrottle(1.0)
    control.ActivateNextStage()
}
Types

This section describes type mappings from the kRPC protocol.

  • Primitives are mapped to Go primitives.
  • Arrays are mapped to slices. Dictionaries and sets are mapped to maps.
  • Tuples are mapped to a special tuple type in the types package. For example, a tuple of strings would map to types.Tuple3[string, string, string].
    • types also contains some convenience types that can be converted to/from the appropriate tuple, such as types.Vector2D, types.Vector3D, and types.Color.
  • Classes and enums are mapped to local structs and constants defined in the appropriate service. For example, a Vessel will be mapped to a *spacecenter.Vessel, and a GameScene will be mapped to a krpc.GameScene.
  • Existing protobuf types can be found in the types package. For example, a Status will be mapped to a *types.Status.
Streams

krpc-go uses Go's built-in channels to handle streams.

Here's an example of using streams to autostage a vessel until a specific stage is reached.

func AutoStageUntil(vessel *spacecenter.Vessel, stopStage int32) {
    go func() {
        control, _ := vessel.Control()
        stage, _ := control.CurrentStage()

        for stage > stopStage {
            resources, _ := vessel.ResourcesInDecoupleStage(stage-1, false)
            amountStream, _ := resources.AmountStream("LiquidFuel")

            // Wait until this stage runs out of liquid fuel.
            for amount := <-amountStream.C; amount > 0.1 {}

            amountStream.Close()
            control.ActivateNextStage()
            stage--
        }
    }()
}
More examples

See tests in integration/ for more usage examples.

Building

TODO

TODO krpc-go docs link kRPC documentation

Documentation

Overview

Package krpcgo provides the client to communicate with a kRPC server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KRPCClient

type KRPCClient struct {
	KRPCClientConfig

	*StreamClient
	// contains filtered or unexported fields
}

KRPCClient is a client for a kRPC server.

func DefaultKRPCClient

func DefaultKRPCClient() *KRPCClient

DefaultKRPCClient creates a new kRPC client with all default parameters. Equivalent to `NewKRPCClient(KRPCClientConfig{})`.

func NewKRPCClient

func NewKRPCClient(cfg KRPCClientConfig) *KRPCClient

NewKRPCClient creates a new client.

func (*KRPCClient) Call

Call performs a remote procedure call.

func (*KRPCClient) CallMultiple

func (c *KRPCClient) CallMultiple(calls []*types.ProcedureCall) ([]*types.ProcedureResult, error)

CallMultiple performs a batch of procedure calls to the rpc server.

func (*KRPCClient) Close

func (c *KRPCClient) Close() error

Close closes the client.

func (*KRPCClient) Connect

func (c *KRPCClient) Connect(ctx context.Context) error

Connect connects to a kRPC server.

func (*KRPCClient) Receive

func (c *KRPCClient) Receive() ([]byte, error)

Receive receives protobuf-encoded data from a kRPC server.

func (*KRPCClient) Send

func (c *KRPCClient) Send(data []byte) error

Send sends protobuf-encoded data to a kRPC server.

type KRPCClientConfig

type KRPCClientConfig struct {
	// Host is the kRPC server host. Defaults to "localhost".
	Host string
	// RPCPort is the kRPC server port. Defaults to "50000".
	RPCPort string
	// StreamPort is the stream server port. Defaults to "50001".
	StreamPort string
	// ClientName is the client name sent to the kRPC server. Defaults to "krpc-go".
	ClientName string
	// RPCOnly will only set up the RPC client (and not the stream client) when enabled.
	// Disabled by default.
	RPCOnly bool
}

KRPCClientConfig is the config for a kRPC client.

func (*KRPCClientConfig) SetDefaults

func (cfg *KRPCClientConfig) SetDefaults()

SetDefaults sets the config defaults.

type Stream

type Stream[T any] struct {
	C  chan T
	ID uint64
	// contains filtered or unexported fields
}

Stream is a struct for receiving stream data.

func MapStream

func MapStream[S, T any](src *Stream[S], m func(S) T) *Stream[T]

MapStream converts a stream to another type.

func (*Stream[T]) AddCloser

func (s *Stream[T]) AddCloser(close func() error)

func (*Stream[T]) Clone

func (s *Stream[T]) Clone() *Stream[T]

Clone clones the stream for another thread to listen on.

func (*Stream[T]) Close

func (s *Stream[T]) Close() error

Close closes the stream.

type StreamClient

type StreamClient struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

StreamClient is a client for kRPC streams.

func NewStreamClient

func NewStreamClient(conn net.Conn) *StreamClient

NewStreamClient creates a new stream client with an existing connection.

func (*StreamClient) Close

func (s *StreamClient) Close() error

Close closes the stream client.

func (*StreamClient) DeleteStream

func (s *StreamClient) DeleteStream(id uint64)

DeleteStream removes a byte stream for a particular stream ID. Note that if the stream hasn't yet been closed on the kRPC server, a new local stream will eventually be recreated.

func (*StreamClient) GetStream

func (s *StreamClient) GetStream(id uint64) *Stream[[]byte]

GetStream gets a byte stream for a particular stream ID.

func (*StreamClient) Receive

func (s *StreamClient) Receive() ([]byte, error)

Receive receives protobuf-encoded data from a stream server.

func (*StreamClient) Run

func (s *StreamClient) Run(ctx context.Context)

Run starts the stream handler.

func (*StreamClient) Send

func (s *StreamClient) Send(data []byte) error

Send sends protobuf-encoded data to a stream server.

func (*StreamClient) WriteToStream

func (s *StreamClient) WriteToStream(id uint64, b []byte)

WriteToStream writes data to a particular stream.

Directories

Path Synopsis
Package dockingcamera provides methods to invoke procedures in the DockingCamera service.
Package dockingcamera provides methods to invoke procedures in the DockingCamera service.
Package drawing provides methods to invoke procedures in the Drawing service.
Package drawing provides methods to invoke procedures in the Drawing service.
Package infernalrobotics provides methods to invoke procedures in the InfernalRobotics service.
Package infernalrobotics provides methods to invoke procedures in the InfernalRobotics service.
Package kerbalalarmclock provides methods to invoke procedures in the KerbalAlarmClock service.
Package kerbalalarmclock provides methods to invoke procedures in the KerbalAlarmClock service.
Package krpc provides methods to invoke procedures in the KRPC service.
Package krpc provides methods to invoke procedures in the KRPC service.
lib
encode
Package encode provides methods to convert values to and from kRPC's protobuf format.
Package encode provides methods to convert values to and from kRPC's protobuf format.
gen
Package gen provides methods to generate kRPC services.
Package gen provides methods to generate kRPC services.
service
Package service provides some definitions needed to generate services.
Package service provides some definitions needed to generate services.
utils
Package utils provides utilities for krpc-go.
Package utils provides utilities for krpc-go.
Package lidar provides methods to invoke procedures in the LiDAR service.
Package lidar provides methods to invoke procedures in the LiDAR service.
Package remotetech provides methods to invoke procedures in the RemoteTech service.
Package remotetech provides methods to invoke procedures in the RemoteTech service.
Package spacecenter provides methods to invoke procedures in the SpaceCenter service.
Package spacecenter provides methods to invoke procedures in the SpaceCenter service.
Package types provides various types for krpc-go.
Package types provides various types for krpc-go.
Package ui provides methods to invoke procedures in the UI service.
Package ui provides methods to invoke procedures in the UI service.

Jump to

Keyboard shortcuts

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