Example #1
0
func StartClient(server string, port int) {
	fmt.Println("Launching Brain Client...")
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
	utils.ProcError(err)
	chReceive := make(chan string)
	chSend := make(chan string)
	errCh := make(chan error)
	// shellInvite := ">"
	// read data goroutine
	go utils.ReadData(bufio.NewReader(conn), chReceive, errCh)
	go utils.ReadData(bufio.NewReader(os.Stdin), chSend, errCh)
	ticker := time.Tick(time.Second)
	for {
		select {
		case data := <-chReceive:
			fmt.Println(data)
		case data := <-chSend:
			// make sure plain '\n' can be sent
			fmt.Fprintf(conn, data)
		case err := <-errCh:
			utils.ProcError(err)
		case <-ticker:
		}
	}
}
Example #2
0
func NewServer(host string, port int, stateCh chan string) *Server {
	ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
	utils.ProcError(err)
	// use stoppable listener further on
	sl, err := listener.New(ln)
	utils.ProcError(err)
	s := &Server{make([]*Game, 0), sl, stateCh, &sync.WaitGroup{}}
	return s
}
Example #3
0
func (s *Server) Start() {
	game := s.addGame()
	game.SystemMsg("Launching Brain Server...", true)
	for {
		conn, err := s.listener.Accept()
		if err == listener.StoppedError {
			game.SystemMsg("Server shutdown", true)
			return
		} else {
			utils.ProcError(err)
		}
		game.joins <- conn
	}
	s.Stop()
}
Example #4
0
func (client *Client) Read() {
	game := client.Game
	for {
		line, err := client.reader.ReadString(settings.EOL)
		oe, ok := err.(*net.OpError)
		if err == io.EOF || ok && oe.Err == syscall.ECONNRESET {
			game.SystemMsg(
				fmt.Sprintf("Client %s disconnected", client.conn.RemoteAddr()), true)
			client.Exit()
			return
		} else if err != nil && client.disconnected {
			// XXX FIXME this read should not occur at all!!!
			game.SystemMsg("WARN: reading from a disconnected client", false)
			return
		}
		utils.ProcError(err)
		client.incoming <- line
	}
}
Example #5
0
func connect() (net.Conn, string) {
	conn, err := net.Dial("tcp", "127.0.0.1:9999")
	utils.ProcError(err)
	return conn, waitForData("(broadcast)")
}