Ejemplo n.º 1
0
func main() {
	log.Infoln("Starting the server.")

	Initialize()

	listener, err := net.Listen("tcp", config.GameServerPort)
	checkError(err)

	log.Infoln("Game Server OK.")

	for {
		conn, err := listener.Accept()
		if err == nil {
			go handleClient(conn)
		}
	}
}
Ejemplo n.º 2
0
func main() {
	ln, err := net.Listen("tcp", config.LoginServerPort)
	if err != nil {
		panic(err)
	}
	log.Infoln("loginserver started")
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Errorln("accept err:", err)
			continue
		}

		log.Infoln("receive a connect request")
		go serve(conn)
	}
}
Ejemplo n.º 3
0
func InitPlayer(player *Player, conn net.Conn) {
	player.PlayerStatus = GPS_BEGIN_SESSION
	player.conn = conn

	read := make(chan packet.Packet, 1)
	write := make(chan packet.Packet, 1)
	player.send = write
	player.client = read

	go func() {
		reader := packet.NewReader()
		player.packetReader = reader
		for {
			data, err := reader.Read(player.conn)
			if err != nil {
				if _, ok := err.(packet.NotImplementError); ok {
					log.Errorln("读到一个未实现的包:", data.PacketID())
				} else {
					if err == io.EOF {
						log.Infoln("后台gouroutine读客户端失败了:", err)
						player.conn.Close()
						// 关闭读channel会使得agent的goroutine退出,回收资源
						close(read)
						return
					} else {
						log.Errorln("这是一个严重的错误:", err)
						return
					}
				}
			}
			log.Debugln("读到了一个packet:", data)
			read <- data
		}
	}()

	go func() {
		writer := packet.NewWriter()
		player.packetWriter = writer
		for {
			pkt, ok := <-write
			if !ok {
				// 关闭使读goroutine退出
				player.conn.Close()
				return
			}
			log.Debugf("write channel get a pkt: %#v\n", pkt)
			err := writer.Write(player.conn, pkt)
			if err != nil {
				log.Errorln(err)
				continue
			}
		}
	}()
}