Skip to content

Commit

Permalink
Make debug packet a callback for client
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Nov 26, 2023
1 parent 1eafacf commit 564c121
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion cmd/soupbintcp-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ func ReceivePacket(packet []byte) {
log.Printf("Received packet: %v\n", packet)
}

func DebugPacket(text string) {
log.Printf("[DEBUG] %s\n", text)
}

func main() {
client := soupbintcp.NewClient(
soupbintcp.WithServer("127.0.0.1", "1337"),
soupbintcp.WithServer("127.0.0.1", "4000"),
soupbintcp.WithAuth("test", "test"),
soupbintcp.WithCallback(ReceivePacket),
soupbintcp.WithDebugCallback(DebugPacket),
)

if err := client.Login(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soupbintcp-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
soupbintcp.WithPacketCallback(ReceivePacket),

// Clients can send debug packets. Not normally used.
soupbintcp.WithDebugCallback(DebugPacket),
soupbintcp.WithServerDebugCallback(DebugPacket),
)

sessionId := "ABCDEFGHIJ"
Expand Down
21 changes: 16 additions & 5 deletions soupbintcp/4.1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ type Client struct {
// UnsequencedCallback is called for every unsequenced packet received. The byte slice parameter contains just the message data and should be further
// parsed as some higher level protocol
UnsequencedCallback func([]byte)
ServerIp string
ServerPort string
Username string
Password string
// DebugCallback is called for every debug packet received. This is not normally used
DebugCallBack func(string)

ServerIp string
ServerPort string
Username string
Password string

conn net.Conn
sequenceNumber uint64
Expand Down Expand Up @@ -62,6 +65,12 @@ func WithUnsequencedCallback(callback func([]byte)) ClientOption {
}
}

func WithDebugCallback(callback func(string)) ClientOption {
return func(c *Client) {
c.DebugCallBack = callback
}
}

func WithServer(ip, port string) ClientOption {
return func(c *Client) {
c.ServerIp = ip
Expand Down Expand Up @@ -201,7 +210,9 @@ func (c *Client) Receive() {

switch packet[2] {
case PacketTypeDebug:
log.Printf("[DEBUG PACKET] %s", packet[3:])
if c.DebugCallBack != nil {
c.DebugCallBack(string(packet[3:]))
}
case PacketTypeServerHeartbeat:
log.Print("received heartbeat packet")
case PacketTypeEndOfSession:
Expand Down
2 changes: 1 addition & 1 deletion soupbintcp/4.1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func WithPacketCallback(callback func([]byte)) ServerOption {
}
}

func WithDebugCallback(callback func([]byte)) ServerOption {
func WithServerDebugCallback(callback func([]byte)) ServerOption {
return func(s *Server) {
s.DebugCallback = callback
}
Expand Down

0 comments on commit 564c121

Please sign in to comment.