import (
"errors"
"github.com/anon55555/mt"
- "github.com/dragonfireclient/hydra-dragonfire/fromlua"
- "github.com/dragonfireclient/hydra-dragonfire/tolua"
+ "github.com/dragonfireclient/hydra-dragonfire/convert"
"github.com/yuin/gopher-lua"
"net"
"sync"
type Component interface {
create(client *Client, l *lua.LState)
- tolua() lua.LValue
+ push() lua.LValue
connect()
process(pkt *mt.Pkt)
}
"state": l_client_state,
"connect": l_client_connect,
"poll": l_client_poll,
- "disconnect": l_client_disconnect,
+ "close": l_client_close,
"enable": l_client_enable,
"subscribe": l_client_subscribe,
"unsubscribe": l_client_unsubscribe,
return strs
}
-func (client *Client) disconnect() {
+func (client *Client) closeConn() {
client.mu.Lock()
defer client.mu.Unlock()
if fun, exists := clientFuncs[key]; exists {
l.Push(l.NewFunction(fun))
} else if component, exists := client.components[key]; exists {
- l.Push(component.tolua())
+ l.Push(component.push())
} else {
l.Push(lua.LNil)
}
if err == nil {
client.mu.Lock()
-
for _, component := range client.components {
component.process(&pkt)
}
+ _, subscribed := client.subscribed[string(convert.PushPktType(&pkt))]
+ client.mu.Unlock()
- if _, exists := client.subscribed[string(tolua.PktType(&pkt))]; exists || client.wildcard {
+ if subscribed || client.wildcard {
client.queue <- &pkt
}
-
- client.mu.Unlock()
} else if errors.Is(err, net.ErrClosed) {
close(client.queue)
return
client := getClient(l)
_, pkt, timeout := doPoll(l, []*Client{client})
- l.Push(tolua.Pkt(l, pkt))
+ l.Push(convert.PushPkt(l, pkt))
l.Push(lua.LBool(timeout))
return 2
}
-func l_client_disconnect(l *lua.LState) int {
+func l_client_close(l *lua.LState) int {
client := getClient(l)
- client.disconnect()
+ client.closeConn()
return 0
}
func l_client_send(l *lua.LState) int {
client := getClient(l)
- cmd := fromlua.Cmd(l)
+
+ if client.state != csConnected {
+ panic("not connected")
+ }
+
+ cmd := convert.ReadCmd(l)
doAck := l.ToBool(4)
client.mu.Lock()