예제 #1
0
func (bridge *Bridge) RunBusLineRecv(conn net.Conn, connClosed chan int) {
	buf := leakybuf.Get()
	defer leakybuf.Put(buf)

	for {
		err := ReadAll(conn, buf[:MsgeaderLen])
		if err != nil {
			log.D("BuslineRecv: ", err)
			break
		}
		msg, dataLen := ParseMessageHeader(buf)
		msg.flow = bridge.flow
		msg.data = make([]byte, dataLen)
		err = ReadAll(conn, msg.data)
		if err != nil {
			log.D("BuslineRecv: ", err)
			break
		}

		msg.flow.RecvMsg(msg)
	}

	connClosed <- 1
	conn.Close()
}
예제 #2
0
func (bridge *Bridge) RunBusLineSend(conn net.Conn, connClosed chan int) {
	buf := leakybuf.Get()
	defer leakybuf.Put(buf)
	var msg *Message = nil

LOOP:
	for {
		select {
		case msg = <-bridge.highPrioSendBus:
		case msg = <-bridge.SendBus:
		case <-connClosed:
			break LOOP
		}

		msg.ack = msg.flow.Ack()
		msg.PacketHeader(buf)
		log.D("msg > seq:", msg.seq, " ack:", msg.ack,
			" channel:", msg.channel, " syn:", msg.syn, " fin:", msg.fin)
		log.D("msg > ", string(msg.data))
		err := WriteAll(conn, buf[:MsgeaderLen])
		if err != nil {
			log.D("BuslineSend: ", err)
			break
		}
		err = WriteAll(conn, msg.data)
		if err != nil {
			log.D("BuslineSend: ", err)
			break
		}

		msg = nil
	}

	conn.Close()
	if msg != nil {
		// resend the message
		bridge.highPrioSendBus <- msg
	}
}
예제 #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 (f *Flow) RecvMsg(msg *Message) {
	log.D("msg < seq:", msg.seq, " ack:", msg.ack,
		" channel:", msg.channel, " syn:", msg.syn, " fin:", msg.fin)
	log.D("msg < ", string(msg.data))
	f.recv <- msg
}