Skip to content

Commit 4b8aafa

Browse files
committed
fix SSLClient
1 parent ae96beb commit 4b8aafa

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/main/java/net/lecousin/framework/network/client/SSLClient.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.lecousin.framework.exception.NoException;
2020
import net.lecousin.framework.network.SocketOptionValue;
2121
import net.lecousin.framework.network.ssl.SSLLayer;
22+
import net.lecousin.framework.util.Triple;
2223

2324
/**
2425
* SSL client adding SSL layer to a TCPClient.
@@ -105,16 +106,26 @@ public void dataReceived(LinkedList<ByteBuffer> data) {
105106
if (data.isEmpty()) return;
106107
AsyncWork<ByteBuffer, IOException> waiting;
107108
ByteBuffer buffer;
109+
Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer> waitAgain = null;
108110
synchronized (this) {
109-
waiting = (AsyncWork<ByteBuffer, IOException>)removeAttribute(WAITING_DATA_ATTRIBUTE);
110-
if (waiting == null) {
111+
LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>> list =
112+
(LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>>)
113+
getAttribute(WAITING_DATA_ATTRIBUTE);
114+
if (list == null) {
111115
receivedData.addAll(data);
112116
return;
113117
}
118+
waiting = list.removeFirst().getValue1();
119+
if (list.isEmpty())
120+
removeAttribute(WAITING_DATA_ATTRIBUTE);
121+
else
122+
waitAgain = list.getFirst();
114123
buffer = data.removeFirst();
115124
if (!data.isEmpty())
116125
receivedData.addAll(data);
117126
}
127+
if (waitAgain != null)
128+
waitForSSLData(waitAgain.getValue2().intValue(), waitAgain.getValue3().intValue());
118129
waiting.unblockSuccess(buffer);
119130
}
120131

@@ -197,40 +208,60 @@ public Void run() {
197208
@Override
198209
public AsyncWork<ByteBuffer, IOException> receiveData(int expectedBytes, int timeout) {
199210
AsyncWork<ByteBuffer, IOException> result = new AsyncWork<>();
211+
boolean firstWait = false;
200212
synchronized (sslClient) {
201213
if (!receivedData.isEmpty()) {
202214
result.unblockSuccess(receivedData.removeFirst());
203-
} else
204-
setAttribute(WAITING_DATA_ATTRIBUTE, result);
215+
return result;
216+
}
217+
@SuppressWarnings("unchecked")
218+
LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>> list =
219+
(LinkedList<Triple<AsyncWork<ByteBuffer, IOException>,Integer,Integer>>)getAttribute(WAITING_DATA_ATTRIBUTE);
220+
if (list == null) {
221+
list = new LinkedList<>();
222+
setAttribute(WAITING_DATA_ATTRIBUTE, list);
223+
firstWait = true;
224+
}
225+
list.add(new Triple<>(result, Integer.valueOf(expectedBytes), Integer.valueOf(timeout)));
205226
}
206227
if (result.isUnblocked())
207228
return result;
208-
waitForSSLData(expectedBytes, timeout);
229+
if (firstWait)
230+
waitForSSLData(expectedBytes, timeout);
209231
return result;
210232
}
211233

234+
private AsyncWork<ByteBuffer, IOException> lastReceive = null;
235+
212236
private void waitForSSLData(int expectedBytes, int timeout) {
237+
if (lastReceive != null && !lastReceive.isUnblocked())
238+
return;
213239
AsyncWork<ByteBuffer, IOException> receive = super.receiveData(expectedBytes, timeout);
240+
lastReceive = receive;
214241
receive.listenAsync(new Task.Cpu<Void, NoException>("Receive SSL data from server", Task.PRIORITY_NORMAL) {
215242
@SuppressWarnings("unchecked")
216243
@Override
217244
public Void run() {
218245
if (receive.hasError()) {
219-
AsyncWork<ByteBuffer, IOException> waiting;
246+
LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>> list;
220247
synchronized (sslClient) {
221-
waiting = (AsyncWork<ByteBuffer, IOException>)removeAttribute(WAITING_DATA_ATTRIBUTE);
248+
list = (LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>>)
249+
removeAttribute(WAITING_DATA_ATTRIBUTE);
222250
}
223-
if (waiting != null)
224-
waiting.error(receive.getError());
251+
if (list != null)
252+
for (Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer> t : list)
253+
t.getValue1().error(receive.getError());
225254
return null;
226255
}
227256
if (receive.isCancelled()) {
228-
AsyncWork<ByteBuffer, IOException> waiting;
257+
LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>> list;
229258
synchronized (sslClient) {
230-
waiting = (AsyncWork<ByteBuffer, IOException>)removeAttribute(WAITING_DATA_ATTRIBUTE);
259+
list = (LinkedList<Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer>>)
260+
removeAttribute(WAITING_DATA_ATTRIBUTE);
231261
}
232-
if (waiting != null)
233-
waiting.cancel(receive.getCancelEvent());
262+
if (list != null)
263+
for (Triple<AsyncWork<ByteBuffer, IOException>, Integer, Integer> t : list)
264+
t.getValue1().cancel(receive.getCancelEvent());
234265
return null;
235266
}
236267
ByteBuffer b = receive.getResult();

src/main/java/net/lecousin/framework/network/client/TCPClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ public SynchronizationPoint<IOException> connect(SocketAddress address, int time
192192
public AsyncWork<ByteBuffer, IOException> receiveData(int expectedBytes, int timeout) {
193193
if (endOfInput)
194194
return new AsyncWork<>(null, null);
195+
if (networkClient.reading != null && !networkClient.reading.isUnblocked()) {
196+
return new AsyncWork<>(null, new IOException("TCPClient is already waiting for data"));
197+
}
195198
if (logger.isDebugEnabled())
196199
logger.debug("Register to NetworkManager for reading data");
197200
networkClient.reading = new AsyncWork<>();

0 commit comments

Comments
 (0)