167 lines
4.1 KiB
Java
167 lines
4.1 KiB
Java
|
|
package com.zhangmeng.service.impl;
|
|||
|
|
|
|||
|
|
import cn.hutool.log.Log;
|
|||
|
|
import cn.hutool.log.LogFactory;
|
|||
|
|
import com.zhangmeng.handler.HandlerApi;
|
|||
|
|
import com.zhangmeng.service.Connection;
|
|||
|
|
|
|||
|
|
import java.io.*;
|
|||
|
|
import java.net.InetAddress;
|
|||
|
|
import java.net.Socket;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @author zm
|
|||
|
|
* @date 2025/2/22 11:41
|
|||
|
|
* @version: 1.0
|
|||
|
|
*/
|
|||
|
|
public class ConnectionImpl implements Connection {
|
|||
|
|
|
|||
|
|
private final Log log = LogFactory.get();
|
|||
|
|
|
|||
|
|
//当前连接的socket TCP套接字
|
|||
|
|
private Socket Conn;
|
|||
|
|
//当前连接的ID 也可以称作为SessionID,ID全局唯一
|
|||
|
|
private int ConnID;
|
|||
|
|
//当前连接的关闭状态
|
|||
|
|
private boolean isClosed;
|
|||
|
|
|
|||
|
|
//该连接的处理方法api
|
|||
|
|
private HandlerApi handleAPI;
|
|||
|
|
|
|||
|
|
//告知该链接已经退出/停止的channel
|
|||
|
|
boolean ExitBuffChan;
|
|||
|
|
|
|||
|
|
private BufferedReader reader;
|
|||
|
|
private BufferedWriter writer;
|
|||
|
|
private OutputStream outputStream;
|
|||
|
|
private InputStream inputStream;
|
|||
|
|
|
|||
|
|
|
|||
|
|
//创建连接的方法
|
|||
|
|
public ConnectionImpl(Socket conn, int connID, HandlerApi callback_api) {
|
|||
|
|
this.Conn = conn;
|
|||
|
|
this.ConnID = connID;
|
|||
|
|
this.isClosed = false;
|
|||
|
|
this.handleAPI = callback_api;
|
|||
|
|
this.ExitBuffChan = false;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
this.inputStream = conn.getInputStream();
|
|||
|
|
this.outputStream = conn.getOutputStream();
|
|||
|
|
this.reader = new BufferedReader(new InputStreamReader(inputStream));
|
|||
|
|
// this.reader = new BufferedReader(new InputStreamReader(System.in));
|
|||
|
|
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
throw new RuntimeException(e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void close() {
|
|||
|
|
try {
|
|||
|
|
reader.close();
|
|||
|
|
writer.close();
|
|||
|
|
outputStream.close();
|
|||
|
|
inputStream.close();
|
|||
|
|
Conn.close();
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
throw new RuntimeException(e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//处理conn读数据的Goroutine
|
|||
|
|
public void StartReader() {
|
|||
|
|
log.info("Reader Goroutine is running");
|
|||
|
|
boolean exit = true;
|
|||
|
|
while (exit) {
|
|||
|
|
//读取我们最大的数据到buf中
|
|||
|
|
try {
|
|||
|
|
String str = reader.readLine();
|
|||
|
|
//调用当前链接业务(这里执行的是当前conn的绑定的handle方法)
|
|||
|
|
this.handleAPI.handle(this.Conn, str.getBytes());
|
|||
|
|
// log.info("recv buf : " + str);
|
|||
|
|
writer.write(str + "\n");
|
|||
|
|
writer.flush();
|
|||
|
|
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
// throw new RuntimeException(e);
|
|||
|
|
log.error("recv buf err ", e.getMessage());
|
|||
|
|
//退出循环
|
|||
|
|
exit = false;
|
|||
|
|
this.ExitBuffChan = true;
|
|||
|
|
this.Stop();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void Start() {
|
|||
|
|
log.info("Start ConnectionImpl ..............................................");
|
|||
|
|
StartReader();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void Stop() {
|
|||
|
|
log.info("Stop ConnectionImpl ..............................................");
|
|||
|
|
//关闭当前连接
|
|||
|
|
this.close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public Socket GetTCPConnection() {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public int GetConnID() {
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public InetAddress RemoteAddr() {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public Socket getConn() {
|
|||
|
|
return Conn;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void setConn(Socket conn) {
|
|||
|
|
Conn = conn;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int getConnID() {
|
|||
|
|
return ConnID;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void setConnID(int connID) {
|
|||
|
|
ConnID = connID;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public boolean isClosed() {
|
|||
|
|
return isClosed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void setClosed(boolean closed) {
|
|||
|
|
isClosed = closed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public HandlerApi getHandleAPI() {
|
|||
|
|
return handleAPI;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void setHandleAPI(HandlerApi handleAPI) {
|
|||
|
|
this.handleAPI = handleAPI;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public boolean isExitBuffChan() {
|
|||
|
|
return ExitBuffChan;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void setExitBuffChan(boolean exitBuffChan) {
|
|||
|
|
ExitBuffChan = exitBuffChan;
|
|||
|
|
}
|
|||
|
|
}
|