public void Send(Conn conn, ProtocolBase protocol) { byte[] bytes = protocol.Encode(); byte[] length = BitConverter.GetBytes(bytes.Length); byte[] sendbuff = length.Concat(bytes).ToArray(); try { conn.socket.BeginSend(sendbuff, 0, sendbuff.Length, SocketFlags.None, null, null); } catch (Exception e) { Console.WriteLine("[发送信息]" + conn.GetAdress() + ":" + e.Message); } }
//心跳 public void HeartBeat() { //Console.WriteLine("[主定时器执行]"); long timeNow = Sys.GetTimeStamp(); for (int i = 0; i < conns.Length; i++) { Conn conn = conns[i]; if (conn == null) { return; } if (!conn.isUse) { return; } if (conn.lastTickTime < timeNow - heartBeatTime) { Console.WriteLine("[心跳引起断开连接]" + conn.GetAdress()); lock (conn) conn.Close(); } } }
public void MsgHeartBeat(Conn conn, ProtocolBase protocolBase) { conn.lastTickTime = Sys.GetTimeStamp(); Console.WriteLine("[更新心跳时间]" + conn.GetAdress()); }