Fix #54 #100

Merged
louis_royer merged 2 commits from fix54 into master 5 years ago

@ -64,7 +64,7 @@ public class HashRequest extends Payload {
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
/* Read algo list */
int size = getPayloadSize(packet);
int size = getPayloadSize(packet) - filenameSize - (FILENAME_POSITION - PAYLOAD_START_POSITION);
String[] algoListStr = BytesArrayTools.readStringArray(packet, FILENAME_POSITION + filenameSize, size, "\n");
int i = 0;
algoList = new HashAlgorithm[algoListStr.length];

@ -28,6 +28,7 @@ import protocolP2P.SizeResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
/** Representation of packet.
* @author Louis Royer
@ -113,20 +114,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
throw new InternalError();
}
Socket ss = (Socket)socket;
byte[] packet = new byte[1024];
byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION];
try {
if (-1 == ss.getInputStream().read(packet)) {
if (-1 == ss.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) {
throw new IOException();
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
ss.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
}
int payloadSize = Payload.getPayloadSize(packetHeader);
byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize);
if (-1 == ss.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) {
throw new IOException();
}
// contruction
boolean protocolError = false;
@ -175,6 +171,16 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
if (protocolError) {
throw new ProtocolError();
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
ss.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
}
}
}
/** Send a Response to a Request (typically used from server).
@ -221,20 +227,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
throw new InternalError();
}
// reception
byte[] packet = new byte[8192];
byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION];
try {
if (-1== requestSocket.getInputStream().read(packet)) {
if (-1 == requestSocket.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) {
throw new IOException();
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
requestSocket.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
}
int payloadSize = Payload.getPayloadSize(packetHeader);
byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize);
if (-1 == requestSocket.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) {
throw new IOException();
}
// contruction
try {
@ -276,6 +277,16 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e;
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
requestSocket.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
}
}
}
/** Private constructor with packet as byte[] parameter (typically used when receiving Packet response).

@ -30,6 +30,7 @@ import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.io.IOException;
import java.util.Arrays;
/** Representation of packet.
* @author Louis Royer
@ -116,9 +117,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket
throw new InternalError();
}
DatagramSocket ss = (DatagramSocket)socket;
byte[] packet = new byte[1024];
DatagramPacket reception = new DatagramPacket(packet, packet.length);
byte[] packetTmp = new byte[0xFFFF];
DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length);
ss.receive(reception);
int payloadSize = Payload.getPayloadSize(packetTmp);
byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize);
responseSocketAddress = reception.getSocketAddress();
remoteHost = new HostItem(reception.getAddress().getHostName(), reception.getPort());
// contruction
@ -216,9 +219,11 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket
throw new InternalError();
}
// reception
byte[] packet = new byte[8192];
DatagramPacket reception = new DatagramPacket(packet, packet.length);
byte[] packetTmp = new byte[0xFFFF];
DatagramPacket reception = new DatagramPacket(packetTmp, packetTmp.length);
requestSocket.receive(reception);
int payloadSize = Payload.getPayloadSize(packetTmp);
byte[] packet = Arrays.copyOf(packetTmp, Payload.PAYLOAD_START_POSITION + payloadSize);
// contruction
try {

Loading…
Cancel
Save