com.fastchar.socket.core.FastTcpSocket Maven / Gradle / Ivy
package com.fastchar.socket.core;
import io.netty.channel.Channel;
import java.util.ArrayList;
import java.util.List;
public class FastTcpSocket {
private final Channel channel;
private final List onSendListeners = new ArrayList<>();
public FastTcpSocket(Channel channel) {
this.channel = channel;
}
public Channel getChannel() {
return channel;
}
public FastTcpSocket addListener(OnSendListener onSendListener) {
if (onSendListeners.contains(onSendListener)) {
return this;
}
onSendListeners.add(onSendListener);
return this;
}
public FastTcpSocket removeListener(OnSendListener onSendListener) {
onSendListeners.remove(onSendListener);
return this;
}
public List getListeners() {
return onSendListeners;
}
public boolean close() {
if (channel == null) {
return false;
}
channel.close();
return true;
}
public boolean send(Object data) {
if (channel == null) {
return false;
}
channel.writeAndFlush(data);
for (OnSendListener onSendListener : onSendListeners) {
if (onSendListener == null) {
continue;
}
onSendListener.onSendMessage(this, data);
}
return true;
}
@Override
public String toString() {
return "FastTcpSocket{" +
"channel=" + channel +
'}';
}
public interface OnSendListener {
void onSendMessage(FastTcpSocket tcpSocket,Object message);
}
}