Add handler context and echo updates handling
This commit is contained in:
parent
2e19f8cb4b
commit
f0958a79e1
@ -3,18 +3,32 @@ package daemonconn
|
||||
const (
|
||||
MessageTypeEchoRequest = iota
|
||||
MessageTypeEchoResponse
|
||||
MessageTypeEchoUpdate
|
||||
)
|
||||
|
||||
type EchoRequest struct {
|
||||
EchoByte byte
|
||||
UpdatesCount byte
|
||||
}
|
||||
|
||||
func (EchoRequest) TypeID() uint32 {
|
||||
return MessageTypeEchoRequest
|
||||
}
|
||||
|
||||
type EchoResponse EchoRequest
|
||||
type EchoResponse struct {
|
||||
EchoByte byte
|
||||
}
|
||||
|
||||
func (EchoResponse) TypeID() uint32 {
|
||||
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
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -56,7 +56,7 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123})
|
||||
err = conn.WriteMessage(daemonconn.EchoRequest{EchoByte: 123, UpdatesCount: 16})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -3,11 +3,30 @@ package msghandlers
|
||||
import (
|
||||
"daemonSocketExample/daemonconn"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
func handleEchoRequest(msg daemonconn.Message) error {
|
||||
func handleEchoRequest(ctx HandlerContext, msg daemonconn.Message) error {
|
||||
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
|
||||
}
|
||||
|
@ -4,17 +4,22 @@ import (
|
||||
"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{
|
||||
daemonconn.MessageTypeEchoRequest: handleEchoRequest,
|
||||
}
|
||||
|
||||
func HandleDaemonConn(conn daemonconn.DaemonConn) error {
|
||||
ctx := HandlerContext{conn: conn}
|
||||
msg, err := conn.ReadMessage()
|
||||
|
||||
for err == nil {
|
||||
err := handleMessage(msg)
|
||||
err := handleMessage(ctx, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -25,8 +30,8 @@ func HandleDaemonConn(conn daemonconn.DaemonConn) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func handleMessage(msg daemonconn.Message) error {
|
||||
func handleMessage(ctx HandlerContext, msg daemonconn.Message) error {
|
||||
handlerFunc := Handlers[msg.TypeID()]
|
||||
|
||||
return handlerFunc(msg)
|
||||
return handlerFunc(ctx, msg)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user