Class ClajRoom

java.lang.Object
com.xpdustry.claj.server.ClajRoom
All Implemented Interfaces:
arc.net.NetListener

public class ClajRoom extends Object implements arc.net.NetListener
  • Field Details

    • UNCREATED_ROOM

      public static final long UNCREATED_ROOM
      Id saying that no room is created. This should be handled as an invalid id.
      See Also:
    • CON_BROADCAST

      public static final int CON_BROADCAST
      Id meaning that that the connection is invalid, and used to broadcast packets to all clients.
      This is used for disconnect, received and idle events, but not for connected one, as it makes no sense.
      See Also:
    • created

      protected boolean created
    • closed

      protected boolean closed
    • id

      public final long id
      The room id.
    • sid

      public final String sid
      The room id encoded in an url-safe base64 string.
      See Also:
      • invalid reference
        com.xpdustry.claj.api.ClajLink
    • host

      public final ClajConnection host
      The host connection of this room.
    • clientsMap

      public final arc.struct.IntMap<ClajConnection> clientsMap
    • clients

      public final arc.struct.Seq<ClajConnection> clients
    • transferredPackets

      public final NetworkSpeed transferredPackets
      For debugging, to know how many packets were transferred from a client to a host, and vice versa.
    • stateRate

      public final arc.util.Ratekeeper stateRate
      Room state rate-limit. New states will simply be discarded.
    • createdAt

      public long createdAt
      Creation date of the room. Sets when create() is called.
    • closedAt

      public long closedAt
      Closing date of the room. Sets when close() is called.
    • isPublic

      public boolean isPublic
      Whether the room will be added is public list or not.
    • isProtected

      public boolean isProtected
      Whether the room needs a password or not to join it.
    • password

      public short password
      The room password
    • canRequestState

      public boolean canRequestState
      Whether the host want, or not, the server to request his state when needed.
    • state

      public Object state
      De-serialized room state, only present if the right decoder is present.
    • rawState

      public ByteBuffer rawState
      State of the room as raw data. null if no state was received.
    • lastReceivedState

      public long lastReceivedState
      Time of the last received room state. (in ns).
    • lastRequestedState

      public long lastRequestedState
      Time of the last requested room state. (in ns).
    • requestingState

      public boolean requestingState
      Whether a state has been requested to the room.
    • type

      public final com.xpdustry.claj.common.status.ClajType type
      Room implementation type. Can be null.
    • maxClients

      public int maxClients
      Maximum number of CLaJ client allowed in this room.
      0 means no limit and the value must not be higher that the server limit.
  • Constructor Details

    • ClajRoom

      public ClajRoom(long id, ClajConnection host, com.xpdustry.claj.common.status.ClajType type)
  • Method Details

    • setRoom

      protected void setRoom(ClajConnection con)
    • removeRoom

      protected void removeRoom(ClajConnection con)
    • connected

      public void connected(arc.net.Connection connection)
      Alerts the host that a new client is coming
      Specified by:
      connected in interface arc.net.NetListener
    • connected

      public void connected(ClajConnection connection)
      Alerts the host of the client arrival.
    • disconnected

      public void disconnected(arc.net.Connection connection, arc.net.DcReason reason)
      Alerts the host that a client disconnected. This doesn't close the connection.
      Specified by:
      disconnected in interface arc.net.NetListener
    • disconnected

      public void disconnected(ClajConnection connection, arc.net.DcReason reason)
      Alerts the host that a client disconnected. This doesn't close the connection.
    • disconnectedQuietly

      public void disconnectedQuietly(arc.net.Connection connection, arc.net.DcReason reason)
      Doesn't notify the room host about a disconnected client.
    • disconnectedQuietly

      public void disconnectedQuietly(ClajConnection connection, arc.net.DcReason reason)
      Doesn't notify the room host about a disconnected client, but this does close it.
    • disconnectAllClients

      public void disconnectAllClients(arc.net.DcReason reason, boolean notify)
      Close all connections of the room.
    • received

      public void received(arc.net.Connection connection, Object object)
      Wraps and re-sends the packet to the host, if it come from a connection.
      Or un-wraps and re-sends the packet to the specified connection.

      Only ConnectionPayloadPacket and RawPacket are allowed.

      Specified by:
      received in interface arc.net.NetListener
    • received

      public void received(ClajConnection connection, Object object)
    • received

      public void received(arc.net.Connection connection, com.xpdustry.claj.common.packets.ConnectionPayloadPacket wrap)
      Unwraps the packet and sends it to the corresponding connection.
      This will notify the host if the connection is not found.

      Please note that this method is mainly called from network thread.

    • received

      public void received(ClajConnection connection, com.xpdustry.claj.common.packets.ConnectionPayloadPacket wrap)
    • received

      public void received(arc.net.Connection connection, com.xpdustry.claj.common.packets.RawPacket raw)
      We never send claj packets to anyone other than the room host, framework packets are ignored and mindustry packets are saved as raw buffer.

      Please note that this method is mainly called from network thread.

    • received

      public void received(ClajConnection connection, com.xpdustry.claj.common.packets.RawPacket raw)
    • idle

      public void idle(arc.net.Connection connection)
      Notifies the host of an idle connection.
      Specified by:
      idle in interface arc.net.NetListener
    • idle

      public void idle(ClajConnection connection)
      Notifies the host of an idle connection.
    • allowsType

      public boolean allowsType(com.xpdustry.claj.common.status.ClajType type)
      Returns:
      true if type is null, the provided one is the same or null, if ClajConfig.acceptNoType is true.
    • isEmpty

      public boolean isEmpty()
      Returns:
      whether this room have clients or not.
    • clients

      public int clients()
      Returns:
      the number of CLaJ clients in this room.
    • isCreataed

      public boolean isCreataed()
      Returns:
      whether the room is created or not.
    • isClosed

      public boolean isClosed()
      Returns:
      whether the room is closed or not.
    • create

      public void create()
      Link this room to the host and notify it's id. Can only be called once.
    • close

      public void close()
    • close

      public void close(com.xpdustry.claj.common.status.CloseReason reason)
      Closes the room and disconnects the host and all clients.
      The room object cannot be used anymore after this.
    • close

      protected void close(com.xpdustry.claj.common.status.CloseReason reason, boolean notify)
    • closeQuietly

      public void closeQuietly()
      Same as close(), but doesn't notify closure to host.
    • message

      public void message(String text)
      Sends a message to the host and clients.
    • message

      public void message(com.xpdustry.claj.common.status.MessageType message)
      Sends a message the host and clients. Will be translated by the room host.
    • popup

      public void popup(String text)
      Sends a popup to the room host.
    • setConfiguration

      public void setConfiguration(boolean isPublic, boolean isProtected, short password, boolean requestState, int maxClients)
    • requestState

      public boolean requestState()
      Only requests state if not already done.
      Returns:
      whether the state has been requested.
    • requestState

      public boolean requestState(long timeNs)
      Only requests state if not already done.
      Returns:
      whether the state has been requested.
    • setState

      public void setState(ByteBuffer rawState)
    • isStateRequestTimedOut

      public boolean isStateRequestTimedOut()
    • isStateRequestTimedOut

      public boolean isStateRequestTimedOut(long timeNs)
    • isStateOutdated

      public boolean isStateOutdated()
    • isStateOutdated

      public boolean isStateOutdated(long timeNs)
    • shouldRequestState

      public boolean shouldRequestState()
    • needStateRequest

      public boolean needStateRequest(long timeNs)
    • sendRoomState

      public void sendRoomState(ClajConnection connection)
      State is only send if room isPublic.
    • isHost

      public boolean isHost(arc.net.Connection con)
      Returns:
      whether specified connection is the room host or not.
    • isHost

      public boolean isHost(ClajConnection con)
      Returns:
      whether specified connection is the room host or not.
    • contains

      public boolean contains(ClajConnection con)
      Returns:
      whether the connection is the room host or one of his client.
    • contains

      public boolean contains(arc.net.Connection con)
      Returns:
      whether the connection is the room host or one of his client.
    • containsClient

      public boolean containsClient(ClajConnection con)
      Returns:
      whether the connection is in this room.
    • containsClient

      public boolean containsClient(arc.net.Connection con)
      Returns:
      whether the connection is in this room.
    • hashCode

      public int hashCode()
      Only hashes id.
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Only uses id as identity.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • sendConnectionJoined

      public static void sendConnectionJoined(ClajConnection dest, ClajConnection joined)
    • sendConnectionAccepted

      public static void sendConnectionAccepted(ClajConnection dest, ClajRoom room)
    • sendConnectionRejected

      public static void sendConnectionRejected(ClajConnection dest, long roomId, com.xpdustry.claj.common.status.RejectReason reason)
    • sendDisconnect

      public static void sendDisconnect(ClajConnection dest, int conId, arc.net.DcReason reason)
    • sendPayload

      public static void sendPayload(ClajConnection dest, int conId, com.xpdustry.claj.common.packets.RawPacket raw, boolean isTcp)
      Note that raw packet will be freed after serialization.
    • sendIdle

      public static void sendIdle(ClajConnection dest, int conId)
    • sendRoomCreated

      public static void sendRoomCreated(ClajConnection dest, long roomId)
    • sendRoomClosed

      public static void sendRoomClosed(ClajConnection dest, com.xpdustry.claj.common.status.CloseReason reason)
    • sendMessage

      public static void sendMessage(ClajConnection dest, String text)
    • sendMessage

      public static void sendMessage(ClajConnection dest, com.xpdustry.claj.common.status.MessageType message)
    • sendPopup

      public static void sendPopup(ClajConnection dest, String text)
    • sendRoomStateRequest

      public static void sendRoomStateRequest(ClajConnection dest)
    • sendRoomState

      public static void sendRoomState(ClajConnection dest, ClajRoom room)
    • sendRoomInfoRejected

      public static void sendRoomInfoRejected(ClajConnection dest)