Skip to content

Commit fb001e3

Browse files
author
Frohwalt Egerer
committed
Fix "partial feedback message" problem for Linux (travis)
1 parent 00b8172 commit fb001e3

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/test/java/com/notnoop/apns/utils/Simulator/ApnsServerSimulator.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.net.ServerSocket;
77
import java.net.SocketException;
8+
import java.nio.*;
89
import java.util.ArrayList;
910
import java.util.Date;
1011
import java.util.HashMap;
@@ -231,11 +232,28 @@ public void interrupt() {
231232
protected void fail(final byte status, final int identifier, final InputOutputSocket inputOutputSocket) throws IOException {
232233
logger.debug("FAIL {} {}", status, identifier);
233234
final DataOutputStream outputStream = inputOutputSocket.getOutputStream();
234-
outputStream.writeByte(8);
235-
outputStream.writeByte(status);
236-
outputStream.writeInt(identifier);
235+
236+
// Here comes the fun ... we need to write the feedback packet as one single packet
237+
// or the client will notice the connection to be closed before it read the complete packet.
238+
// But - only on linux, however. (I was not able to see that problem on Windows 7 or OS X)
239+
// What also helped was inserting a little sleep between the flush and closing the connection.
240+
//
241+
// I believe this is irregular (writing to a tcp socket then closing it should result in ALL data
242+
// being visible at the client) but interestingly in Netty there is (was) a similar problem:
243+
// https://github.com/netty/netty/issues/1952
244+
//
245+
// Funnily that appeared as somebody ported this library to use netty.
246+
//
247+
//
248+
//
249+
ByteBuffer bb = ByteBuffer.allocate(6);
250+
bb.put((byte) 8);
251+
bb.put(status);
252+
bb.putInt(identifier);
253+
outputStream.write(bb.array());
237254
outputStream.flush();
238255
inputOutputSocket.close();
256+
logger.debug("FAIL - closed");
239257
}
240258

241259
private class FeedbackRunner extends Thread {

src/test/java/com/notnoop/apns/utils/Simulator/InputOutputSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public DataOutputStream getOutputStream() {
3131
return outputStream;
3232
}
3333

34-
public void close() {
34+
public synchronized void close() {
3535
if (inputStream != null) {
3636
try {
3737
inputStream.close();

0 commit comments

Comments
 (0)