Commit 35f80508 authored by 潘栩锋's avatar 潘栩锋 :bicyclist:

修复 TCPConn,FlyAD7 的 超时判断使用 stopwatch, 避免 DateTime 的出错

parent e5bada30
...@@ -4,39 +4,82 @@ using System.Linq; ...@@ -4,39 +4,82 @@ using System.Linq;
using System.Text; using System.Text;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Diagnostics;
namespace FObjBase namespace FObjBase
{ {
/// <summary>
/// 解码
/// </summary>
/// <param name="packet"></param>
/// <param name="conn"></param>
/// <returns></returns>
public delegate bool ParsePacketHandler(byte[] packet, IFConn conn); public delegate bool ParsePacketHandler(byte[] packet, IFConn conn);
/// <summary>
///
/// </summary>
public class TCPConn:IFConn public class TCPConn:IFConn
{ {
static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// 需要CRC校验
/// </summary>
public bool HasCRC = false; public bool HasCRC = false;
TimeSpan Heartbeat_Interval = TimeSpan.FromSeconds(3); // heartbeat包发送间隔时间,3秒 TimeSpan Heartbeat_Interval = TimeSpan.FromSeconds(3); // heartbeat包发送间隔时间,3秒
TimeSpan Silent_Time = TimeSpan.FromSeconds(10); // 单位ms没有收到任何东西的时间,10秒 TimeSpan Silent_Time = TimeSpan.FromSeconds(10); // 单位ms没有收到任何东西的时间,10秒
const int MAX_BUFFER = 20 * 0x4000;//20个大包, 大包的尺寸在FObjSys 定义 const int MAX_BUFFER = 20 * 0x4000;//20个大包, 大包的尺寸在FObjSys 定义
protected List<byte> in_buffer = new List<byte>(MAX_BUFFER); List<byte> in_buffer = new List<byte>(MAX_BUFFER);
protected List<byte> out_buffer = new List<byte>(MAX_BUFFER); List<byte> out_buffer = new List<byte>(MAX_BUFFER);
protected DateTime comm_time;
protected DateTime heartbeat_time = DateTime.Now; /// <summary>
/// 通信超时判断
/// </summary>
Stopwatch stopwatch_comm = new Stopwatch();
/// <summary>
/// 心跳包判断
/// </summary>
Stopwatch stopwatch_heatbeat = new Stopwatch();
/// <summary>
///
/// </summary>
public ParsePacketHandler ParsePacket = null; public ParsePacketHandler ParsePacket = null;
/// <summary>
///
/// </summary>
public Socket sock; public Socket sock;
/// <summary>
///
/// </summary>
protected bool first_poll=true; protected bool first_poll=true;
/// <summary>
///
/// </summary>
public TCPConn() public TCPConn()
{ {
} }
/// <summary>
///
/// </summary>
/// <param name="sock"></param>
public TCPConn(Socket sock) public TCPConn(Socket sock)
{ {
this.sock = sock; this.sock = sock;
} }
/// <summary>
/// 发送数据
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public virtual int SendPacket(byte[] buffer) public virtual int SendPacket(byte[] buffer)
{ {
lock (out_buffer) lock (out_buffer)
...@@ -49,6 +92,10 @@ namespace FObjBase ...@@ -49,6 +92,10 @@ namespace FObjBase
} }
} }
/// <summary>
/// 从包提取数据
/// </summary>
/// <returns></returns>
protected virtual int GetRecvInfoPacket() // return length of the packet protected virtual int GetRecvInfoPacket() // return length of the packet
{ {
int len = in_buffer.Count(); int len = in_buffer.Count();
...@@ -95,6 +142,11 @@ namespace FObjBase ...@@ -95,6 +142,11 @@ namespace FObjBase
return 0; return 0;
} }
/// <summary>
/// 解包
/// </summary>
/// <param name="len"></param>
/// <returns></returns>
protected virtual bool Parse_Packet(int len) protected virtual bool Parse_Packet(int len)
{ {
byte[] packet = in_buffer.GetRange(0, len).ToArray(); byte[] packet = in_buffer.GetRange(0, len).ToArray();
...@@ -102,7 +154,9 @@ namespace FObjBase ...@@ -102,7 +154,9 @@ namespace FObjBase
return ParsePacket(packet, this); return ParsePacket(packet, this);
} }
/// <summary>
/// 发送心跳包
/// </summary>
void Send_HeartBeat() void Send_HeartBeat()
{ {
byte[] buf = new byte[2]; byte[] buf = new byte[2];
...@@ -115,15 +169,14 @@ namespace FObjBase ...@@ -115,15 +169,14 @@ namespace FObjBase
{ {
if (out_buffer.Count() == 0) if (out_buffer.Count() == 0)
{ {
if ((DateTime.Now - heartbeat_time) > Heartbeat_Interval) if (stopwatch_heatbeat.Elapsed > Heartbeat_Interval)
{ {
Send_HeartBeat();//是时候把心跳包放入发送缓存!!! Send_HeartBeat();//是时候把心跳包放入发送缓存!!!
} }
else else
return 0; return 0;
} }
stopwatch_heatbeat.Restart();
heartbeat_time = DateTime.Now;
return Send_Poll(); return Send_Poll();
} }
int Send_Poll() int Send_Poll()
...@@ -144,6 +197,7 @@ namespace FObjBase ...@@ -144,6 +197,7 @@ namespace FObjBase
{ {
if (e.SocketErrorCode == SocketError.WouldBlock)//当前发不了,退出循环,等下次!!!! if (e.SocketErrorCode == SocketError.WouldBlock)//当前发不了,退出循环,等下次!!!!
break; break;
logger.Error(e, "TCPConn Send_Poll 发送异常");
return -1;//异常,断开连接!!! return -1;//异常,断开连接!!!
} }
...@@ -176,7 +230,7 @@ namespace FObjBase ...@@ -176,7 +230,7 @@ namespace FObjBase
//FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString()); //FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString());
if (reclen_total == 0) if (reclen_total == 0)
{ {
logger.Debug(e,"TCPConn Receive_Poll 什么都收不到"); logger.Error(e,"TCPConn Receive_Poll 什么都收不到");
return -1; return -1;
} }
else else
...@@ -196,7 +250,7 @@ namespace FObjBase ...@@ -196,7 +250,7 @@ namespace FObjBase
//FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString()); //FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString());
if (reclen_total == 0) if (reclen_total == 0)
{ {
logger.Debug(e, "TCPConn Receive_Poll 什么都收不到"); logger.Error(e, "TCPConn Receive_Poll 什么都收不到");
return -1; return -1;
} }
else else
...@@ -207,7 +261,7 @@ namespace FObjBase ...@@ -207,7 +261,7 @@ namespace FObjBase
in_buffer.AddRange(buf); in_buffer.AddRange(buf);
reclen_total += reclen; reclen_total += reclen;
comm_time = DateTime.Now; stopwatch_comm.Restart();
} }
} }
} }
...@@ -215,8 +269,8 @@ namespace FObjBase ...@@ -215,8 +269,8 @@ namespace FObjBase
{ {
if (first_poll) if (first_poll)
{ {
comm_time = DateTime.Now; stopwatch_comm.Restart();
heartbeat_time = DateTime.Now; stopwatch_heatbeat.Restart();
first_poll = false; first_poll = false;
} }
} }
...@@ -233,7 +287,7 @@ namespace FObjBase ...@@ -233,7 +287,7 @@ namespace FObjBase
} }
else if (reclen == 0) else if (reclen == 0)
{ {
if ((DateTime.Now-comm_time) > Silent_Time) if (stopwatch_comm.Elapsed > Silent_Time)
{ {
logger.Error("TCPConn OnPoll 长时间没收到任何数据 断开连接"); logger.Error("TCPConn OnPoll 长时间没收到任何数据 断开连接");
......
...@@ -9,7 +9,7 @@ using FObjBase; ...@@ -9,7 +9,7 @@ using FObjBase;
using System.IO; using System.IO;
using Misc; using Misc;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics;
namespace FlyADBase namespace FlyADBase
{ {
...@@ -66,15 +66,9 @@ namespace FlyADBase ...@@ -66,15 +66,9 @@ namespace FlyADBase
DtAndBool driveman_wait = new DtAndBool(); DtAndBool driveman_wait = new DtAndBool();
DtAndBool sysinfo_wait = new DtAndBool(); DtAndBool sysinfo_wait = new DtAndBool();
/// <summary>
/// 没有收到timegrid的次数, 1秒加1次
/// </summary>
int noTGridCnt = 0;
/// <summary>
/// 60秒收不到timegrid, 重连
/// </summary>
const int ReConnectTGridMaxCnt = 60;
TimeSpan NoTGridTimeOut = TimeSpan.FromSeconds(60);
Stopwatch stopwatch_noTGrid = new Stopwatch();
int last_position = int.MinValue; int last_position = int.MinValue;
...@@ -100,16 +94,18 @@ namespace FlyADBase ...@@ -100,16 +94,18 @@ namespace FlyADBase
{ {
if (!IsConnected) if (!IsConnected)
{ {
noTGridCnt = 0; stopwatch_noTGrid.Stop();
} }
else else
{ {
noTGridCnt++; if (!stopwatch_noTGrid.IsRunning)
if (noTGridCnt >= ReConnectTGridMaxCnt) stopwatch_noTGrid.Restart();
{ else {
noTGridCnt = 0; if (stopwatch_noTGrid.Elapsed > NoTGridTimeOut) {
logger.Error("60秒都无法接收到ad盒的timegrid,reconnect"); logger.Error("60秒都无法接收到ad盒的timegrid,reconnect");
ReConnect(); ReConnect();
stopwatch_noTGrid.Restart();
}
} }
} }
...@@ -705,8 +701,7 @@ namespace FlyADBase ...@@ -705,8 +701,7 @@ namespace FlyADBase
break; break;
case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID: case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID:
{ {
noTGridCnt = 0;//收到timegrid!!!!!!!!!!! stopwatch_noTGrid.Restart();//收到timegrid!!!!!!!!!!!
int version; int version;
FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2(); FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment