Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
35f80508
Commit
35f80508
authored
Jan 20, 2021
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复 TCPConn,FlyAD7 的 超时判断使用 stopwatch, 避免 DateTime 的出错
parent
e5bada30
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
33 deletions
+82
-33
TCPConn.cs
Project.FLY.FObjSys/FObjSys/TCPConn.cs
+69
-15
FlyAD7.cs
Project.FLY.FlyADBase/FlyADBase/FlyAD7.cs
+13
-18
No files found.
Project.FLY.FObjSys/FObjSys/TCPConn.cs
View file @
35f80508
...
...
@@ -4,39 +4,82 @@ using System.Linq;
using
System.Text
;
using
System.Net
;
using
System.Net.Sockets
;
using
System.Diagnostics
;
namespace
FObjBase
{
/// <summary>
/// 解码
/// </summary>
/// <param name="packet"></param>
/// <param name="conn"></param>
/// <returns></returns>
public
delegate
bool
ParsePacketHandler
(
byte
[]
packet
,
IFConn
conn
);
/// <summary>
///
/// </summary>
public
class
TCPConn
:
IFConn
{
static
NLog
.
Logger
logger
=
NLog
.
LogManager
.
GetCurrentClassLogger
();
/// <summary>
/// 需要CRC校验
/// </summary>
public
bool
HasCRC
=
false
;
TimeSpan
Heartbeat_Interval
=
TimeSpan
.
FromSeconds
(
3
);
// heartbeat包发送间隔时间,3秒
TimeSpan
Silent_Time
=
TimeSpan
.
FromSeconds
(
10
);
// 单位ms没有收到任何东西的时间,10秒
const
int
MAX_BUFFER
=
20
*
0x4000
;
//20个大包, 大包的尺寸在FObjSys 定义
protected
List
<
byte
>
in_buffer
=
new
List
<
byte
>(
MAX_BUFFER
);
protected
List
<
byte
>
out_buffer
=
new
List
<
byte
>(
MAX_BUFFER
);
protected
DateTime
comm_time
;
protected
DateTime
heartbeat_time
=
DateTime
.
Now
;
List
<
byte
>
in_buffer
=
new
List
<
byte
>(
MAX_BUFFER
);
List
<
byte
>
out_buffer
=
new
List
<
byte
>(
MAX_BUFFER
);
/// <summary>
/// 通信超时判断
/// </summary>
Stopwatch
stopwatch_comm
=
new
Stopwatch
();
/// <summary>
/// 心跳包判断
/// </summary>
Stopwatch
stopwatch_heatbeat
=
new
Stopwatch
();
/// <summary>
///
/// </summary>
public
ParsePacketHandler
ParsePacket
=
null
;
/// <summary>
///
/// </summary>
public
Socket
sock
;
/// <summary>
///
/// </summary>
protected
bool
first_poll
=
true
;
/// <summary>
///
/// </summary>
public
TCPConn
()
{
}
/// <summary>
///
/// </summary>
/// <param name="sock"></param>
public
TCPConn
(
Socket
sock
)
{
this
.
sock
=
sock
;
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public
virtual
int
SendPacket
(
byte
[]
buffer
)
{
lock
(
out_buffer
)
...
...
@@ -49,6 +92,10 @@ namespace FObjBase
}
}
/// <summary>
/// 从包提取数据
/// </summary>
/// <returns></returns>
protected
virtual
int
GetRecvInfoPacket
()
// return length of the packet
{
int
len
=
in_buffer
.
Count
();
...
...
@@ -95,6 +142,11 @@ namespace FObjBase
return
0
;
}
/// <summary>
/// 解包
/// </summary>
/// <param name="len"></param>
/// <returns></returns>
protected
virtual
bool
Parse_Packet
(
int
len
)
{
byte
[]
packet
=
in_buffer
.
GetRange
(
0
,
len
).
ToArray
();
...
...
@@ -102,7 +154,9 @@ namespace FObjBase
return
ParsePacket
(
packet
,
this
);
}
/// <summary>
/// 发送心跳包
/// </summary>
void
Send_HeartBeat
()
{
byte
[]
buf
=
new
byte
[
2
];
...
...
@@ -115,15 +169,14 @@ namespace FObjBase
{
if
(
out_buffer
.
Count
()
==
0
)
{
if
(
(
DateTime
.
Now
-
heartbeat_time
)
>
Heartbeat_Interval
)
if
(
stopwatch_heatbeat
.
Elapsed
>
Heartbeat_Interval
)
{
Send_HeartBeat
();
//是时候把心跳包放入发送缓存!!!
}
else
return
0
;
}
heartbeat_time
=
DateTime
.
Now
;
stopwatch_heatbeat
.
Restart
();
return
Send_Poll
();
}
int
Send_Poll
()
...
...
@@ -144,6 +197,7 @@ namespace FObjBase
{
if
(
e
.
SocketErrorCode
==
SocketError
.
WouldBlock
)
//当前发不了,退出循环,等下次!!!!
break
;
logger
.
Error
(
e
,
"TCPConn Send_Poll 发送异常"
);
return
-
1
;
//异常,断开连接!!!
}
...
...
@@ -176,7 +230,7 @@ namespace FObjBase
//FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString());
if
(
reclen_total
==
0
)
{
logger
.
Debug
(
e
,
"TCPConn Receive_Poll 什么都收不到"
);
logger
.
Error
(
e
,
"TCPConn Receive_Poll 什么都收不到"
);
return
-
1
;
}
else
...
...
@@ -196,7 +250,7 @@ namespace FObjBase
//FDEBUG.Debug.LogMessage(this, 10, "Receive_Poll e=" + e.ToString());
if
(
reclen_total
==
0
)
{
logger
.
Debug
(
e
,
"TCPConn Receive_Poll 什么都收不到"
);
logger
.
Error
(
e
,
"TCPConn Receive_Poll 什么都收不到"
);
return
-
1
;
}
else
...
...
@@ -207,7 +261,7 @@ namespace FObjBase
in_buffer
.
AddRange
(
buf
);
reclen_total
+=
reclen
;
comm_time
=
DateTime
.
Now
;
stopwatch_comm
.
Restart
()
;
}
}
}
...
...
@@ -215,8 +269,8 @@ namespace FObjBase
{
if
(
first_poll
)
{
comm_time
=
DateTime
.
Now
;
heartbeat_time
=
DateTime
.
Now
;
stopwatch_comm
.
Restart
()
;
stopwatch_heatbeat
.
Restart
()
;
first_poll
=
false
;
}
}
...
...
@@ -233,7 +287,7 @@ namespace FObjBase
}
else
if
(
reclen
==
0
)
{
if
(
(
DateTime
.
Now
-
comm_time
)
>
Silent_Time
)
if
(
stopwatch_comm
.
Elapsed
>
Silent_Time
)
{
logger
.
Error
(
"TCPConn OnPoll 长时间没收到任何数据 断开连接"
);
...
...
Project.FLY.FlyADBase/FlyADBase/FlyAD7.cs
View file @
35f80508
...
...
@@ -9,7 +9,7 @@ using FObjBase;
using
System.IO
;
using
Misc
;
using
System.Collections.ObjectModel
;
using
System.Diagnostics
;
namespace
FlyADBase
{
...
...
@@ -66,15 +66,9 @@ namespace FlyADBase
DtAndBool
driveman_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
;
...
...
@@ -100,16 +94,18 @@ namespace FlyADBase
{
if
(!
IsConnected
)
{
noTGridCnt
=
0
;
stopwatch_noTGrid
.
Stop
()
;
}
else
{
noTGridCnt
++;
if
(
noTGridCnt
>=
ReConnectTGridMaxCnt
)
{
noTGridCnt
=
0
;
logger
.
Error
(
"60秒都无法接收到ad盒的timegrid,reconnect"
);
ReConnect
();
if
(!
stopwatch_noTGrid
.
IsRunning
)
stopwatch_noTGrid
.
Restart
();
else
{
if
(
stopwatch_noTGrid
.
Elapsed
>
NoTGridTimeOut
)
{
logger
.
Error
(
"60秒都无法接收到ad盒的timegrid,reconnect"
);
ReConnect
();
stopwatch_noTGrid
.
Restart
();
}
}
}
...
...
@@ -705,8 +701,7 @@ namespace FlyADBase
break
;
case
FLYAD7_OBJ_INTERFACE
.
PUSH_DATA_INTERFACE
.
PUSH_TIMEGRID
:
{
noTGridCnt
=
0
;
//收到timegrid!!!!!!!!!!!
stopwatch_noTGrid
.
Restart
();
//收到timegrid!!!!!!!!!!!
int
version
;
FLYAD7_OBJ_INTERFACE
.
PUSH_DATA_INTERFACE
.
Pack_PushTGrid_2
pack
=
new
FLYAD7_OBJ_INTERFACE
.
PUSH_DATA_INTERFACE
.
Pack_PushTGrid_2
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment