LastMUD/internal/server/connection.go

76 lines
1.1 KiB
Go
Raw Normal View History

2025-06-19 16:22:55 +03:00
package server
import (
"bufio"
"fmt"
"net"
"time"
"github.com/google/uuid"
)
type Connection struct {
identity uuid.UUID
conn net.Conn
inputChannel chan string
closeChannel chan struct{}
}
func CreateConnection(conn net.Conn) *Connection {
return &Connection{
identity: uuid.New(),
conn: conn,
inputChannel: make(chan string),
closeChannel: make(chan struct{}),
}
}
func (c *Connection) listen() (err error) {
defer c.conn.Close()
c.conn.SetReadDeadline(time.Time{})
for {
if c.shouldClose() {
break
}
message, err := bufio.NewReader(c.conn).ReadString('\n')
if err != nil {
fmt.Println(err)
return err
}
c.inputChannel <- message
c.conn.Write([]byte(message))
}
return
}
func (c *Connection) shouldClose() bool {
select {
case <-c.closeChannel:
return true
default:
return false
}
}
func (c *Connection) Close() {
c.closeChannel <- struct{}{}
}
func (c *Connection) NextInput() (next string, err error) {
select {
case val := <-c.inputChannel:
return val, nil
default:
return "", newInputEmptyError()
}
}