Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.88 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "log"
  7.     "net"
  8. )
  9.  
  10. type (
  11.     ServerRoom struct {
  12.         clientIdIterator int
  13.         messages         chan string
  14.         clients          map[*Client]bool
  15.         newConnections   chan *Client
  16.         deadConnections  chan *Client
  17.     }
  18.  
  19.     Client struct {
  20.         id     int
  21.         conn   net.Conn
  22.         reader *bufio.Reader
  23.         writer *bufio.Writer
  24.     }
  25. )
  26.  
  27. func main() {
  28.     listener, err := net.Listen("tcp", ":1337")
  29.  
  30.     if err != nil {
  31.         fmt.Println("Some error occured:", err.Error())
  32.     }
  33.  
  34.     defer listener.Close()
  35.  
  36.     room := &ServerRoom{
  37.         0,
  38.         make(chan string),
  39.         make(map[*Client]bool),
  40.         make(chan *Client),
  41.         make(chan *Client),
  42.     }
  43.  
  44.     go func() {
  45.         for {
  46.             conn, err := listener.Accept()
  47.  
  48.             if err != nil {
  49.                 fmt.Println("Some error occured:", err.Error())
  50.             }
  51.  
  52.             reader := bufio.NewReader(conn)
  53.             writer := bufio.NewWriter(conn)
  54.  
  55.             client := &Client{
  56.                 id:     room.clientIdIterator,
  57.                 conn:   conn,
  58.                 reader: reader,
  59.                 writer: writer,
  60.             }
  61.  
  62.             room.clients[client] = false
  63.  
  64.             room.clientIdIterator++
  65.             room.newConnections <- client
  66.         }
  67.     }()
  68.  
  69.     for {
  70.         select {
  71.         case client := <-room.newConnections:
  72.             log.Printf("Accepted new client, #%d", client.id)
  73.  
  74.             go func() {
  75.                 for {
  76.                     incoming, err := client.reader.ReadString('\n')
  77.  
  78.                     if err != nil {
  79.                         break
  80.                     }
  81.  
  82.                     room.messages <- fmt.Sprintf("Client %d > %s", client.id, incoming)
  83.                 }
  84.             }()
  85.         case message := <-room.messages:
  86.             for client := range room.clients {
  87.                 go func() {
  88.                     _, err := client.writer.WriteString(message)
  89.  
  90.                     if err != nil {
  91.                         room.deadConnections <- client
  92.                     }
  93.  
  94.                     err = client.writer.Flush()
  95.  
  96.                     if err != nil {
  97.                         room.deadConnections <- client
  98.                     }
  99.                 }()
  100.             }
  101.         case client := <-room.deadConnections:
  102.             log.Printf("Client %d disconnected", client.id)
  103.             delete(room.clients, client)
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement