Commit 5b3205ce authored by 潘栩锋's avatar 潘栩锋 🚴

修改 fobjsys 的调试

parent 0c55e17f
...@@ -38,14 +38,8 @@ ...@@ -38,14 +38,8 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.Expression.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.Expression.Drawing, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<SpecificVersion>False</SpecificVersion> <Reference Include="Microsoft.Expression.Interactions, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<HintPath>..\dll\Microsoft.Expression.Drawing.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Expression.Interactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dll\Microsoft.Expression.Interactions.dll</HintPath>
</Reference>
<Reference Include="PresentationFramework.Aero" /> <Reference Include="PresentationFramework.Aero" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
......
...@@ -108,17 +108,82 @@ namespace FObjBase ...@@ -108,17 +108,82 @@ namespace FObjBase
} }
else else
{ {
((TCPConn)s1).SendPacket(new Pack_Proto() var p = new Pack_Proto()
{ {
destid = destid, destid = destid,
srcid = srcid, srcid = srcid,
magic = magic, magic = magic,
info = info_no, info = info_no,
buf = info_data buf = info_data
}.ToBytes()); };
((TCPConn)s1).SendPacket(p.ToBytes());
if (!(s1 is TCPCConn))
{
if (loggerServer.IsDebugEnabled)
{
loggerServer.Debug($"(S) SendMessageEx{Environment.NewLine}{Pack_ProtoToString(p)}");
}
}
return; return;
} }
} }
static string BytesToHexString(byte[] buf)
{
int row = 0;
StringBuilder sb = new StringBuilder();
StringBuilder sb_hex = new StringBuilder();
StringBuilder sb_str = new StringBuilder();
sb.Append($" | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ");
sb.AppendLine();
sb.Append($"--------| -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |----------------");
sb.AppendLine();
for (int i = 0; i < buf.Count(); i++)
{
sb_hex.Append($"{buf[i]:X2} ");
char c = (char)buf[i];
if (c > 0x20 && c < 0x7e)
{
sb_str.Append(c);
}
else
{
sb_str.Append(" ");
}
if ((i + 1) % 16 == 0)
{
sb.Append($"{row * 16:X08}| {sb_hex}|{sb_str}");
row++;
sb.AppendLine();
sb_hex.Clear();
sb_str.Clear();
}
}
int remaining = buf.Count() % 16;
if (remaining > 0)
{
for (int i = 0; i < (16 - remaining); i++)
{
sb_hex.Append(" ");
sb_str.Append(" ");
}
sb.Append($"{row * 16:X08}| {sb_hex}|{sb_str}");
row++;
sb.AppendLine();
sb_hex.Clear();
sb_str.Clear();
}
return sb.ToString();
}
static string Pack_ProtoToString(Pack_Proto p)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"destid=0x{p.destid:x8}, srcid=0x{p.srcid:x8}, magic={p.magic}, info=0x{p.info:x4}, buf=");
sb.Append(BytesToHexString(p.buf));
return sb.ToString();
}
#region 服务端 #region 服务端
/// <summary> /// <summary>
...@@ -789,7 +854,7 @@ namespace FObjBase ...@@ -789,7 +854,7 @@ namespace FObjBase
TCPCConn cc = (TCPCConn)conn; TCPCConn cc = (TCPCConn)conn;
if (loggerClient.IsDebugEnabled) if (loggerClient.IsDebugEnabled)
{ {
loggerClient.Debug($"CConnConnectAction {cc.sock.RemoteEndPoint} conn.IsConnected={conn.IsConnected}"); loggerClient.Debug($"CConnConnectAction {cc.RemoteEP} conn.IsConnected={conn.IsConnected}");
} }
KeyValuePair<TCPCConn, List<UInt32>> cc_kv; KeyValuePair<TCPCConn, List<UInt32>> cc_kv;
...@@ -819,7 +884,7 @@ namespace FObjBase ...@@ -819,7 +884,7 @@ namespace FObjBase
{ {
if (conn is TCPConn) if (conn is TCPConn)
{ {
loggerServer.Debug($"SConnConnectAction {(conn as TCPConn).sock.RemoteEndPoint} IsConnected={conn.IsConnected}"); loggerServer.Debug($"SConnConnectAction IsConnected={conn.IsConnected}");
} }
} }
...@@ -1012,7 +1077,6 @@ namespace FObjBase ...@@ -1012,7 +1077,6 @@ namespace FObjBase
/// <returns></returns> /// <returns></returns>
bool ParsePacketInClient(byte[] packet, IFConn conn) bool ParsePacketInClient(byte[] packet, IFConn conn)
{ {
//DateTime dt2 = DateTime.Now;
Pack_Proto p = new Pack_Proto(); Pack_Proto p = new Pack_Proto();
if (!p.TryParse(packet)) if (!p.TryParse(packet))
return false; return false;
...@@ -1020,8 +1084,7 @@ namespace FObjBase ...@@ -1020,8 +1084,7 @@ namespace FObjBase
if (loggerClient.IsDebugEnabled) if (loggerClient.IsDebugEnabled)
{ {
string p_json = Newtonsoft.Json.JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.Indented); loggerClient.Debug($"(C) ParsePacketInClient {Environment.NewLine}{Pack_ProtoToString(p)}");
loggerClient.Debug($"ParsePacketInClient {(conn as TCPConn).sock.RemoteEndPoint} packet={p_json}");
} }
...@@ -1081,10 +1144,6 @@ namespace FObjBase ...@@ -1081,10 +1144,6 @@ namespace FObjBase
} break; } break;
} }
//DateTime dt1 = DateTime.Now;
//Console.WriteLine("FObjSys ParsePacketInClient " + "p.info=" + p.info.ToString() + " " + (dt1 - dt2).TotalMilliseconds.ToString() + " " + (dt1 - dtlast).TotalMilliseconds.ToString());
//dtlast = dt1;
return true; return true;
} }
...@@ -1100,11 +1159,7 @@ namespace FObjBase ...@@ -1100,11 +1159,7 @@ namespace FObjBase
Pack_Proto p = new Pack_Proto(); Pack_Proto p = new Pack_Proto();
if (!p.TryParse(packet)) if (!p.TryParse(packet))
return false; return false;
if (loggerServer.IsDebugEnabled)
{
string p_json = Newtonsoft.Json.JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.Indented);
loggerServer.Debug($"ParsePacketInServer {(conn as TCPConn).sock.RemoteEndPoint} packet={p_json}");
}
conn.TranID = p.magic; conn.TranID = p.magic;
{ {
if (Process(conn, p.srcid, p.destid, p.magic, p.info, p.buf, out byte[] retdata) == 0) if (Process(conn, p.srcid, p.destid, p.magic, p.info, p.buf, out byte[] retdata) == 0)
......
...@@ -22,6 +22,9 @@ namespace FObjBase ...@@ -22,6 +22,9 @@ namespace FObjBase
private DateTime conn_delay_time; private DateTime conn_delay_time;
private IPEndPoint ep = new IPEndPoint(IPAddress.Any, 20006); private IPEndPoint ep = new IPEndPoint(IPAddress.Any, 20006);
/// <summary>
/// 远端地址
/// </summary>
public IPEndPoint RemoteEP public IPEndPoint RemoteEP
{ {
get get
...@@ -35,6 +38,10 @@ namespace FObjBase ...@@ -35,6 +38,10 @@ namespace FObjBase
} }
PollModule.PollHandler onpoll_func; PollModule.PollHandler onpoll_func;
/// <summary>
///
/// </summary>
/// <param name="ipep"></param>
public TCPCConn(IPEndPoint ipep) public TCPCConn(IPEndPoint ipep)
{ {
RemoteEP = ipep; RemoteEP = ipep;
......
...@@ -11,15 +11,13 @@ namespace FObjBase ...@@ -11,15 +11,13 @@ namespace FObjBase
public delegate bool ParsePacketHandler(byte[] packet, IFConn conn); public delegate bool ParsePacketHandler(byte[] packet, IFConn conn);
public class TCPConn:IFConn public class TCPConn:IFConn
{ {
static NLog.Logger logger = NLog.LogManager.GetLogger("tcp");
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); protected List<byte> in_buffer = new List<byte>(MAX_BUFFER);
protected int packet_start;
protected int packet_len;
protected List<byte> out_buffer = new List<byte>(MAX_BUFFER); protected List<byte> out_buffer = new List<byte>(MAX_BUFFER);
protected DateTime comm_time; protected DateTime comm_time;
protected DateTime heartbeat_time = DateTime.Now; protected DateTime heartbeat_time = DateTime.Now;
...@@ -45,18 +43,21 @@ namespace FObjBase ...@@ -45,18 +43,21 @@ namespace FObjBase
out_buffer.AddRange(buffer); out_buffer.AddRange(buffer);
return len; return len;
} }
protected virtual int GetRecvInfoPacket() // return length of the packet protected virtual int GetRecvInfoPacket() // return length of the packet
{ {
int len = in_buffer.Count() - packet_start; int len = in_buffer.Count();
if (len < 2) return 0; if (len < 2) return 0;
byte[] bs = new byte[2]; byte[] bs = new byte[2];
int plen = BitConverter.ToUInt16(in_buffer.GetRange(packet_start, 2).ToArray(), 0); int plen = BitConverter.ToUInt16(in_buffer.GetRange(0, 2).ToArray(), 0);
if (plen > 0x4000*2) if (plen > 0x4000 * 2)
{ {
//包太大,不正常!!!!!! //包太大,不正常!!!!!!
//断开重新连接 //断开重新连接
if (logger.IsErrorEnabled)
logger.Error("TCPConn GetRecvInfoPacket 包太大,不正常, 断开重新连接");
sock.Close(); sock.Close();
return -1; return -1;
...@@ -69,12 +70,13 @@ namespace FObjBase ...@@ -69,12 +70,13 @@ namespace FObjBase
if (HasCRC) //TODO if (HasCRC) //TODO
{ {
UInt16 crc = Misc.CRC.CRC16(in_buffer, packet_start, plen - 2); UInt16 crc = Misc.CRC.CRC16(in_buffer, 0, plen - 2);
int packet_crc_idx = packet_start + plen - 2; int packet_crc_idx = plen - 2;
UInt16 packet_crc = BitConverter.ToUInt16(in_buffer.GetRange(packet_crc_idx, 2).ToArray(), 0); UInt16 packet_crc = BitConverter.ToUInt16(in_buffer.GetRange(packet_crc_idx, 2).ToArray(), 0);
if (crc != packet_crc) if (crc != packet_crc)
{ {
Misc.Log.LogMessage("TCPConn.cs", 1, "CRC != packet_crc"); if (logger.IsErrorEnabled)
logger.Error("TCPConn GetRecvInfoPacket CRC != packet_crc 断开重新连接");
//断开重新连接 //断开重新连接
sock.Close(); sock.Close();
return -1; return -1;
...@@ -89,20 +91,14 @@ namespace FObjBase ...@@ -89,20 +91,14 @@ namespace FObjBase
} }
return 0; return 0;
} }
protected virtual bool Parse_Packet()
protected virtual bool Parse_Packet(int len)
{ {
byte[] packet = in_buffer.GetRange(packet_start, packet_len).ToArray(); byte[] packet = in_buffer.GetRange(0, len).ToArray();
in_buffer.RemoveRange(0, len);
return ParsePacket(packet, this); return ParsePacket(packet, this);
} }
protected virtual int Clear_Packet()
{
if (packet_start > 0)
{
in_buffer.RemoveRange(0, packet_start);
}
return in_buffer.Count();
}
void Send_HeartBeat() void Send_HeartBeat()
{ {
...@@ -118,7 +114,7 @@ namespace FObjBase ...@@ -118,7 +114,7 @@ namespace FObjBase
{ {
if ((DateTime.Now - heartbeat_time) > Heartbeat_Interval) if ((DateTime.Now - heartbeat_time) > Heartbeat_Interval)
{ {
Send_HeartBeat(); Send_HeartBeat();//是时候把心跳包放入发送缓存!!!
} }
else else
return 0; return 0;
...@@ -130,10 +126,10 @@ namespace FObjBase ...@@ -130,10 +126,10 @@ namespace FObjBase
int Send_Poll() int Send_Poll()
{ {
int cnt_total = 0; int cnt_total = 0;
if (out_buffer.Count() == 0) if (out_buffer.Count() == 0)//没数据,直接返回
return 0; return 0;
while (out_buffer.Count() > 0) while (out_buffer.Count() > 0)//只有有数据发送,且能发送,没有 block, 就会一直循环
{ {
int cnt; int cnt;
try try
...@@ -142,14 +138,14 @@ namespace FObjBase ...@@ -142,14 +138,14 @@ namespace FObjBase
} }
catch (System.Net.Sockets.SocketException e) catch (System.Net.Sockets.SocketException e)
{ {
if (e.ErrorCode == 10035) if (e.SocketErrorCode == SocketError.WouldBlock)//当前发不了,退出循环,等下次!!!!
break; break;
return -1; return -1;//异常,断开连接!!!
} }
if (cnt > 0) if (cnt > 0)
{ {
out_buffer.RemoveRange(0, cnt); out_buffer.RemoveRange(0, cnt);//发送成功,删除!!!
cnt_total+=cnt; cnt_total+=cnt;
} }
else else
...@@ -157,7 +153,7 @@ namespace FObjBase ...@@ -157,7 +153,7 @@ namespace FObjBase
break; break;
} }
} }
return cnt_total; return cnt_total;//返回总发送量
} }
int Receive_Poll() int Receive_Poll()
...@@ -174,7 +170,11 @@ namespace FObjBase ...@@ -174,7 +170,11 @@ 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)
{
if (logger.IsErrorEnabled)
logger.Error(e,"TCPConn Receive_Poll 什么都收不到");
return -1; return -1;
}
else else
return reclen_total; return reclen_total;
} }
...@@ -191,7 +191,11 @@ namespace FObjBase ...@@ -191,7 +191,11 @@ 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)
{
if (logger.IsErrorEnabled)
logger.Error(e, "TCPConn Receive_Poll 什么都收不到");
return -1; return -1;
}
else else
return reclen_total; return reclen_total;
} }
...@@ -213,7 +217,6 @@ namespace FObjBase ...@@ -213,7 +217,6 @@ namespace FObjBase
first_poll = false; first_poll = false;
} }
} }
public virtual int OnPoll() public virtual int OnPoll()
{ {
int ret=0; int ret=0;
...@@ -229,30 +232,29 @@ namespace FObjBase ...@@ -229,30 +232,29 @@ namespace FObjBase
{ {
if ((DateTime.Now-comm_time) > Silent_Time) if ((DateTime.Now-comm_time) > Silent_Time)
{ {
//FDEBUG.Debug.LogMessage(this, 10, "ERROR 超时出错!"); if (logger.IsErrorEnabled)
logger.Error("TCPConn OnPoll 长时间没收到任何数据 断开连接");
ret = -2; ret = -2;
goto end; goto end;
} }
} }
else else
{ {
packet_start = 0;
while (true) while (true)
{ {
packet_len = GetRecvInfoPacket(); int packet_len = GetRecvInfoPacket();
if (packet_len > 0) if (packet_len > 0)
{ {
Parse_Packet(); Parse_Packet(packet_len);
packet_start += packet_len;
} }
else if (packet_len == 0) else if (packet_len == 0)
{ {
Clear_Packet();
break; break;
} }
else else
{ {
Clear_Packet(); //异常
ret = -1; ret = -1;
goto end; goto end;
} }
...@@ -262,15 +264,18 @@ namespace FObjBase ...@@ -262,15 +264,18 @@ namespace FObjBase
if (sendlen < 0) if (sendlen < 0)
{ {
ret = -1; ret = -1;
goto end; goto end;
} }
end: end:
if (ret != 0) if (ret != 0)
{ {
//连接断开 //连接断开,清空接收缓存区
if (logger.IsErrorEnabled)
logger.Error("TCPConn OnPoll 连接断开,清空接收缓存区");
in_buffer.Clear();
out_buffer.Clear();
Enable = false; Enable = false;
} }
return ret; return ret;
......
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