Skip to content

Commit 2e36007

Browse files
major cleanup / simplification
1 parent 7d11da6 commit 2e36007

File tree

8 files changed

+50
-136
lines changed

8 files changed

+50
-136
lines changed

cmd/client/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func main() {
4343
fmt.Printf("Connecting to '%s:%d' with username '%s'\n", host, port, username)
4444

4545
client := commandclient.NewCommandClient(host, port)
46-
go commandclient.DebugHandler(client)
47-
4846
err := client.Connect()
4947
if err != nil {
5048
panic(err)
@@ -55,25 +53,27 @@ func main() {
5553
panic(err)
5654
}
5755

58-
if !stalk {
56+
if stalk {
57+
go commandclient.DebugHandler(client)
58+
59+
} else {
5960
err = commandclient.Login(client, username, password, true)
6061
if err != nil {
6162
panic(err)
6263
}
63-
64-
go func() {
65-
err = commandclient.ClientReady(client)
66-
if err != nil {
67-
panic(err)
68-
}
69-
}()
64+
fmt.Println("Successfully logged in")
7065

7166
if downloadmedia {
7267
err = commandclient.FetchMedia(client)
7368
if err != nil {
7469
panic(err)
7570
}
7671
}
72+
73+
err = commandclient.ClientReady(client)
74+
if err != nil {
75+
panic(err)
76+
}
7777
}
7878

7979
c := make(chan os.Signal, 1)

commandclient/client.go

Lines changed: 31 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net"
7-
"sync"
8-
"time"
97

108
"github.com/minetest-go/minetest_client/commands"
119
"github.com/minetest-go/minetest_client/packet"
@@ -14,28 +12,22 @@ import (
1412
var ErrTimeout = errors.New("timeout")
1513

1614
type CommandClient struct {
17-
conn net.Conn
18-
Host string
19-
Port int
20-
PeerID uint16
21-
sph *packet.SplitpacketHandler
22-
netrx chan []byte
23-
listeners []chan commands.Command
24-
listener_lock *sync.RWMutex
25-
payload_listeners []chan []byte
26-
payload_listener_lock *sync.RWMutex
15+
conn net.Conn
16+
Host string
17+
Port int
18+
PeerID uint16
19+
sph *packet.SplitpacketHandler
20+
netrx chan []byte
21+
cmd_chan chan commands.Command
2722
}
2823

2924
func NewCommandClient(host string, port int) *CommandClient {
3025
return &CommandClient{
31-
Host: host,
32-
Port: port,
33-
sph: packet.NewSplitPacketHandler(),
34-
netrx: make(chan []byte, 1000),
35-
listeners: make([]chan commands.Command, 0),
36-
listener_lock: &sync.RWMutex{},
37-
payload_listeners: make([]chan []byte, 0),
38-
payload_listener_lock: &sync.RWMutex{},
26+
Host: host,
27+
Port: port,
28+
sph: packet.NewSplitPacketHandler(),
29+
netrx: make(chan []byte, 1000),
30+
cmd_chan: make(chan commands.Command, 1000),
3931
}
4032
}
4133

@@ -59,79 +51,12 @@ func (c *CommandClient) Disconnect() error {
5951
return err
6052
}
6153
close(c.netrx)
62-
for _, l := range c.listeners {
63-
close(l)
64-
}
65-
54+
close(c.cmd_chan)
6655
return c.conn.Close()
6756
}
6857

69-
func (c *CommandClient) AddListener(ch chan commands.Command) {
70-
c.listener_lock.Lock()
71-
defer c.listener_lock.Unlock()
72-
c.listeners = append(c.listeners, ch)
73-
}
74-
75-
func (c *CommandClient) RemoveListener(ch chan commands.Command) {
76-
c.listener_lock.Lock()
77-
defer c.listener_lock.Unlock()
78-
newlisteners := make([]chan commands.Command, 0)
79-
for _, l := range c.listeners {
80-
if l != ch {
81-
newlisteners = append(newlisteners, l)
82-
}
83-
}
84-
c.listeners = newlisteners
85-
}
86-
87-
func (c *CommandClient) AddPayloadListener(ch chan []byte) {
88-
c.payload_listener_lock.Lock()
89-
defer c.payload_listener_lock.Unlock()
90-
c.payload_listeners = append(c.payload_listeners, ch)
91-
}
92-
93-
func (c *CommandClient) RemovePayloadListener(ch chan []byte) {
94-
c.payload_listener_lock.Lock()
95-
defer c.payload_listener_lock.Unlock()
96-
97-
newlisteners := make([]chan []byte, 0)
98-
for _, l := range c.payload_listeners {
99-
if l != ch {
100-
newlisteners = append(newlisteners, l)
101-
}
102-
}
103-
c.payload_listeners = newlisteners
104-
}
105-
106-
func (c *CommandClient) WaitFor(cmd commands.Command, timeout time.Duration) error {
107-
ch := make(chan []byte, 1000)
108-
c.AddPayloadListener(ch)
109-
defer c.RemovePayloadListener(ch)
110-
until := time.Now().Add(timeout)
111-
112-
for {
113-
select {
114-
case <-time.After(time.Until(until)):
115-
return ErrTimeout
116-
case payload := <-ch:
117-
cmdId := commands.GetCommandID(payload)
118-
if cmdId == cmd.GetCommandId() {
119-
return cmd.UnmarshalPacket(commands.GetCommandPayload(payload))
120-
}
121-
}
122-
}
123-
}
124-
125-
func (c *CommandClient) emitCommand(cmd commands.Command) {
126-
c.listener_lock.RLock()
127-
defer c.listener_lock.RUnlock()
128-
129-
for _, ch := range c.listeners {
130-
select {
131-
case ch <- cmd:
132-
default:
133-
}
134-
}
58+
func (c *CommandClient) CommandChannel() chan commands.Command {
59+
return c.cmd_chan
13560
}
13661

13762
func (c *CommandClient) SendOriginalCommand(cmd commands.Command) error {
@@ -195,31 +120,36 @@ func (c *CommandClient) Send(packet *packet.Packet) error {
195120
}
196121

197122
func (c *CommandClient) handleCommandPayload(payload []byte) error {
198-
c.payload_listener_lock.RLock()
199-
for _, ch := range c.payload_listeners {
200-
ch <- payload
201-
}
202-
c.payload_listener_lock.RUnlock()
123+
//fmt.Printf("Received bytes: len=%d, cmdId=%d\n", len(payload), commands.GetCommandID(payload))
203124

204125
cmd, err := commands.Parse(payload)
205126
if err != nil {
206127
return err
207128
}
208-
c.emitCommand(cmd)
129+
130+
c.cmd_chan <- cmd
209131
return nil
210132
}
211133

212134
func (c *CommandClient) onReceive(p *packet.Packet) error {
213-
//fmt.Printf("Received packet: %s\n", p)
214-
135+
//fmt.Printf("Packet: %s\n", p.String())
215136
if p.PacketType == packet.Reliable || p.PacketType == packet.Original {
216137
if p.ControlType == packet.SetPeerID {
217138
c.PeerID = p.PeerID
218-
cmd := &commands.ServerSetPeer{
219-
PeerID: p.PeerID,
139+
cmd := &commands.ServerSetPeer{PeerID: p.PeerID}
140+
141+
// send as raw payload to potential listeners
142+
payload, err := commands.CreatePayload(cmd)
143+
if err != nil {
144+
return fmt.Errorf("peerId marshal error: %v", err)
145+
}
146+
147+
err = c.handleCommandPayload(payload)
148+
if err != nil {
149+
return fmt.Errorf("handleCommandPayload error: %v", err)
220150
}
221151

222-
c.emitCommand(cmd)
152+
c.cmd_chan <- cmd
223153
}
224154
}
225155

commandclient/clientready.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import (
77
)
88

99
func ClientReady(cc *CommandClient) error {
10-
ch := make(chan commands.Command, 100)
11-
cc.AddListener(ch)
12-
defer cc.RemoveListener(ch)
13-
14-
for o := range ch {
10+
for o := range cc.CommandChannel() {
1511
switch o.(type) {
1612
case *commands.ServerCSMRestrictionFlags:
1713
err := cc.SendCommand(commands.NewClientReady(5, 5, 5, "mt-bot", 4))

commandclient/debughandler.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import (
99
)
1010

1111
func DebugHandler(cc *CommandClient) error {
12-
ch := make(chan commands.Command, 100)
13-
cc.AddListener(ch)
14-
defer cc.RemoveListener(ch)
15-
16-
for o := range ch {
12+
for o := range cc.CommandChannel() {
1713
switch cmd := o.(type) {
1814
case *commands.ServerBlockData:
1915
gotblocks := commands.NewClientGotBlocks()

commandclient/fetchmedia.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ import (
99
)
1010

1111
func FetchMedia(cc *CommandClient) error {
12-
ch := make(chan commands.Command, 1000)
13-
cc.AddListener(ch)
14-
defer cc.RemoveListener(ch)
1512
received_count := 0
1613
expected_count := 0
1714

18-
for o := range ch {
15+
for o := range cc.CommandChannel() {
1916
switch cmd := o.(type) {
2017

2118
case *commands.ServerAnnounceMedia:
@@ -50,6 +47,7 @@ func FetchMedia(cc *CommandClient) error {
5047
}
5148
} else {
5249
// nothing to fetch
50+
fmt.Printf("All media files up to date\n")
5351
return nil
5452
}
5553

commandclient/init.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import (
88
)
99

1010
func Init(cc *CommandClient, username string) error {
11-
ch := make(chan commands.Command, 100)
12-
cc.AddListener(ch)
13-
defer cc.RemoveListener(ch)
14-
15-
for o := range ch {
11+
for o := range cc.CommandChannel() {
1612
switch o.(type) {
1713
case *commands.ServerSetPeer:
1814
time.Sleep(1 * time.Second)

commandclient/login.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ var ErrAccessDenied = errors.New("access denied")
1212
var ErrNotRegistered = errors.New("username not registered")
1313

1414
func Login(cc *CommandClient, username, password string, enable_registration bool) error {
15-
ch := make(chan commands.Command, 100)
16-
cc.AddListener(ch)
17-
defer cc.RemoveListener(ch)
18-
1915
var srppub, srppriv []byte
2016

21-
for o := range ch {
17+
for o := range cc.CommandChannel() {
2218
switch cmd := o.(type) {
2319
case *commands.ServerHello:
2420
packet.ResetSeqNr(65500)

commands/server_setpeer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ func (p *ServerSetPeer) GetCommandId() uint16 {
1414
}
1515

1616
func (p *ServerSetPeer) MarshalPacket() ([]byte, error) {
17-
return nil, nil
17+
buf := make([]byte, 2)
18+
binary.BigEndian.PutUint16(buf, p.PeerID)
19+
return buf, nil
1820
}
1921

2022
func (p *ServerSetPeer) UnmarshalPacket(payload []byte) error {

0 commit comments

Comments
 (0)