示例#1
0
//---------------------------------------------------------- Hub processing
func HubAgent(incoming chan []byte, conn net.Conn) {
	config := cfg.Get()
	if config["profile"] == "true" {
		helper.SetMemProfileRate(1)
		defer func() {
			helper.GC()
			helper.DumpHeap()
			helper.PrintGCSummary()
		}()
	}

	hostid := atomic.AddInt32(&_host_genid, 1)
	// forward buffer
	forward := make(chan []byte, MAXCHAN)
	// output buffer
	output := make(chan []byte, MAXCHAN)

	protos.AddServer(hostid, forward)
	log.Printf("game server [id:%v] connected\n", hostid)

	go _write_routine(output, conn)

	defer func() {
		protos.RemoveServer(hostid)
		core.LogoutServer(hostid)
		close(forward)
		close(output)

		log.Printf("game server [id:%v] disconnected\n", hostid)
	}()

	for {
		select {
		case msg, ok := <-incoming: // from hub
			if !ok {
				return
			}

			reader := packet.Reader(msg)
			protos.HandleRequest(hostid, reader, output)
		case msg := <-forward: // send forward packet
			helper.SendChan(0, msg, output)
		}
	}

}
示例#2
0
文件: agent.go 项目: chenbk85/gonet
//---------------------------------------------------------- HUB SERVICE
func StartAgent(incoming chan []byte, conn net.Conn) {
	hostid := atomic.AddInt32(&_host_genid, 1)
	log.Printf("game server [id:%v] connected\n", hostid)

	// forward queue (IPCObject)
	forward := make(chan IPCObject, 100000)
	protos.AddServer(hostid, forward)

	// closing
	defer func() {
		protos.RemoveServer(hostid)
		core.LogoutServer(hostid)
		close(forward)

		log.Printf("game server [id:%v] disconnected\n", hostid)
	}()

	for {
		select {
		case msg, ok := <-incoming: // request from game server
			if !ok {
				return
			}

			// read seqid
			reader := packet.Reader(msg)
			seqid, err := reader.ReadU32()
			if err != nil {
				log.Println("read SEQID failed.", err)
				return
			}

			// handle request
			ret := GSProxy(hostid, reader)
			// send result
			if len(ret) != 0 {
				_send(seqid, ret, conn)
			}
		case obj := <-forward: // forwarding packets(ie. seqid == 0)
			_send(0, obj.Json(), conn)
		}
	}
}