diff options
Diffstat (limited to 'core/payload.go')
| -rw-r--r-- | core/payload.go | 95 |
1 files changed, 83 insertions, 12 deletions
diff --git a/core/payload.go b/core/payload.go index 0711040..b8e8b9f 100644 --- a/core/payload.go +++ b/core/payload.go @@ -5,6 +5,35 @@ import ( "time" ) +type Success struct{} + +func (s Success) Wrap() (PayloadType, []any) { + return PayloadSuccess, []any{} +} + +type Error struct { + ErrorType ErrorType +} + +func (e Error) Wrap() (PayloadType, []any) { + return PayloadError, []any{e.ErrorType} +} + +func DecodeError(buf io.Reader) (Error, error) { + var ( + errorData uint8 + e Error + ) + + err := decodeNumeric(buf, &errorData) + if err != nil { + return e, err + } + + e.ErrorType = ErrorType(errorData) + return e, nil +} + type Handshake struct { Major, Minor uint8 } @@ -31,30 +60,72 @@ func DecodeHandshake(buf io.Reader) (Handshake, error) { return h, nil } -type Ping struct{} +type Auth struct { + Name string + Pass string +} -func (p Ping) Wrap() (PayloadType, []any) { - return PayloadPing, []any{} +func (a Auth) Wrap() (PayloadType, []any) { + return PayloadAuth, []any{ + a.Name, a.Pass, + } } -type Pong struct { +func DecodeAuth(buf io.Reader) (Auth, error) { + var a Auth + err := decodeString(buf, &a.Name) + if err != nil { + return a, err + } + + err = decodeString(buf, &a.Pass) + if err != nil { + return a, err + } + + return a, nil +} + +type Message struct { + Source string + Target string Timestamp time.Time + Content string } -func (p Pong) Wrap() (PayloadType, []any) { - return PayloadPong, []any{ - p.Timestamp, +func (m Message) Wrap() (PayloadType, []any) { + return PayloadMessage, []any{ + m.Source, + m.Target, + m.Timestamp, + m.Content, } } -func DecodePong(buf io.Reader) (Pong, error) { - var p Pong - err := decodeTime(buf, &p.Timestamp) +func DecodeMessage(buf io.Reader) (Message, error) { + var m Message + + err := decodeString(buf, &m.Source) + if err != nil { + return m, err + } + + err = decodeString(buf, &m.Target) + if err != nil { + return m, err + } + + err = decodeTime(buf, &m.Timestamp) + if err != nil { + return m, err + } + + err = decodeString(buf, &m.Content) if err != nil { - return p, err + return m, err } - return p, nil + return m, nil } type Test struct { |
