add doc
This commit is contained in:
@@ -23,6 +23,10 @@ public class Socket {
|
||||
|
||||
public final Signal onClose = new Signal();
|
||||
|
||||
/**
|
||||
* Construct a UDP Socket to connect to a server
|
||||
* @throws SocketException
|
||||
*/
|
||||
public Socket() throws SocketException {
|
||||
this.socket = new DatagramSocket();
|
||||
this.handlers = new ArrayList<>();
|
||||
@@ -31,6 +35,11 @@ public class Socket {
|
||||
this.readThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a UDP Socket to listen to connexion at the specified port
|
||||
* @param listeningPort the port to listen to
|
||||
* @throws SocketException
|
||||
*/
|
||||
public Socket(int listeningPort) throws SocketException {
|
||||
this.socket = new DatagramSocket(listeningPort);
|
||||
this.handlers = new ArrayList<>();
|
||||
@@ -43,11 +52,20 @@ public class Socket {
|
||||
this.handlers.add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return this.socket.getLocalPort()
|
||||
*/
|
||||
public int getLocalPort() {
|
||||
return this.socket.getLocalPort();
|
||||
}
|
||||
|
||||
// needs to be accessible by PacketPool
|
||||
/**
|
||||
* Send a packet to the specified address
|
||||
* @param packet the packet to send
|
||||
* @param address the address to send to
|
||||
* @throws IOException
|
||||
*/
|
||||
void sendPacket(ReliablePacket packet, InetSocketAddress address) throws IOException {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(stream);
|
||||
@@ -59,6 +77,12 @@ public class Socket {
|
||||
this.socket.send(dataPacket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to recieve packets and send them to the PacketPool.
|
||||
* This method blocks until something has been read
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
private void recievePacketReliable()
|
||||
throws IOException, ClassNotFoundException {
|
||||
byte[] buffer = new byte[65535];
|
||||
@@ -73,10 +97,22 @@ public class Socket {
|
||||
this.packetPool.onPacketReceived(packet, address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a packet to the specified address
|
||||
* @param packet the packet to send
|
||||
* @param address the address to send to
|
||||
* @throws IOException
|
||||
*/
|
||||
public void sendPacket(Packet packet, InetSocketAddress address) throws IOException {
|
||||
this.packetPool.sendPacket(packet, address);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recieve packet in a reliable way (packets are in the right order).
|
||||
* This method is blocking
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
private void recievePacket() throws IOException, ClassNotFoundException {
|
||||
this.recievePacketReliable();
|
||||
var entry = this.packetPool.getNextPacket();
|
||||
@@ -86,6 +122,9 @@ public class Socket {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the socket
|
||||
*/
|
||||
public void close() {
|
||||
this.socket.close();
|
||||
this.packetPool.close();
|
||||
@@ -93,12 +132,20 @@ public class Socket {
|
||||
this.onClose.emit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the packet
|
||||
* @param packet the packet to dispatch
|
||||
* @param address the incoming address
|
||||
*/
|
||||
void handlePacket(Packet packet, InetSocketAddress address) {
|
||||
for (PacketHandler handler : this.handlers) {
|
||||
handler.handlePacket(packet, address);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop to read all packets
|
||||
*/
|
||||
private void readLoop() {
|
||||
try {
|
||||
while (!Thread.interrupted()) {
|
||||
|
||||
Reference in New Issue
Block a user