aboutsummaryrefslogtreecommitdiff
path: root/process.go
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2022-05-01 10:28:41 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2022-05-01 10:28:41 +0200
commitfabb8e2400e22a638c105c0a9cd99919f2fc7dee (patch)
treeaaacd907773c8ca6afe6b6c5240ff7c9e4ca8648 /process.go
parentc71129fdf4827d1757ce6d3d2be7c89e22987560 (diff)
Make chat command handlers run concurrently
They can't hang the packet handler anymore and the user is warned if commands take a lot of time to run
Diffstat (limited to 'process.go')
-rw-r--r--process.go33
1 files changed, 27 insertions, 6 deletions
diff --git a/process.go b/process.go
index 5208c97..3fe0c24 100644
--- a/process.go
+++ b/process.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net"
"strings"
+ "time"
"github.com/HimbeerserverDE/srp"
"github.com/anon55555/mt"
@@ -432,12 +433,32 @@ func (cc *ClientConn) process(pkt mt.Pkt) {
srv.swapAOID(&cmd.Pointed.(*mt.PointedAO).ID)
}
case *mt.ToSrvChatMsg:
- result, isCmd := onChatMsg(cc, cmd)
- if !isCmd {
- break
- } else if result != "" {
- cc.SendChatMsg(result)
- }
+ done := make(chan struct{})
+
+ go func(done chan<- struct{}) {
+ result, isCmd := onChatMsg(cc, cmd)
+ if !isCmd {
+ if srv == nil {
+ cc.Log("->", "no server")
+ return
+ }
+
+ srv.Send(pkt)
+ } else if result != "" {
+ cc.SendChatMsg(result)
+ }
+
+ close(done)
+ }(done)
+
+ go func(done <-chan struct{}) {
+ select {
+ case <-done:
+ case <-time.After(ChatCmdTimeout):
+ cmdName := strings.Split(cmd.Msg, " ")[0]
+ cc.SendChatMsg("Command", cmdName, "is taking suspiciously long to execute.")
+ }
+ }(done)
return
}