Exemplo n.º 1
0
func (s *Server) runBusLine(listener net.Listener) error {
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.E("Busline accept failed: ", err)
			continue
		}

		go s.serveBusLine(conn)
	}
}
Exemplo n.º 2
0
func (c *Client) genNewChannelId() uint16 {
	select {
	case id := <-c.freeChannelId:
		return id
	default:
		id := atomic.AddUint32(&c.maxChannelId, 1)
		if id > math.MaxUint16 {
			log.E("Too many channels! ", id, " > ", math.MaxUint16)
		}
		return uint16(id)
	}
}
Exemplo n.º 3
0
func (c *Client) runBusLine(addr Addr) {
	// TODO use config param
	for {
		conn, err := net.Dial("tcp", addr.String())
		if err != nil {
			log.E("Busline failed: ", err)
			time.Sleep(time.Second * 2)
			continue
		}

		log.I("Busline established: ", addr)

		c.bridge.RunBusLine(conn)

		log.I("Busline closed: ", addr, " , reconnect")
	}
}
Exemplo n.º 4
0
func run() {
	err := server.Listen(bridge.Addr{"127.0.0.1", 8000})
	if err != nil {
		log.F("Bridge listen: ", err)
	}

	log.I("Start listening")

	for {
		conn, err := server.Accept()
		if err != nil {
			log.E("Bridge accept: ", err)
			continue
		}

		go handleConn(conn)
	}

}
Exemplo n.º 5
0
func (c *Channel) Read(buf []byte) (int, error) {
	if c.Closed() {
		return 0, io.ErrClosedPipe
	}

	msg := <-c.recv
	if (msg == nil) || msg.fin {
		c.Close()
		return 0, io.ErrClosedPipe
	}
	if len(msg.data) > len(buf) {
		log.E("Recevive message which size is bigger than excepted!")
		c.Close()
		return 0, errors.New("unexcepted message")
	}
	copy(buf, msg.data)

	return len(msg.data), nil
}
Exemplo n.º 6
0
func run(listenAddr string) {
	listener, err := net.Listen("tcp", listenAddr)
	if err != nil {
		log.F(err)
	}

	log.I("Start listening at " + listenAddr)

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.E("accept: ", err)
			continue
		}

		log.I("Accept connection " + conn.RemoteAddr().String())
		go handleConn(conn)
	}
}