3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

fix closed channel on socket close

This commit is contained in:
Jeremy Latt 2014-02-18 09:45:10 -08:00
parent 9c1ab4f610
commit 58517ad4e8
4 changed files with 26 additions and 20 deletions

View File

@ -67,6 +67,8 @@ func (client *Client) readCommands() {
msg.SetClient(client) msg.SetClient(client)
client.server.commands <- msg client.server.commands <- msg
} }
client.server.toDestroy <- client
} }
func (client *Client) Touch() { func (client *Client) Touch() {
@ -117,9 +119,8 @@ func (client *Client) connectionClosed() {
func (client *Client) Destroy() { func (client *Client) Destroy() {
if DEBUG_CLIENT { if DEBUG_CLIENT {
log.Printf("%s destroy", client) log.Printf("%s: destroying", client)
} }
// clean up self // clean up self
client.socket.Close() client.socket.Close()
@ -143,7 +144,7 @@ func (client *Client) Destroy() {
client.server.clients.Remove(client) client.server.clients.Remove(client)
if DEBUG_CLIENT { if DEBUG_CLIENT {
log.Printf("%s destroyed", client) log.Printf("%s: destroyed", client)
} }
} }
@ -203,7 +204,7 @@ func (c *Client) Id() string {
} }
func (c *Client) String() string { func (c *Client) String() string {
return c.UserHost() return c.Id()
} }
func (client *Client) AddFriend(friend *Client) { func (client *Client) AddFriend(friend *Client) {
@ -240,5 +241,5 @@ func (client *Client) Quit(message string) {
} }
client.Reply(RplError(client.server, client)) client.Reply(RplError(client.server, client))
client.Destroy() client.socket.Close()
} }

View File

@ -460,14 +460,14 @@ func (changes ModeChanges) String() string {
type ModeCommand struct { type ModeCommand struct {
BaseCommand BaseCommand
nickname string nickname string
changes []ModeChange changes ModeChanges
} }
// MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) ) // MODE <nickname> *( ( "+" / "-" ) *( "i" / "w" / "o" / "O" / "r" ) )
func NewUserModeCommand(args []string) (editableCommand, error) { func NewUserModeCommand(args []string) (editableCommand, error) {
cmd := &ModeCommand{ cmd := &ModeCommand{
nickname: args[0], nickname: args[0],
changes: make([]ModeChange, 0), changes: make(ModeChanges, 0),
} }
for _, modeChange := range args[1:] { for _, modeChange := range args[1:] {

View File

@ -23,6 +23,7 @@ type Server struct {
name string name string
operators map[string]string operators map[string]string
password string password string
toDestroy chan *Client
} }
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
@ -36,6 +37,7 @@ func NewServer(config *Config) *Server {
name: config.Name, name: config.Name,
operators: make(map[string]string), operators: make(map[string]string),
password: config.Password, password: config.Password,
toDestroy: make(chan *Client),
} }
for _, opConf := range config.Operators { for _, opConf := range config.Operators {
@ -55,6 +57,9 @@ func (server *Server) ReceiveCommands() {
case conn := <-server.conns: case conn := <-server.conns:
NewClient(server, conn) NewClient(server, conn)
case client := <-server.toDestroy:
client.Destroy()
case cmd := <-server.commands: case cmd := <-server.commands:
client := cmd.Client() client := cmd.Client()
if DEBUG_SERVER { if DEBUG_SERVER {
@ -65,7 +70,7 @@ func (server *Server) ReceiveCommands() {
case Authorization: case Authorization:
authCmd, ok := cmd.(AuthServerCommand) authCmd, ok := cmd.(AuthServerCommand)
if !ok { if !ok {
client.Destroy() client.socket.Close()
continue continue
} }
authCmd.HandleAuthServer(server) authCmd.HandleAuthServer(server)
@ -73,7 +78,7 @@ func (server *Server) ReceiveCommands() {
case Registration: case Registration:
regCmd, ok := cmd.(RegServerCommand) regCmd, ok := cmd.(RegServerCommand)
if !ok { if !ok {
client.Destroy() client.socket.Close()
continue continue
} }
regCmd.HandleRegServer(server) regCmd.HandleRegServer(server)
@ -249,7 +254,7 @@ func (m *PassCommand) HandleAuthServer(s *Server) {
if s.password != m.password { if s.password != m.password {
client.Reply(ErrPasswdMismatch(s)) client.Reply(ErrPasswdMismatch(s))
client.Destroy() client.socket.Close()
return return
} }

View File

@ -13,7 +13,6 @@ const (
) )
type Socket struct { type Socket struct {
closed bool
conn net.Conn conn net.Conn
done chan bool done chan bool
reader *bufio.Reader reader *bufio.Reader
@ -43,13 +42,7 @@ func (socket *Socket) String() string {
} }
func (socket *Socket) Close() { func (socket *Socket) Close() {
if socket.closed {
return
}
socket.closed = true
socket.done <- true socket.done <- true
close(socket.done)
} }
func (socket *Socket) Read() <-chan string { func (socket *Socket) Read() <-chan string {
@ -82,6 +75,7 @@ func (socket *Socket) readLines() {
} }
close(socket.receive) close(socket.receive)
socket.Close()
if DEBUG_NET { if DEBUG_NET {
log.Printf("%s closed", socket) log.Printf("%s closed", socket)
} }
@ -114,10 +108,16 @@ func (socket *Socket) writeLines() {
if DEBUG_NET { if DEBUG_NET {
log.Printf("%s closing", socket) log.Printf("%s closing", socket)
} }
socket.conn.Close() if done {
socket.conn.Close()
}
for _ = range socket.send { // read incoming messages and discard to avoid hangs
// discard lines for {
select {
case <-socket.send:
case <-socket.done:
}
} }
} }