client: Split out event loop handling to let people handle it themselves

This commit is contained in:
Daniel Oaks 2016-02-10 23:14:02 +10:00
parent 1cb8e4762b
commit 810734eaaf
3 changed files with 23 additions and 6 deletions

@ -6,10 +6,12 @@ package gircclient
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/DanielOaks/girc-go/eventmgr"
"github.com/DanielOaks/girc-go/ircmsg"
@ -39,9 +41,10 @@ type ServerConnection struct {
// newServerConnection returns an initialised ServerConnection, for internal
// use.
func newServerConnection() *ServerConnection {
func newServerConnection(name string) *ServerConnection {
var sc ServerConnection
sc.Name = name
sc.Caps = NewClientCapabilities()
sc.Caps.AddWantedCaps("account-notify", "away-notify", "extended-join", "multi-prefix", "sasl")
@ -52,6 +55,12 @@ func newServerConnection() *ServerConnection {
// Connect connects to the given address.
func (sc *ServerConnection) Connect(address string, ssl bool, tlsconfig *tls.Config) error {
// check the required attributes
if sc.InitialNick == "" || sc.InitialUser == "" {
return errors.New("InitialNick and InitialUser must be set before connecting")
}
// connect
var conn net.Conn
var err error
@ -70,13 +79,17 @@ func (sc *ServerConnection) Connect(address string, ssl bool, tlsconfig *tls.Con
sc.Send(nil, "", "CAP", "LS", "302")
go sc.receiveLoop()
return nil
}
// receiveLoop runs a loop of receiving and dispatching new messages.
func (sc *ServerConnection) receiveLoop() {
// ReceiveLoop runs a loop of receiving and dispatching new messages.
func (sc *ServerConnection) ReceiveLoop() {
// wait for the connection to become available
for sc.connection == nil {
waitTime, _ := time.ParseDuration("10ms")
time.Sleep(waitTime)
}
reader := bufio.NewReader(sc.connection)
for {
@ -150,6 +163,7 @@ func (sc *ServerConnection) Send(tags *map[string]ircmsg.TagValue, prefix string
// dispatch raw event
info := eventmgr.NewInfoMap()
info["server"] = sc
info["direction"] = "out"
info["data"] = line
sc.dispatchRawOut(info)

@ -36,7 +36,7 @@ func NewReactor() Reactor {
// CreateServer creates a ServerConnection and returns it.
func (r *Reactor) CreateServer(name string) *ServerConnection {
sc := newServerConnection()
sc := newServerConnection(name)
r.ServerConnections[name] = sc

@ -31,6 +31,7 @@ func TestPlainConnection(t *testing.T) {
listener, _ := net.Listen("tcp", ":0")
client.Connect(listener.Addr().String(), false, nil)
go client.ReceiveLoop()
testServerConnection(t, reactor, client, listener)
}
@ -93,6 +94,7 @@ func TestTLSConnection(t *testing.T) {
clientTLSConfig.RootCAs = clientTLSCertPool
clientTLSConfig.ServerName = "localhost"
go client.Connect(listener.Addr().String(), true, &clientTLSConfig)
go client.ReceiveLoop()
testServerConnection(t, reactor, client, listener)
}
@ -112,6 +114,7 @@ func sendMessage(conn net.Conn, tags *map[string]ircmsg.TagValue, prefix string,
}
func testServerConnection(t *testing.T, reactor Reactor, client *ServerConnection, listener net.Listener) {
// start our reader
conn, _ := listener.Accept()
reader := bufio.NewReader(conn)