2014-02-14 04:37:16 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2014-02-14 08:16:07 +01:00
|
|
|
"sync"
|
2014-02-14 04:37:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Socket struct {
|
|
|
|
closed bool
|
|
|
|
conn net.Conn
|
2014-02-14 08:16:07 +01:00
|
|
|
mutex *sync.Mutex
|
2014-02-14 04:37:16 +01:00
|
|
|
reader *bufio.Reader
|
|
|
|
receive chan string
|
2014-02-14 08:16:07 +01:00
|
|
|
send chan string
|
|
|
|
writer *bufio.Writer
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSocket(conn net.Conn) *Socket {
|
|
|
|
socket := &Socket{
|
|
|
|
conn: conn,
|
|
|
|
reader: bufio.NewReader(conn),
|
|
|
|
receive: make(chan string),
|
|
|
|
send: make(chan string),
|
2014-02-14 08:16:07 +01:00
|
|
|
mutex: &sync.Mutex{},
|
2014-02-14 04:37:16 +01:00
|
|
|
writer: bufio.NewWriter(conn),
|
|
|
|
}
|
|
|
|
|
|
|
|
go socket.readLines()
|
|
|
|
go socket.writeLines()
|
|
|
|
|
|
|
|
return socket
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) String() string {
|
|
|
|
return socket.conn.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
|
2014-02-14 08:16:07 +01:00
|
|
|
func (socket *Socket) IsClosed() bool {
|
|
|
|
socket.mutex.Lock()
|
|
|
|
defer socket.mutex.Unlock()
|
|
|
|
return socket.closed
|
|
|
|
}
|
|
|
|
|
2014-02-14 04:37:16 +01:00
|
|
|
func (socket *Socket) Close() {
|
2014-02-14 08:16:07 +01:00
|
|
|
if socket.IsClosed() {
|
2014-02-14 04:37:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s closed", socket)
|
|
|
|
}
|
|
|
|
|
2014-02-14 08:16:07 +01:00
|
|
|
socket.mutex.Lock()
|
2014-02-14 04:37:16 +01:00
|
|
|
socket.closed = true
|
|
|
|
socket.conn.Close()
|
|
|
|
close(socket.receive)
|
2014-02-14 08:16:07 +01:00
|
|
|
socket.mutex.Unlock()
|
2014-02-14 04:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) Read() <-chan string {
|
|
|
|
return socket.receive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) Write(lines []string) error {
|
|
|
|
for _, line := range lines {
|
2014-02-14 08:16:07 +01:00
|
|
|
if socket.IsClosed() {
|
2014-02-14 04:37:16 +01:00
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
socket.send <- line
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) readLines() {
|
2014-02-14 05:38:22 +01:00
|
|
|
for {
|
2014-02-14 04:37:16 +01:00
|
|
|
line, err := socket.reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s → error: %s", socket, err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s → %s", socket, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.receive <- line
|
|
|
|
}
|
|
|
|
socket.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) writeLines() {
|
|
|
|
for line := range socket.send {
|
2014-02-14 04:49:31 +01:00
|
|
|
if DEBUG_NET {
|
2014-02-14 04:37:16 +01:00
|
|
|
log.Printf("%s ← %s", socket, line)
|
|
|
|
}
|
|
|
|
if _, err := socket.writer.WriteString(line); socket.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _, err := socket.writer.WriteString(CRLF); socket.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := socket.writer.Flush(); socket.maybeLogWriteError(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (socket *Socket) maybeLogWriteError(err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
if DEBUG_NET {
|
|
|
|
log.Printf("%s ← error: %s", socket, err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|