//---------------------------------------------------------- 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) } } }
func HandleRequest(hostid int32, reader *packet.Packet, output chan []byte) { defer helper.PrintPanicStack() seqid, err := reader.ReadU64() // read seqid if err != nil { log.Println("Read Sequence Id failed.", err) return } b, err := reader.ReadS16() if err != nil { log.Println("read protocol error") return } handle := ProtoHandler[b] if handle != nil { ret := handle(hostid, reader) if len(ret) != 0 { helper.SendChan(seqid, ret, output) } } }