gempir

broker

Mar 20th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.79 KB | None | 0 0
  1. // ControlConn simple tcp server for commands
  2. func ControlConn() {
  3.     ln, err := net.Listen("tcp", ":"+TCPPort)
  4.     if err != nil {
  5.         log.Println("[control] error listening:", err.Error())
  6.     }
  7.     log.Printf("[control] listening to port %s for connections...", TCPPort)
  8.  
  9.     for {
  10.         conn, err := ln.Accept()
  11.         bot := NewBot()
  12.         bot.inconn = conn
  13.         if err != nil {
  14.             fmt.Println("[control] Error accepting: ", err.Error())
  15.         }
  16.         handleRequest(conn, bot)
  17.     }
  18. }
  19.  
  20. func handleRequest(conn net.Conn, bot *Bot) {
  21.     defer conn.Close()
  22.  
  23.     reader := bufio.NewReader(conn)
  24.     tp := textproto.NewReader(reader)
  25.     for {
  26.         line, err := tp.ReadLine()
  27.  
  28.         if err != nil {
  29.             fmt.Println("[control] read error:", err)
  30.             conn.Close()
  31.             return
  32.         }
  33.         log.Println("[control] " + line)
  34.         handleMessage(line, bot)
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment