hopp/examples/chat/generated.go

370 lines
11 KiB
Go

package chat
import "git.tebibyte.media/sashakoshka/hopp"
import "git.tebibyte.media/sashakoshka/hopp/tape"
// Send sends one message along a transaction.
func Send(trans hopp.Trans, message hopp.Message) error {
buffer, err := message.MarshalBinary()
if err != nil { return err }
return trans.Send(message.Method(), buffer)
}
// Receive receives one message from a transaction.
func Receive(trans hopp.Trans) (hopp.Message, error) {
method, data, err := trans.Receive()
if err != nil { return nil, err }
switch method {
case 0x0000:
message := &MessageError { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0001:
message := &MessageSuccess { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0100:
message := &MessageUpdateProfile { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0200:
message := &MessageJoin { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0201:
message := &MessageChat { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0300:
message := &MessageJoinNotify { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
case 0x0301:
message := &MessageLeaveNotify { }
err := message.UnmarshalBinary(data)
if err != nil { return nil, err }
return message, nil
default: return nil, hopp.ErrUnknownMethod
}
}
// (0) Error is sent by a party when the other party has done something erroneous. The valid error codes are:
//
// 0: General, unspecified error
//
// The description field, if specified, determines a human-readable error to be shown to the user. The sending party must immediately close the transaction after this message is sent.
type MessageError struct {
/* 0 */ Code uint16
/* 1 */ Description hopp.Option[string]
}
// Method returns the method number of the message.
func (msg MessageError) Method() uint16 {
return 0
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageError) MarshalBinary() ([]byte, error) {
size := 0
count := 1
offsetCode := size
{ value := msg.Code
size += 2; _ = value }
offsetDescription := size
if value, ok := msg.Description.Get(); ok {
count ++
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
{ value := msg.Code
tape.EncodeI16(buffer[offsetCode:], value)}
if value, ok := msg.Description.Get(); ok {
tape.EncodeString(buffer[offsetDescription:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageError) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
foundRequired := 0
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeI16[uint16](data)
if err != nil { return err }
msg.Code = value
foundRequired ++
case 1:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Description = hopp.O(value)
}
}
if foundRequired != 1 { return hopp.ErrTablePairMissing }
return nil
}
// (1) Success is sent by a party when it has successfully completed a task given to it by the other party. The sending party must immediately close the transaction after this message is sent.
type MessageSuccess struct {
}
// Method returns the method number of the message.
func (msg MessageSuccess) Method() uint16 {
return 1
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageSuccess) MarshalBinary() ([]byte, error) {
size := 0
count := 0
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageSuccess) UnmarshalBinary(buffer []byte) error {
// no fields
return nil
}
// (256) UpdateProfile is sent by the client in a new transaction to update the profile details that will be shown to other connected clients.
type MessageUpdateProfile struct {
/* 0 */ Nickname hopp.Option[string]
}
// Method returns the method number of the message.
func (msg MessageUpdateProfile) Method() uint16 {
return 256
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageUpdateProfile) MarshalBinary() ([]byte, error) {
size := 0
count := 0
offsetNickname := size
if value, ok := msg.Nickname.Get(); ok {
count ++
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
if value, ok := msg.Nickname.Get(); ok {
tape.EncodeString(buffer[offsetNickname:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageUpdateProfile) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
foundRequired := 0
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Nickname = hopp.O(value)
}
}
if foundRequired != 1 { return hopp.ErrTablePairMissing }
return nil
}
// (512) Join is sent by the client when it wishes to join a room. It must begin a new transaction, and that transaction will persist while the user is in that room. Messages having to do with the room will be sent along this transaction. To leave the room, the client must close the transaction.
type MessageJoin struct {
/* 0 */ Room string
}
// Method returns the method number of the message.
func (msg MessageJoin) Method() uint16 {
return 512
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageJoin) MarshalBinary() ([]byte, error) {
size := 0
count := 1
offsetRoom := size
{ value := msg.Room
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
{ value := msg.Room
tape.EncodeString(buffer[offsetRoom:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageJoin) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Room = value
}
}
return nil
}
// (513) Chat is sent by the client when it wishes to post a message to the room. It is also relayed by the server to other clients to notify them of the message. It must be sent within a room transaction.
type MessageChat struct {
/* 0 */ Nickname hopp.Option[string]
/* 1 */ Content string
}
// Method returns the method number of the message.
func (msg MessageChat) Method() uint16 {
return 513
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageChat) MarshalBinary() ([]byte, error) {
size := 0
count := 1
offsetNickname := size
if value, ok := msg.Nickname.Get(); ok {
count ++
size += len(value) }
offsetContent := size
{ value := msg.Content
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
if value, ok := msg.Nickname.Get(); ok {
tape.EncodeString(buffer[offsetNickname:], value)}
{ value := msg.Content
tape.EncodeString(buffer[offsetContent:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageChat) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
foundRequired := 0
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Nickname = hopp.O(value)
case 1:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Content = value
foundRequired ++
}
}
if foundRequired != 1 { return hopp.ErrTablePairMissing }
return nil
}
// (768) JoinNotify is sent by the server when another client joins the room. It must be sent within a room transaction.
type MessageJoinNotify struct {
/* 0 */ Nickname hopp.Option[string]
}
// Method returns the method number of the message.
func (msg MessageJoinNotify) Method() uint16 {
return 768
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageJoinNotify) MarshalBinary() ([]byte, error) {
size := 0
count := 0
offsetNickname := size
if value, ok := msg.Nickname.Get(); ok {
count ++
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
if value, ok := msg.Nickname.Get(); ok {
tape.EncodeString(buffer[offsetNickname:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageJoinNotify) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
foundRequired := 0
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Nickname = hopp.O(value)
}
}
if foundRequired != 1 { return hopp.ErrTablePairMissing }
return nil
}
// (769) LeaveNotify is sent by the server when another client leaves the room. It must be sent within a room transaction.
type MessageLeaveNotify struct {
/* 0 */ Nickname hopp.Option[string]
}
// Method returns the method number of the message.
func (msg MessageLeaveNotify) Method() uint16 {
return 769
}
// MarshalBinary encodes the data in this message into a buffer.
func (msg *MessageLeaveNotify) MarshalBinary() ([]byte, error) {
size := 0
count := 0
offsetNickname := size
if value, ok := msg.Nickname.Get(); ok {
count ++
size += len(value) }
if size > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
if count > 0xFFFF { return nil, hopp.ErrPayloadTooLarge}
buffer := make([]byte, 2 + 4 * count + size)
tape.EncodeI16(buffer[:2], uint16(count))
if value, ok := msg.Nickname.Get(); ok {
tape.EncodeString(buffer[offsetNickname:], value)}
return buffer, nil
}
// UnmarshalBinary dencodes the data from a buffer int this message.
func (msg *MessageLeaveNotify) UnmarshalBinary(buffer []byte) error {
pairs, err := tape.DecodePairs(buffer)
if err != nil { return err }
foundRequired := 0
for tag, data := range pairs {
switch tag {
case 0:
value, err := tape.DecodeString[string](data)
if err != nil { return err }
msg.Nickname = hopp.O(value)
}
}
if foundRequired != 1 { return hopp.ErrTablePairMissing }
return nil
}