forked from shogo4405/HaishinKit.kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Socket.java
121 lines (106 loc) · 3.62 KB
/
Socket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.haishinkit.net;
import com.haishinkit.util.Log;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public abstract class Socket {
protected ByteBuffer inputBuffer = null;
private java.net.Socket socket = null;
private Thread output = null;
private Thread network = null;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private BlockingQueue<ByteBuffer> outputQueue = null;
public Socket() {
inputBuffer = ByteBuffer.allocate(0);
outputQueue = new ArrayBlockingQueue<ByteBuffer>(128);
}
public final void connect(final String dstName, final int dstPort) {
outputQueue.clear();
network = new Thread() {
public void run() {
doConnection(dstName, dstPort);
}
};
network.start();
}
public void close(final boolean disconnected) {
IOUtils.closeQuietly(socket);
try {
network.join();
} catch (InterruptedException e) {
Log.w(getClass().getName() + "#close", e.toString());
}
}
public void doOutput(ByteBuffer buffer) {
try {
outputQueue.put(buffer);
} catch (InterruptedException e) {
Log.v(getClass().getName(), e.toString());
}
}
protected abstract void onConnect();
protected abstract void listen(final ByteBuffer buffer);
private void doInput() {
try {
int available = inputStream.available();
if (available == 0) {
return;
}
ByteBuffer buffer = ByteBuffer.allocate(inputBuffer.capacity() + available);
buffer.put(inputBuffer);
inputStream.read(buffer.array(), inputBuffer.capacity(), available);
buffer.position(0);
listen(buffer);
inputBuffer = buffer.slice();
} catch (IOException e) {
close(true);
}
}
private void doOutput() {
while (socket != null && socket.isConnected()) {
for (ByteBuffer buffer : outputQueue) {
try {
buffer.flip();
outputStream.write(buffer.array());
outputStream.flush();
outputQueue.remove(buffer);
} catch (IOException e) {
//IOUtils.closeQuietly(socket);
Log.e(getClass().getName() + "#doOutput", e.toString());
}
}
}
}
private void doConnection(final String dstName, final int dstPort) {
try {
socket = new java.net.Socket(dstName, dstPort);
if (socket.isConnected()) {
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
output = new Thread() {
@Override
public void run() {
doOutput();
}
};
output.start();
onConnect();
}
while (socket != null && socket.isConnected()) {
doInput();
}
} catch (Exception e) {
Log.e(getClass().getName() + "#doOutput", e.toString());
close(true);
}
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}