]> git.lizzy.rs Git - hydra-dragonfire.git/blobdiff - client.go
Re-add luax as regular directory
[hydra-dragonfire.git] / client.go
index 341f564a257bc6e3e1c1b910f0b1c40c45ca8242..d27af337946edb5c5c3d21a409b9b555de76f4fb 100644 (file)
--- a/client.go
+++ b/client.go
@@ -3,8 +3,7 @@ package main
 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"
@@ -20,7 +19,7 @@ const (
 
 type Component interface {
        create(client *Client, l *lua.LState)
-       tolua() lua.LValue
+       push() lua.LValue
        connect()
        process(pkt *mt.Pkt)
 }
@@ -42,7 +41,7 @@ var clientFuncs = map[string]lua.LGFunction{
        "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,
@@ -77,7 +76,7 @@ func getStrings(l *lua.LState) []string {
        return strs
 }
 
-func (client *Client) disconnect() {
+func (client *Client) closeConn() {
        client.mu.Lock()
        defer client.mu.Unlock()
 
@@ -109,7 +108,7 @@ func l_client_index(l *lua.LState) int {
        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)
        }
@@ -163,16 +162,15 @@ func l_client_connect(l *lua.LState) int {
 
                        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
@@ -193,14 +191,14 @@ func l_client_poll(l *lua.LState) int {
        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
 }
 
@@ -258,7 +256,12 @@ func l_client_wildcard(l *lua.LState) int {
 
 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()