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

修改 fobjsys 的调试

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