func (self *Player) AsyncWrap(targetPlayerId string, fun func()) { if self.PlayerId == targetPlayerId { self.HandleAsyncWrap(fun) } else { gen_server.Cast(targetPlayerId, "HandleAsyncWrap", fun) } }
func (self *Player) PublishChannelMsg(channel, category string, data interface{}) { msg := &BroadcastMsg{ Category: category, Channel: channel, SenderId: self.PlayerId, Data: data, } gen_server.Cast(BROADCAST_SERVER_ID, "Publish", msg) }
func (self *Broadcast) handlePublish(msg *BroadcastMsg) { channel := msg.Channel if v, ok := self.channels[channel]; ok { for id, _ := range v { if _, ok := gen_server.GetGenServer(id); ok { gen_server.Cast(id, "broadcast", msg) } else { delete(self.channels[channel], id) } } } }
func handleRequest(conn net.Conn) { defer func() { if x := recover(); x != nil { ERR("caught panic in handleClient", x) } }() server_name := conn.RemoteAddr().String() gen_server.Start(server_name, new(Player), server_name) header := make([]byte, 2) for { // header conn.SetReadDeadline(time.Now().Add(TCP_TIMEOUT * time.Second)) n, err := io.ReadFull(conn, header) if err != nil { NOTICE("Connection Closed: ", err) break } // data size := binary.BigEndian.Uint16(header) data := make([]byte, size) n, err = io.ReadFull(conn, data) if err != nil { WARN("error receiving msg, bytes:", n, "reason:", err) break } gen_server.Cast(server_name, "HandleRequest", data, conn) } gen_server.Cast(server_name, "removeConn") }
func (self *Player) startPersistTimer() { self.persistTimer = time.AfterFunc(300*time.Second, func() { gen_server.Cast(self.PlayerId, "PersistData") }) }
func (self *Player) LeaveChannel(channel string) { gen_server.Cast(BROADCAST_SERVER_ID, "LeaveChannel", self.PlayerId, channel) }