Skip to content

Commit

Permalink
merge client_incoming.go into client.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Cooper committed Nov 21, 2014
1 parent 28b551c commit 17bcd3b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
43 changes: 42 additions & 1 deletion gumble/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package gumble

import (
"crypto/tls"
"encoding/binary"
"errors"
"io"
"net"
"runtime"
"sync"
Expand All @@ -25,6 +27,10 @@ const (
// to the server.
const pingInterval time.Duration = time.Second * 10

// maximumPacketLength is the maximum byte length of a packet that will be
// accepted from the server.
const maximumPacketLength = 1024 * 1024 * 10 // 10 megabytes

var (
ErrConnected = errors.New("client is already connected to a server")
)
Expand Down Expand Up @@ -78,7 +84,7 @@ func (c *Client) Connect() error {

// Channels and goroutines
c.end = make(chan bool)
go clientIncoming(c)
go c.readRoutine()
go c.pingRoutine()

// Initial packets
Expand Down Expand Up @@ -126,6 +132,41 @@ func (c *Client) pingRoutine() {
}
}

// readRoutine reads protocol buffer messages from the server.
func (c *Client) readRoutine() {
defer c.Close()

conn := c.connection
data := make([]byte, 1024)

for {
var pType uint16
var pLength uint32

if err := binary.Read(conn, binary.BigEndian, &pType); err != nil {
return
}
if err := binary.Read(conn, binary.BigEndian, &pLength); err != nil {
return
}
pLengthInt := int(pLength)
if pLengthInt > maximumPacketLength {
return
}
if pLengthInt > cap(data) {
data = make([]byte, pLengthInt)
}
if _, err := io.ReadFull(conn, data[:pLengthInt]); err != nil {
return
}
if handle, ok := handlers[pType]; ok {
if err := handle(c, data[:pLengthInt]); err != nil {
// TODO: log error?
}
}
}
}

// Close disconnects the client from the server.
func (c *Client) Close() error {
c.closeMutex.Lock()
Expand Down
43 changes: 0 additions & 43 deletions gumble/client_incoming.go

This file was deleted.

0 comments on commit 17bcd3b

Please sign in to comment.