| | |
| | |
|
| | |
|
| | |
|
| | | import com.hdl.sdk.common.utils.ByteUtils;
|
| | | import com.hdl.sdk.common.utils.LogUtils;
|
| | | import com.hdl.sdk.common.utils.ThreadToolUtils;
|
| | | import com.hdl.sdk.socket.SocketBoot;
|
| | | import com.hdl.sdk.socket.SocketOptions;
|
| | |
| | | import java.io.OutputStream;
|
| | | import java.net.InetSocketAddress;
|
| | | import java.net.Socket;
|
| | | import java.net.SocketAddress;
|
| | | import java.net.StandardSocketOptions;
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | |
|
| | |
| | | */
|
| | | public final class TcpClient implements IClient {
|
| | |
|
| | |
|
| | | private SocketOptions socketOptions;
|
| | |
|
| | | private final String ip;
|
| | |
| | |
|
| | | private Socket mSocket;
|
| | |
|
| | | private byte[] readBuffer;
|
| | | private final static List<TcpClient> tcpClientList = new ArrayList();
|
| | |
|
| | | /**
|
| | | * 从连接池中找出当前IP及端口的连接客户端
|
| | | * @param ipAdderss 连接IP地址
|
| | | * @param port 连接端口
|
| | | * @return
|
| | | */
|
| | | public static TcpClient getTcpClientByIP(String ipAdderss,int port) {
|
| | | for(TcpClient tcpClient:tcpClientList){
|
| | | if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port)
|
| | | {
|
| | | return tcpClient;
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | private byte[] readBuffer = new byte[4*1024];
|
| | |
|
| | | private TcpClient(String ip, int port, SocketOptions socketOptions) {
|
| | | this.socketOptions = socketOptions;
|
| | | this.ip = ip;
|
| | | this.port = port;
|
| | | socketOptions.setIp(ip);
|
| | | socketOptions.setPort(port);
|
| | | }
|
| | |
|
| | | public static SocketBoot init(String ip, int port, SocketOptions options) {
|
| | |
| | |
|
| | | @Override
|
| | | public void connect() throws Exception {
|
| | |
|
| | | mSocket = getSocket();
|
| | | SocketOptions options = getOptions();
|
| | | mSocket.connect(new InetSocketAddress(ip, port));
|
| | | // SocketOptions options = getOptions();
|
| | | mSocket.connect(new InetSocketAddress(ip, port), 3 * 1000);
|
| | | mSocket.setTcpNoDelay(true);
|
| | | mSocket.setReuseAddress(true);
|
| | | mSocket.setKeepAlive(true);
|
| | | readBuffer = new byte[options.getReadMaxBufferSize()];
|
| | | // mSocket.setKeepAlive(true);
|
| | | mSocket.setSoTimeout(25 * 1000);
|
| | |
|
| | | tcpClientList.add(this);
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | |
|
| | | @Override
|
| | |
| | | }
|
| | |
|
| | | @Override
|
| | | public boolean isConnect() {
|
| | | if (mSocket == null) {
|
| | | return false;
|
| | | }
|
| | |
|
| | | return mSocket.isConnected() && !mSocket.isClosed();
|
| | | }
|
| | |
|
| | |
|
| | | @Override
|
| | | public synchronized SocketOptions getOptions() {
|
| | | if (socketOptions == null) {
|
| | | socketOptions = new SocketOptions();
|
| | |
| | | @Override
|
| | | public void onHandleResponse() throws Exception {
|
| | | final InputStream stream = getInputStream();
|
| | |
|
| | | if (stream != null && getOptions() != null) {
|
| | | readBuffer = new byte[1024];
|
| | | while ((getInputStream().read(readBuffer)) != -1) {
|
| | | while ( true) {
|
| | | int len=getInputStream().read(readBuffer);
|
| | | if(len<=0){
|
| | | throw new Exception("接收异常,接收数据长度len="+len);
|
| | | }
|
| | |
|
| | | IHandleMessage handleMessage = getOptions().getHandleMessage();
|
| | | if (handleMessage != null) {
|
| | | handleMessage.read(readBuffer);
|
| | | byte []bytes = new byte[len];
|
| | | System.arraycopy(readBuffer,0,bytes,0,len);
|
| | | //完整的数据才回调
|
| | | handleMessage.read(bytes,ip);
|
| | | }
|
| | | }
|
| | | }
|
| | |
| | | public void sendMsg(byte[] msg) throws Exception {
|
| | | final OutputStream outputStream = getOutStream();
|
| | | if (outputStream != null && getOptions() != null) {
|
| | | try {
|
| | | IHandleMessage handleMessage = getOptions().getHandleMessage();
|
| | | handleMessage.write(handleMessage.write(msg));
|
| | | getOutStream().write(msg);
|
| | |
|
| | | } finally {
|
| | | outputStream.write(msg);
|
| | | outputStream.flush();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|