Package it.polimi.ingsw.eriantys.server
Class ClientConnection
java.lang.Object
it.polimi.ingsw.eriantys.server.ClientConnection
This class represents a single client-server connection.
It exposes methods which allow to read from and write to the connection's I/O streams.
-
Constructor Summary
ConstructorDescriptionClientConnection
(Server server, Socket socketToClient) Constructs a new instance ofClientConnection
with the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptiongetGame()
A getter for the game whichthis
refers to.boolean
A getter to know if the client is inside a game lobby.void
ping()
Sends aPing
message to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.void
read()
Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages.void
Sets the connection's game reference to the specifiedGame
.void
setJoinedLobby
(boolean joinedLobby) Sets thejoinedLobby
member variable to the specified value.void
setRunning
(boolean running) Sets therunning
member variable to the specified value.void
Writes the specifiedMessage
to the connection socket's output stream, disconnecting the client if an I/O error occurs.
-
Constructor Details
-
ClientConnection
Constructs a new instance ofClientConnection
with the specified parameters.- Parameters:
server
- the game serversocketToClient
- the socket between the server and this connection's client- Throws:
IOException
- if an error occurs when retrieving the input or output stream
-
-
Method Details
-
setRunning
public void setRunning(boolean running) Sets therunning
member variable to the specified value.- Parameters:
running
- the desired value
-
getGame
A getter for the game whichthis
refers to.- Returns:
- the game which the connection refers to (if there is one), or
null
otherwise.
-
setGame
Sets the connection's game reference to the specifiedGame
.- Parameters:
game
- the connection's newGame
reference
-
hasJoinedLobby
public boolean hasJoinedLobby()A getter to know if the client is inside a game lobby.- Returns:
true
if and only if the client associated withthis
has joined a game lobby
-
setJoinedLobby
public void setJoinedLobby(boolean joinedLobby) Sets thejoinedLobby
member variable to the specified value.- Parameters:
joinedLobby
- the desired value
-
read
public void read()Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages. -
write
Writes the specifiedMessage
to the connection socket's output stream, disconnecting the client if an I/O error occurs.- Parameters:
message
- the message to be written and sent to the client
-
ping
public void ping()Sends aPing
message to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.
-