Class Connection

  • Direct Known Subclasses:
    NettyConnection

    public abstract class Connection
    extends java.lang.Object
    A connection is created when the first request from client reach and pass in your server. The connection has some types based on its client's request type (TCP, UDP, WebSocket). It can work stand-alone for some cases by but most of the time, it is a part of a particular player. You can send a message from the server-side to one client via its corresponding connection/player. Once the connection is established, it should be attached to a player or be refused (be disconnected) as soon as possible.
    Author:
    kong
    • Method Detail

      • isType

        public boolean isType​(ConnectionType type)
        Determine if the current type of connection is matched or not?
        Parameters:
        type - the comparison type
        Returns:
        true is the current type is matched, false otherwise.
      • getIndex

        public int getIndex()
        Returns:
        the current index of connection in one player
      • getUsername

        public java.lang.String getUsername()
        Retrieve the "connection" id, this id is player's name, see AbstractPlayer.getName()
        Returns:
        the username
      • setUsername

        public void setUsername​(java.lang.String username)
        Set id for the "connection", this id is player's name, see AbstractPlayer.getName()
        Parameters:
        username - the identify of this connection
      • removeUsername

        public void removeUsername()
        Remove the username value from channel cache
      • setAddress

        public void setAddress​(java.lang.String address)
        Set the address
        Parameters:
        address - the new address value
      • getAddress

        public java.lang.String getAddress()
        Retrieve the "connection" address in string type
        Returns:
        the address
      • send

        public abstract void send​(TObject message)
        Send a message to the client
        Parameters:
        message - the message content, see TObject
      • close

        public abstract void close()
        Close a "connection" between a client with the server
      • clean

        public abstract void clean()
        Delete the keys which are used to identify a player in one "connection". This method need to be implemented when the "connection" type is Socket or WebSocket
      • setRemote

        public abstract void setRemote​(java.net.InetSocketAddress remote)
        Set the current address for your "connection" (only need for Socket type)
        Parameters:
        remote - , see InetSocketAddress
      • getThis

        public abstract Connection getThis()
        Retrieve this connection itself by channel
        Returns:
        the current connection
      • setThis

        public abstract void setThis()
        Set this connection into current channel Note: Set value for one key. You can set your custom data to one connection to quick access. These keys and values should be saved to channel (which is defined by NIO mechanism)
      • removeThis

        public abstract void removeThis()
        Remove the connection object from channel cache