38 lines
703 B
Go

package msghandlers
import (
"daemonSocketExample/daemonconn"
)
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(ctx, msg)
if err != nil {
return err
}
msg, err = conn.ReadMessage()
}
return err
}
func handleMessage(ctx HandlerContext, msg daemonconn.Message) error {
handlerFunc := Handlers[msg.TypeID()]
return handlerFunc(ctx, msg)
}