package com.ld.io.netty; import com.ld.io.api.InvokeResult; import com.ld.io.api.IoSession; import java.util.Date; import java.util.Objects; public class NettySession implements IoSession { private String id;// 随机唯一标识 private String businessKey;// 业务唯一标识 private String address;// ip地址 private Integer port;// 端口 private Date createTime;// 创建时间 private String companyId;// 组织编码 private NettyChannel nettyChannel; private NettySessionFactory nettySessionFactory; public String getId() { return id; } public String getBusinessKey() { return businessKey; } public void setBusinessKey(String businessKey) { this.businessKey = businessKey; } public String getAddress() { return address; } public Integer getPort() { return port; } public Date getCreateTime() { return createTime; } void setId(String id) { this.id = id; } void setAddress(String address) { this.address = address; } void setPort(Integer port) { this.port = port; } void setCreateTime(Date createTime) { this.createTime = createTime; } void setNettyChannel(NettyChannel nettyChannel) { this.nettyChannel = nettyChannel; } public NettyChannel getNettyChannel() { return nettyChannel; } public void setNettySessionFactory(NettySessionFactory nettySessionFactory) { this.nettySessionFactory = nettySessionFactory; } @Override public String getCompanyId() { return this.companyId; } @Override public void setCompanyId(String companyId) { this.companyId = companyId; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NettySession that = (NettySession) o; return Objects.equals(id, that.id); } @Override public int hashCode() { return Objects.hash(id); } public InvokeResult invoke(byte[] bytes) { if (nettyChannel == null) { return InvokeResult.SOCKET_NOT_CREATE; } InvokeResult result = nettyChannel.invoke(bytes); if (InvokeResult.CHANNEL_CLOSED.equals(result)) { this.destroy(); } return result; } public void destroy() { nettySessionFactory.destroy(this); } @Override public String toString() { return "NettySession{" + "id='" + id + '\'' + ", businessKey='" + businessKey + '\'' + ", address='" + address + '\'' + ", port=" + port + ", createTime=" + createTime + '}'; } }