Per-player Network Usage Stats

Discussion in 'Plugin Development' started by NVX, Sep 11, 2012.

Thread Status:
Not open for further replies.
  1. Offline


    Is there an API to retrieve the number of byes sent to and received from each player? From looking at the JavaDocs I can't see any way to get it. Is there a way to get it by referencing CraftBukkit directly perhaps? I'm looking at writing a little plugin to help keep a track of the servers bandwidth usage.

    Also, I've found an API that shows when the player first joined the server (org.bukkit.OfflinePlayer.getFirstPlayed), but is there one that shows the time a player has been online for? I could hook a Player Join event and log the time, but it doesn't seem very elegant, and probably will fail hard on a reload, especially if the plugin is only loaded for the first time on said reload.
  2. Is there an API? No.
    Is it possible? Yes.

    You need to dig deep into the server code, the NetServerHandler and NetworkManager in particular, I guess. It shouldn't be that hard to replace the last instance that actually outputs the data to the socket and then monitor that, if you know your way around reflection.

    Replacing native classes with custom instances that extend these also theoretically survives reloads, but from what I experienced that starts getting extremely messy due to your new active plugin being in a different ClassLoader context (or something like that).
    Probably also causes memory leaks, since the old classes and instances are still referenced, not sure.

    The reload functionality of bukkit thus becomes a big hassle for plugins like that to manage. You'll probably have to clean everything you did up in onDisable.

    Nice idea, actually, I would like such a plugin.
  3. Offline


    Unfortunately I'm not too good with reflection in Java - I actually hate the language, go figure :p.

    Editing the CraftBukkit source and compiling a custom build would make it very simple to add such a feature looking at those classes, but doing it dynamically at runtime with reflection would probably make me want to kill myself unless someone more familiar with doing that kind of overriding can pitch in.

    Give me asm hooks any day :p
  4. Offline


    Answering my own question here (and thanks Bone008 for the pointer to NetworkManager/NetServerManager) I noticed two counters, net.minecraft.server.NetworkManager.d and net.minecraft.server.NetworkManager.e that contain exactly what I'm after, except on a per-server basis (static variables) rather than a per-player basis (not being static variables).

    Might write up a little plugin that grabs it for the server-wide statistics, as I can still get which packets are causing the bandwidth usage, and later on perhaps look into adding a more specific counter to do per-player stuff (it's literally 2 lines of code, plus 2 lines for the variable declarations, it's just adding it in at runtime without editing the server jar that's a pita...).
    Bone008 likes this.
Thread Status:
Not open for further replies.

Share This Page