Add handler context and echo updates handling

This commit is contained in:
Maciej Krzyżanowski 2024-12-29 03:06:56 +01:00
parent 2e19f8cb4b
commit f0958a79e1
4 changed files with 47 additions and 9 deletions

View File

@ -3,18 +3,32 @@ package daemonconn
const ( const (
MessageTypeEchoRequest = iota MessageTypeEchoRequest = iota
MessageTypeEchoResponse MessageTypeEchoResponse
MessageTypeEchoUpdate
) )
type EchoRequest struct { type EchoRequest struct {
EchoByte byte EchoByte byte
UpdatesCount byte
} }
func (EchoRequest) TypeID() uint32 { func (EchoRequest) TypeID() uint32 {
return MessageTypeEchoRequest return MessageTypeEchoRequest
} }
type EchoResponse EchoRequest type EchoResponse struct {
EchoByte byte
}
func (EchoResponse) TypeID() uint32 { func (EchoResponse) TypeID() uint32 {
return MessageTypeEchoResponse return MessageTypeEchoResponse
} }
// EchoUpdate should be sent from the side, which received EchoRequest
// UpdatesCount times (which is specified in the EchoRequest message)
type EchoUpdate struct {
EchoByte byte
}
func (EchoUpdate) TypeID() uint32 {
return MessageTypeEchoUpdate
}

View File

@ -56,7 +56,7 @@ func main() {
panic(err) panic(err)
} }
err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123}) err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123, UpdatesCount: 16})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -3,11 +3,30 @@ package msghandlers
import ( import (
"daemonSocketExample/daemonconn" "daemonSocketExample/daemonconn"
"log/slog" "log/slog"
"time"
) )
func handleEchoRequest(msg daemonconn.Message) error { func handleEchoRequest(ctx HandlerContext, msg daemonconn.Message) error {
echoRequest := msg.(daemonconn.EchoRequest) echoRequest := msg.(daemonconn.EchoRequest)
slog.Info("Got echo request", "echoByte", echoRequest.EchoByte) slog.Info("Got echo request", "echoByte", echoRequest.EchoByte, "updatesCount", echoRequest.UpdatesCount)
for i := 0; i < int(echoRequest.UpdatesCount); i++ {
err := ctx.conn.WriteMessage(daemonconn.EchoUpdate{EchoByte: echoRequest.EchoByte})
if err != nil {
return err
}
slog.Info("Sent echo update", "i", i)
time.Sleep(1 * time.Second)
}
echoResponse := daemonconn.EchoResponse{EchoByte: echoRequest.EchoByte}
err := ctx.conn.WriteMessage(echoResponse)
if err != nil {
return err
}
slog.Info("Sent echo response", "echoByte", echoResponse.EchoByte)
return nil return nil
} }

View File

@ -4,17 +4,22 @@ import (
"daemonSocketExample/daemonconn" "daemonSocketExample/daemonconn"
) )
type MessageHandler func(daemonconn.Message) error type HandlerContext struct {
conn daemonconn.DaemonConn
}
type MessageHandler func(HandlerContext, daemonconn.Message) error
var Handlers = map[uint32]MessageHandler{ var Handlers = map[uint32]MessageHandler{
daemonconn.MessageTypeEchoRequest: handleEchoRequest, daemonconn.MessageTypeEchoRequest: handleEchoRequest,
} }
func HandleDaemonConn(conn daemonconn.DaemonConn) error { func HandleDaemonConn(conn daemonconn.DaemonConn) error {
ctx := HandlerContext{conn: conn}
msg, err := conn.ReadMessage() msg, err := conn.ReadMessage()
for err == nil { for err == nil {
err := handleMessage(msg) err := handleMessage(ctx, msg)
if err != nil { if err != nil {
return err return err
} }
@ -25,8 +30,8 @@ func HandleDaemonConn(conn daemonconn.DaemonConn) error {
return err return err
} }
func handleMessage(msg daemonconn.Message) error { func handleMessage(ctx HandlerContext, msg daemonconn.Message) error {
handlerFunc := Handlers[msg.TypeID()] handlerFunc := Handlers[msg.TypeID()]
return handlerFunc(msg) return handlerFunc(ctx, msg)
} }