Пример #1
0
func (self *Player) AsyncWrap(targetPlayerId string, fun func()) {
	if self.PlayerId == targetPlayerId {
		self.HandleAsyncWrap(fun)
	} else {
		gen_server.Cast(targetPlayerId, "HandleAsyncWrap", fun)
	}
}
Пример #2
0
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)
}
Пример #3
0
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)
			}
		}
	}
}
Пример #4
0
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")

}
Пример #5
0
func (self *Player) startPersistTimer() {
	self.persistTimer = time.AfterFunc(300*time.Second, func() {
		gen_server.Cast(self.PlayerId, "PersistData")
	})
}
Пример #6
0
func (self *Player) LeaveChannel(channel string) {
	gen_server.Cast(BROADCAST_SERVER_ID, "LeaveChannel", self.PlayerId, channel)
}