1
0
Fork 0
forked from External/ergo

improve performance by using less goroutines

- get rid of command channel in Client
- get rid of loginTimer; use other timers instead
- move debugging code to debug.go
This commit is contained in:
Jeremy Latt 2014-04-15 08:49:52 -07:00
parent cab21782b4
commit 6c10add399
6 changed files with 153 additions and 108 deletions

View file

@ -7,25 +7,22 @@ import (
)
const (
R = '→'
W = '←'
EOF = ""
R = '→'
W = '←'
)
type Socket struct {
conn net.Conn
writer *bufio.Writer
conn net.Conn
scanner *bufio.Scanner
writer *bufio.Writer
}
func NewSocket(conn net.Conn, commands chan<- Command) *Socket {
socket := &Socket{
conn: conn,
writer: bufio.NewWriter(conn),
func NewSocket(conn net.Conn) *Socket {
return &Socket{
conn: conn,
scanner: bufio.NewScanner(conn),
writer: bufio.NewWriter(conn),
}
go socket.readLines(commands)
return socket
}
func (socket *Socket) String() string {
@ -37,32 +34,22 @@ func (socket *Socket) Close() {
Log.debug.Printf("%s closed", socket)
}
func (socket *Socket) readLines(commands chan<- Command) {
commands <- NewProxyCommand(AddrLookupHostname(socket.conn.RemoteAddr()))
scanner := bufio.NewScanner(socket.conn)
for scanner.Scan() {
line := scanner.Text()
func (socket *Socket) Read() (line string, err error) {
for socket.scanner.Scan() {
line = socket.scanner.Text()
if len(line) == 0 {
continue
}
Log.debug.Printf("%s → %s", socket, line)
msg, err := ParseCommand(line)
if err != nil {
// TODO error messaging to client
continue
}
commands <- msg
return
}
if err := scanner.Err(); err != nil {
Log.debug.Printf("%s error: %s", socket, err)
err = socket.scanner.Err()
socket.isError(err, R)
if err == nil {
err = io.EOF
}
commands <- NewQuitCommand("connection closed")
close(commands)
return
}
func (socket *Socket) Write(line string) (err error) {