144 lines
4.5 KiB
Go
144 lines
4.5 KiB
Go
package ping
|
|
|
|
// Code generated by the Holanet PDL compiler. DO NOT EDIT.
|
|
// The source file is located at <path>
|
|
// Please edit that file instead, and re-compile it to this location.
|
|
// HOPP, TAPE, METADAPT, PDL/0 (c) 2025 holanet.xyz
|
|
|
|
import "fmt"
|
|
import "git.tebibyte.media/sashakoshka/hopp"
|
|
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
|
|
|
// Table is a KTV table with an undefined schema.
|
|
type Table = map[uint16] any
|
|
|
|
// Message is any message that can be sent along this protocol.
|
|
type Message interface {
|
|
tape.Encodable
|
|
tape.Decodable
|
|
|
|
// Method returns the method code of the message.
|
|
Method() uint16
|
|
}
|
|
|
|
// Send encodes a message and sends it along a transaction.
|
|
func Send(trans hopp.Trans, message Message) (n int, err error) {
|
|
writer, err := trans.SendWriter(message.Method())
|
|
if err != nil { return n, err }
|
|
defer writer.Close()
|
|
encoder := tape.NewEncoder(writer)
|
|
n, err = message.Encode(encoder)
|
|
if err != nil { return n, err }
|
|
return n, encoder.Flush()
|
|
}
|
|
|
|
// canAssign determines if data from the given source tag can be assigned to
|
|
// a Go type represented by destination. It is designed to receive destination
|
|
// values from [generate.Generator.generateCanAssign]. The eventual Go type and
|
|
// the destination tag must come from the same (or hash-equivalent) PDL type.
|
|
func canAssign(destination, source tape.Tag) bool {
|
|
if destination.Is(source) { return true }
|
|
if (destination.Is(tape.SBA) || destination.Is(tape.LBA)) &&
|
|
(source.Is(tape.SBA) || source.Is(tape.LBA)) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// boolInt converts a bool to an integer.
|
|
func boolInt(input bool) int {
|
|
if input {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// ensure ucontainer is always imported
|
|
var _ hopp.Option[int]
|
|
|
|
// Ping is sent by the client to the server. It may contain any number. This
|
|
// number will be returned to the client via a [Pong] message.
|
|
type MessagePing int32
|
|
|
|
// Method returns the message's method number.
|
|
func(this *MessagePing) Method() uint16 { return 0x0000 }
|
|
|
|
// Encode encodes this message's tag and value.
|
|
func(this *MessagePing) Encode(encoder *tape.Encoder) (n int, err error) {
|
|
tag_1 := tape.LSI.WithCN(3)
|
|
nn, err := encoder.WriteTag(tag_1)
|
|
n += nn; if err != nil { return n, err }
|
|
nn, err = encoder.WriteInt32(int32((*this)))
|
|
n += nn; if err != nil { return n, err }
|
|
return n, nil
|
|
}
|
|
|
|
// Decode decodes this message's tag and value.
|
|
func(this *MessagePing) Decode(decoder *tape.Decoder) (n int, err error) {
|
|
tag, nn, err := decoder.ReadTag()
|
|
n += nn; if err != nil { return n, err }
|
|
if !(canAssign(tape.LSI, tag)) {
|
|
nn, err = tape.Skim(decoder, tag)
|
|
n += nn; if err != nil { return n, err }
|
|
return n, nil
|
|
}
|
|
destination_2, nn, err := decoder.ReadInt32()
|
|
n += nn; if err != nil { return n, err }
|
|
*this = MessagePing(destination_2)
|
|
return n, nil
|
|
}
|
|
|
|
// Pong is sent by the server to the client in response to a [Ping] message, It
|
|
// will contain the same number as that message.
|
|
type MessagePong int32
|
|
|
|
// Method returns the message's method number.
|
|
func(this *MessagePong) Method() uint16 { return 0x0001 }
|
|
|
|
// Encode encodes this message's tag and value.
|
|
func(this *MessagePong) Encode(encoder *tape.Encoder) (n int, err error) {
|
|
tag_3 := tape.LSI.WithCN(3)
|
|
nn, err := encoder.WriteTag(tag_3)
|
|
n += nn; if err != nil { return n, err }
|
|
nn, err = encoder.WriteInt32(int32((*this)))
|
|
n += nn; if err != nil { return n, err }
|
|
return n, nil
|
|
}
|
|
|
|
// Decode decodes this message's tag and value.
|
|
func(this *MessagePong) Decode(decoder *tape.Decoder) (n int, err error) {
|
|
tag, nn, err := decoder.ReadTag()
|
|
n += nn; if err != nil { return n, err }
|
|
if !(canAssign(tape.LSI, tag)) {
|
|
nn, err = tape.Skim(decoder, tag)
|
|
n += nn; if err != nil { return n, err }
|
|
return n, nil
|
|
}
|
|
destination_4, nn, err := decoder.ReadInt32()
|
|
n += nn; if err != nil { return n, err }
|
|
*this = MessagePong(destination_4)
|
|
return n, nil
|
|
}
|
|
|
|
// Receive decodes a message from a transaction and returns it as a value.
|
|
// Use a type switch to determine what type of message it is.
|
|
func Receive(trans hopp.Trans) (message any, n int, err error) {
|
|
method, reader, err := trans.ReceiveReader()
|
|
decoder := tape.NewDecoder(reader)
|
|
if err != nil { return nil, n, err }
|
|
switch method {
|
|
case 0x0000:
|
|
var message MessagePing
|
|
nn, err := message.Decode(decoder)
|
|
n += nn; if err != nil { return nil, n, err }
|
|
return message, n, nil
|
|
case 0x0001:
|
|
var message MessagePong
|
|
nn, err := message.Decode(decoder)
|
|
n += nn; if err != nil { return nil, n, err }
|
|
return message, n, nil
|
|
}
|
|
return nil, n, fmt.Errorf("%w: M%04X", hopp.ErrUnknownMethod, method)
|
|
}
|