Package lu.kbra.p4j.socket.server
Class P4JServer
java.lang.Object
lu.kbra.p4j.socket.server.P4JServer
- All Implemented Interfaces:
Closeable,AutoCloseable,Runnable,P4JInstance,P4JInstance.P4JServerInstance,EventDispatcher
public class P4JServer
extends Object
implements P4JInstance.P4JServerInstance, EventDispatcher, Closeable, Runnable
-
Nested Class Summary
Nested classes/interfaces inherited from interface lu.kbra.p4j.socket.P4JInstance
P4JInstance.P4JClientInstance, P4JInstance.P4JClientServerInstance, P4JInstance.P4JServerClientInstance, P4JInstance.P4JServerInstance -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionP4JServer(CodecManager codecManager, EncryptionManager encryptionManager, CompressionManager compressionManager) Default constructor for a P4JServer, creates a defaultClientManagerbound to this server instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidbind(InetSocketAddress inetSocketAddress) Binds the current server to the local address.voidbroadcast(Function<ServerClient, S2CPacket<?>> packetSupplier) Sends the packet provided by the supplier to all the connected clients.voidSends the packets to all the connected clients.voidSends the packet to all the connected clients.voidbroadcastIf(Function<ServerClient, S2CPacket<?>> packetSupplier, Predicate<ServerClient> condition) Iterates over all the connected clients and sends the packet provided by the supplier, if the predicate's condition is met.voidbroadcastIf(List<S2CPacket<?>> packets, Predicate<ServerClient> condition) Iterates over all the connected clients and sends the specified packet(s) if the predicate's condition is met.voidbroadcastIf(S2CPacket<?> packet, Predicate<ServerClient> condition) Iterates over all the connected clients and sends the specified packet if the predicate's condition is met.voidclose()Closes the server socket.
The server will no longer accept new client connections, all clients will be forcefully disconnected and the local port is released.voidvoiddispatchEvent(P4JEvent event) getCodec()final P4JEndPointintgetPort()getState()final booleanisAlive()final voidjoin()final voidjoin(long millis) final voidjoin(long millis, int nanos) voidregisterPacket(Class<?> p, int id) voidrun()voidSets the server socket in client accept mode.
The server will accept all future incoming client connections.voidsetClientManager(ClientManager clientManager) voidsetCodec(CodecManager codec) voidsetCompression(CompressionManager compression) voidsetEncryption(EncryptionManager encryption) voidsetEventManager(EventManager eventManager) voidsetPackets(PacketManager packets) voidSets the server socket in client refuse mode.
The server will refuse all future incoming client connections but keep current connections alive.toString()
-
Field Details
-
MAX_PACKET_SIZE
public static int MAX_PACKET_SIZE
-
-
Constructor Details
-
P4JServer
public P4JServer(CodecManager codecManager, EncryptionManager encryptionManager, CompressionManager compressionManager) Default constructor for a P4JServer, creates a defaultClientManagerbound to this server instance.- Parameters:
codecManager- the server codec managerencryptionManager- the server encryption managercompressionManager- the server compression manager
-
-
Method Details
-
bind
Binds the current server to the local address.- Parameters:
inetSocketAddress- the local address to bind to- Throws:
IOException- if theServerSocketChannelorSelectorcannot be opened or boundP4JServerException- if the server is already bound
-
broadcast
Sends the packet provided by the supplier to all the connected clients. -
broadcast
Sends the packets to all the connected clients. -
broadcast
Sends the packet to all the connected clients. -
broadcastIf
public void broadcastIf(Function<ServerClient, S2CPacket<?>> packetSupplier, Predicate<ServerClient> condition) Iterates over all the connected clients and sends the packet provided by the supplier, if the predicate's condition is met. -
broadcastIf
Iterates over all the connected clients and sends the specified packet(s) if the predicate's condition is met. -
broadcastIf
Iterates over all the connected clients and sends the specified packet if the predicate's condition is met. -
close
public void close()Closes the server socket.
The server will no longer accept new client connections, all clients will be forcefully disconnected and the local port is released.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
P4JServerException- if the server socket is already closed
-
disconnectAll
public void disconnectAll() -
dispatchEvent
-
getClientManager
-
getCodec
-
getCompression
-
getConnectedClients
-
getEncryption
-
getEndPoint
- Specified by:
getEndPointin interfaceP4JInstance- Specified by:
getEndPointin interfaceP4JInstance.P4JServerInstance
-
getEventManager
-
getLocalInetSocketAddress
-
getPackets
-
getPort
public int getPort()- Returns:
- the local port bound to the server or -1 if the server is closed
-
getServerStatus
-
getState
-
isAlive
public final boolean isAlive() -
join
- Throws:
InterruptedException
-
join
- Throws:
InterruptedException
-
join
- Throws:
InterruptedException
-
registerPacket
-
run
public void run() -
setAccepting
public void setAccepting()Sets the server socket in client accept mode.
The server will accept all future incoming client connections.- Throws:
P4JServerException- if the server socket is closed
-
setClientManager
-
setCodec
-
setCompression
-
setEncryption
-
setEventManager
-
setPackets
-
setRefusing
public void setRefusing()Sets the server socket in client refuse mode.
The server will refuse all future incoming client connections but keep current connections alive.- Throws:
P4JServerException- if the server socket is closed.
-
toString
-