2015-07-10 06:01:56 -07:00
|
|
|
package com.rusefi.io.tcp;
|
|
|
|
|
2017-03-01 14:57:03 -08:00
|
|
|
import com.opensr5.io.DataListener;
|
2020-07-08 16:49:01 -07:00
|
|
|
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
2020-07-04 22:40:29 -07:00
|
|
|
import com.rusefi.io.ByteReader;
|
2020-07-22 11:22:08 -07:00
|
|
|
import com.rusefi.io.serial.AbstractIoStream;
|
2022-10-15 11:36:41 -07:00
|
|
|
import com.rusefi.core.FileUtil;
|
2020-07-22 14:42:16 -07:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-07-27 21:24:52 -07:00
|
|
|
import java.io.*;
|
2020-06-25 20:35:48 -07:00
|
|
|
import java.net.Socket;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
/**
|
2020-06-09 17:08:16 -07:00
|
|
|
* Andrey Belomutskiy, (c) 2013-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
* 5/11/2015.
|
|
|
|
*/
|
2020-07-22 11:22:08 -07:00
|
|
|
public class TcpIoStream extends AbstractIoStream {
|
2015-12-02 17:10:06 -08:00
|
|
|
private final InputStream input;
|
|
|
|
private final OutputStream output;
|
2020-07-18 21:07:37 -07:00
|
|
|
private final String loggingPrefix;
|
2020-07-23 21:52:33 -07:00
|
|
|
private final DisconnectListener disconnectListener;
|
2020-07-22 14:42:16 -07:00
|
|
|
@NotNull
|
|
|
|
private final Socket socket;
|
2020-07-08 19:10:06 -07:00
|
|
|
private final IncomingDataBuffer dataBuffer;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-07-24 09:44:29 -07:00
|
|
|
public TcpIoStream(String loggingPrefix, Socket socket) throws IOException {
|
|
|
|
this(loggingPrefix, socket, DisconnectListener.VOID);
|
2020-07-23 21:52:33 -07:00
|
|
|
}
|
|
|
|
|
2020-07-24 09:44:29 -07:00
|
|
|
public TcpIoStream(String loggingPrefix, Socket socket, DisconnectListener disconnectListener) throws IOException {
|
2020-07-18 21:07:37 -07:00
|
|
|
this.loggingPrefix = loggingPrefix;
|
2020-07-23 21:52:33 -07:00
|
|
|
this.disconnectListener = disconnectListener;
|
2020-07-22 14:42:16 -07:00
|
|
|
if (socket == null)
|
|
|
|
throw new NullPointerException("socket");
|
|
|
|
this.socket = socket;
|
2022-02-10 19:44:21 -08:00
|
|
|
input = new BufferedInputStream(socket.getInputStream());
|
|
|
|
output = new BufferedOutputStream(socket.getOutputStream());
|
|
|
|
dataBuffer = createDataBuffer(loggingPrefix);
|
2020-07-18 21:07:37 -07:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:45:51 -07:00
|
|
|
@NotNull
|
2020-10-28 21:04:30 -07:00
|
|
|
public static TcpIoStream open(String port) throws IOException {
|
2020-10-08 20:45:51 -07:00
|
|
|
int portPart = TcpConnector.getTcpPort(port);
|
|
|
|
String hostname = TcpConnector.getHostname(port);
|
|
|
|
Socket socket = new Socket(hostname, portPart);
|
|
|
|
|
|
|
|
return new TcpIoStream("[start] ", socket);
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:42:16 -07:00
|
|
|
@Override
|
|
|
|
public void close() {
|
2020-07-26 13:52:58 -07:00
|
|
|
// we need to guarantee only one onDisconnect invocation for retry logic to be healthy
|
|
|
|
synchronized (this) {
|
|
|
|
if (!isClosed()) {
|
|
|
|
super.close();
|
|
|
|
disconnectListener.onDisconnect("on close");
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 14:42:16 -07:00
|
|
|
FileUtil.close(socket);
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:49:01 -07:00
|
|
|
@Override
|
|
|
|
public IncomingDataBuffer getDataBuffer() {
|
|
|
|
return dataBuffer;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
@Override
|
|
|
|
public void write(byte[] bytes) throws IOException {
|
2020-07-30 17:34:21 -07:00
|
|
|
super.write(bytes);
|
2015-12-02 17:10:06 -08:00
|
|
|
output.write(bytes);
|
2020-07-23 19:16:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void flush() throws IOException {
|
|
|
|
super.flush();
|
2015-12-02 17:10:06 -08:00
|
|
|
output.flush();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-06-20 21:49:37 -07:00
|
|
|
public void setInputListener(final DataListener listener) {
|
2020-07-25 17:05:37 -07:00
|
|
|
ByteReader.runReaderLoop(loggingPrefix, listener, input::read, this);
|
2020-07-23 21:52:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface DisconnectListener {
|
2020-07-26 13:52:58 -07:00
|
|
|
DisconnectListener VOID = (String message) -> {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-07-23 21:52:33 -07:00
|
|
|
};
|
2020-07-26 13:52:58 -07:00
|
|
|
void onDisconnect(String message);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|