Allow Server to have access to upstreamConn
This commit is contained in:
parent
8bbba42aef
commit
36c404c50c
@ -39,10 +39,10 @@ func (err ircError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type downstreamConn struct {
|
type downstreamConn struct {
|
||||||
net net.Conn
|
net net.Conn
|
||||||
irc *irc.Conn
|
irc *irc.Conn
|
||||||
srv *Server
|
srv *Server
|
||||||
logger Logger
|
logger Logger
|
||||||
|
|
||||||
registered bool
|
registered bool
|
||||||
closed bool
|
closed bool
|
||||||
@ -53,9 +53,9 @@ type downstreamConn struct {
|
|||||||
|
|
||||||
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
|
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
|
||||||
return &downstreamConn{
|
return &downstreamConn{
|
||||||
net: netConn,
|
net: netConn,
|
||||||
irc: irc.NewConn(netConn),
|
irc: irc.NewConn(netConn),
|
||||||
srv: srv,
|
srv: srv,
|
||||||
logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())},
|
logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
server.go
11
server.go
@ -54,8 +54,13 @@ func (s *Server) Run() {
|
|||||||
upstream := &s.Upstreams[i]
|
upstream := &s.Upstreams[i]
|
||||||
// TODO: retry connecting
|
// TODO: retry connecting
|
||||||
go func() {
|
go func() {
|
||||||
if err := connect(s, upstream); err != nil {
|
conn, err := connectToUpstream(s, upstream)
|
||||||
s.Logger.Printf("Failed to connect to upstream server %q: %v", upstream.Addr, err)
|
if err != nil {
|
||||||
|
s.Logger.Printf("failed to connect to upstream server %q: %v", upstream.Addr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := conn.readMessages(); err != nil {
|
||||||
|
conn.logger.Printf("failed to handle messages: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -72,7 +77,7 @@ func (s *Server) Serve(ln net.Listener) error {
|
|||||||
s.downstreamConns = append(s.downstreamConns, conn)
|
s.downstreamConns = append(s.downstreamConns, conn)
|
||||||
go func() {
|
go func() {
|
||||||
if err := conn.readMessages(); err != nil {
|
if err := conn.readMessages(); err != nil {
|
||||||
conn.logger.Printf("Error handling messages: %v", err)
|
conn.logger.Printf("failed to handle messages: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
47
upstream.go
47
upstream.go
@ -175,28 +175,12 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect(s *Server, upstream *Upstream) error {
|
func (c *upstreamConn) readMessages() error {
|
||||||
logger := &prefixLogger{s.Logger, fmt.Sprintf("upstream %q: ", upstream.Addr)}
|
defer c.net.Close()
|
||||||
logger.Printf("connecting to server")
|
|
||||||
|
|
||||||
netConn, err := tls.Dial("tcp", upstream.Addr, nil)
|
err := c.irc.WriteMessage(&irc.Message{
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c := upstreamConn{
|
|
||||||
upstream: upstream,
|
|
||||||
logger: logger,
|
|
||||||
net: netConn,
|
|
||||||
irc: irc.NewConn(netConn),
|
|
||||||
srv: s,
|
|
||||||
channels: make(map[string]*upstreamChannel),
|
|
||||||
}
|
|
||||||
defer netConn.Close()
|
|
||||||
|
|
||||||
err = c.irc.WriteMessage(&irc.Message{
|
|
||||||
Command: "NICK",
|
Command: "NICK",
|
||||||
Params: []string{upstream.Nick},
|
Params: []string{c.upstream.Nick},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -204,7 +188,7 @@ func connect(s *Server, upstream *Upstream) error {
|
|||||||
|
|
||||||
err = c.irc.WriteMessage(&irc.Message{
|
err = c.irc.WriteMessage(&irc.Message{
|
||||||
Command: "USER",
|
Command: "USER",
|
||||||
Params: []string{upstream.Username, "0", "*", upstream.Realname},
|
Params: []string{c.upstream.Username, "0", "*", c.upstream.Realname},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -223,5 +207,24 @@ func connect(s *Server, upstream *Upstream) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return netConn.Close()
|
return c.net.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectToUpstream(s *Server, upstream *Upstream) (*upstreamConn, error) {
|
||||||
|
logger := &prefixLogger{s.Logger, fmt.Sprintf("upstream %q: ", upstream.Addr)}
|
||||||
|
logger.Printf("connecting to server")
|
||||||
|
|
||||||
|
netConn, err := tls.Dial("tcp", upstream.Addr, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to dial %q: %v", upstream.Addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &upstreamConn{
|
||||||
|
upstream: upstream,
|
||||||
|
logger: logger,
|
||||||
|
net: netConn,
|
||||||
|
irc: irc.NewConn(netConn),
|
||||||
|
srv: s,
|
||||||
|
channels: make(map[string]*upstreamChannel),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user