Esempio n. 1
0
func (self *binaryAcceptor) Start() {

	ln, err := net.Listen("tcp", self.Address)

	if err != nil {
		log.Printf("error listen %s", err.Error())
		return
	}

	// Accept线程
	go func() {

		for {
			conn, err := ln.Accept()

			if err != nil {
				continue
			}

			// 启动session线程
			go func() {
				ses := netdef.NewSession()
				ses.Codec = self.Codec
				ses.PStream = NewPacketStream(conn, self.GetSocketReadTimeout(), self.GetSocketWriteTimeout())

				self.addSession(ses)

				ses.OnInternalStop = func(e error) {

					ses.EventChan <- true
					self.removeSession(ses)

				}

				// 启动派发线程(逻辑线程)
				go self.DispatchLoop(ses)

				// 启动发送线程
				go self.SendPacketLoop(ses)

				// 心跳
				go self.SessionEventLoop(ses)

				// 启动接收线程
				go netfw.RecvPacketLoop(ses)

				// 投递接受的消息
				ses.PostEvent(&netdef.NetEvent{Method: netdef.EventAccepted, Ses: ses, Tag: self})
			}()

		}

	}()

	self.Ready = true

}
Esempio n. 2
0
func (self *binaryConnector) Start() {

	// 已经连接, 就不重复连接
	if self.Ready {
		return
	}

	if self.Session == nil {
		self.Session = netdef.NewSession()
		self.Session.Codec = self.Codec
	}

	// 防止线程阻塞
	go self.connect()

}