Exemplo n.º 1
0
// new Transport object
func NewUdpConnection(newcid int, conn *net.UDPConn, endianer gts.IEndianer, userAddr *net.UDPAddr) *UdpConnection {
	c := &UdpConnection{
		ConnectionBuffer: &ConnectionBuffer{Stream: gts.NewRWStream(1024, endianer)},
		id:               10000 + newcid,
		conn:             conn,
		userAddr:         userAddr,
		udpStream:        gts.NewRWStream(1024, endianer),

		sendNo:           0,
		lastValidOrderNo: 1,
		lastOrderNo:      1,
		sendCache:        gts.NewMap(),
		recvCache:        gts.NewMap(),
		reqCache:         gts.NewMap(),

		//outgoing:       make(chan *RoutePacket, 5),
		//outgoingBytes:  make(chan []byte, 20),
		outFrame:    make(chan *UdpFrame, 20),
		receiveChan: make(chan bool, 1),

		closeded:     false,
		quitInterval: make(chan bool),
		quitSender:   make(chan bool),
		quitConnect:  make(chan bool),
		protocolType: EPROTOCOL_TYPE_UDP,
	}

	go c.interval()

	//创建go的线程 使用Goroutine
	go c.reader()

	if conn != nil {
		go c.ConnSenderServer()
	}

	return c
}
Exemplo n.º 2
0
// new Transport object
func NewTcpConnection(newcid int, conn net.Conn, endianer gts.IEndianer) *TcpConnection {
	c := &TcpConnection{
		ConnectionBuffer: &ConnectionBuffer{Stream: gts.NewRWStream(1024, endianer)},
		id:               newcid,
		conn:             conn,
		read_buffer_size: 1024,

		outgoing:      make(chan *RoutePacket, 1),
		outgoingBytes: make(chan []byte),

		closeded:     false,
		quit:         make(chan bool),
		Quit:         make(chan bool),
		protocolType: EPROTOCOL_TYPE_TCP,
	}

	if c.conn != nil {
		//创建go的线程 使用Goroutine
		go c.ConnSender()
		go c.ConnReader()
	}

	return c
}