示例#1
0
func (s *Server) serveBusLine(conn net.Conn) {
	log.I("Busline opened: ", conn.RemoteAddr())

	s.bridge.RunBusLine(conn)

	log.I("Busline closed: ", conn.RemoteAddr())
}
示例#2
0
func handleConn(conn net.Conn) {
	log.I("Bridge accept: ", conn.RemoteAddr())

	remote, err := net.Dial("tcp", "127.0.0.1:1080")
	if err != nil {
		log.W("Cannot dail remote ", err)
		conn.Close()
		return
	}

	go core.ConnCopy(conn, remote)
	core.ConnCopy(remote, conn)

	log.I("Bridge closed: ", conn.RemoteAddr())
}
示例#3
0
// TODO add chan to inform bridge closed
func (f *Flow) runFlowControl() {
	heap := heap.New()

	for {
		msg := <-f.recv
		heap.Push(msg)

		ack := f.ack
		for !heap.Empty() {
			msg := heap.Top().(*Message)
			if msg.seq != ack+1 {
				break
			}

			heap.Pop()
			ack = msg.seq

			ch, ok := f.bridge.BusChannel(msg.channel)
			if !ok {
				if msg.syn {
					// new channel to accept
					if f.bridge.canAccept {
						conn := f.bridge.NewChannel(msg.channel)
						select {
						case f.bridge.AcceptChan <- conn:
							log.I("new channel opened: ", msg.channel)
						default:
							conn.Close()
							log.I("new channel denied: ", msg.channel)
						}
						continue
					}
				}
				// channel not found, ignored
				log.I("ignore packet from unknown channel: ", msg.channel)
				continue
			}

			log.D("dispatch msg to channel ", msg.channel)
			trySend(ch, msg)
		}

		f.setAck(ack)
		// TODO set timeout to send extra ack packet
	}
}
示例#4
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")
	}
}
示例#5
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)
	}
}
示例#6
0
func handleConn(conn net.Conn) {
	remote, err := client.Dial()
	if err != nil {
		log.W("Cannot dail remote")
		conn.Close()
		return
	}

	go core.ConnCopy(conn, remote)
	core.ConnCopy(remote, conn)

	log.I("Close connection " + conn.RemoteAddr().String())
}
示例#7
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)
	}

}