2024-12-28 19:11:50 +01:00
|
|
|
package msghandlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"daemonSocketExample/daemonconn"
|
|
|
|
)
|
|
|
|
|
2024-12-29 03:06:56 +01:00
|
|
|
type HandlerContext struct {
|
|
|
|
conn daemonconn.DaemonConn
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageHandler func(HandlerContext, daemonconn.Message) error
|
2024-12-28 19:11:50 +01:00
|
|
|
|
|
|
|
var Handlers = map[uint32]MessageHandler{
|
|
|
|
daemonconn.MessageTypeEchoRequest: handleEchoRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleDaemonConn(conn daemonconn.DaemonConn) error {
|
2024-12-29 03:06:56 +01:00
|
|
|
ctx := HandlerContext{conn: conn}
|
2024-12-28 19:11:50 +01:00
|
|
|
msg, err := conn.ReadMessage()
|
|
|
|
|
|
|
|
for err == nil {
|
2024-12-29 03:06:56 +01:00
|
|
|
err := handleMessage(ctx, msg)
|
2024-12-28 19:11:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err = conn.ReadMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-29 03:06:56 +01:00
|
|
|
func handleMessage(ctx HandlerContext, msg daemonconn.Message) error {
|
2024-12-28 19:11:50 +01:00
|
|
|
handlerFunc := Handlers[msg.TypeID()]
|
|
|
|
|
2024-12-29 03:06:56 +01:00
|
|
|
return handlerFunc(ctx, msg)
|
2024-12-28 19:11:50 +01:00
|
|
|
}
|