public override DataEventArgs GetResult(DataEventArgs e, ChannelPool channel) { try { if (channel.Client == null || channel.Client.Connected != true) { channel.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); channel.Client.Connect(channel.IpPoint); channel.Available = true; } byte[] _sbuffer = e.ToByteArray(); channel.Client.Send(_sbuffer, _sbuffer.Length, SocketFlags.None); ByteBuilder builder = new ByteBuilder(1); byte[] _recbuff = new byte[2048]; int i = channel.Client.Receive(_recbuff, 2048, SocketFlags.None); builder.Add(_recbuff.Take(i).ToArray()); int total = builder.GetInt32(0); while (channel.Client.Available > 0) { i = channel.Client.Receive(_recbuff, 2048, SocketFlags.None); builder.Add(_recbuff, 0, i); } while (total > builder.Count) { i = channel.Client.Receive(_recbuff, 2048, SocketFlags.None); builder.Add(_recbuff, 0, i); } if (total == builder.Count) { DataEventArgs d = DataEventArgs.Parse(builder); d.StatusCode = StatusCode.Success; Channels[e.CallHashCode].ActiveHash = 0; return(d); } Channels[e.CallHashCode].ActiveHash = 0; e.StatusCode = StatusCode.TimeOut; return(e); } catch (SocketException se) { foreach (ChannelPool p in Channels) { if (p.IpPoint == channel.IpPoint) { p.Available = false; } } e.StatusCode = StatusCode.Error; return(e); } catch (Exception ex) { Console.WriteLine(ex); e.StatusCode = StatusCode.Error; return(e); } }
/// <summary> /// 解析数据包 /// </summary> /// <param name="recvBuilder">接收到的历史数据</param> /// <returns></returns> public static DataEventArgs Parse(ByteBuilder recvBuilder) { int count = recvBuilder.Count; int total = recvBuilder.GetInt32(0); // 数据一定要大于等于固定长度 包长小于等于数据长度 if (recvBuilder.Count >= ConstLength && recvBuilder.GetInt32(0) <= recvBuilder.Count) { // 包长 int totalLength = recvBuilder.ReadInt32(); // cmdLength int cmdLength = recvBuilder.ReadInt32(); int paramLength = recvBuilder.ReadInt32(); int idLength = recvBuilder.ReadInt32(); int errLength = recvBuilder.ReadInt32(); // 哈希值 int hashCode = recvBuilder.ReadInt32(); int taskId = recvBuilder.ReadInt32(); int statusCode = recvBuilder.ReadInt32(); int tryTimes = recvBuilder.ReadInt32(); int ipLength = recvBuilder.ReadInt32(); int pavalLength = recvBuilder.ReadInt32(); String cmd = Encoding.UTF8.GetString(recvBuilder.ReadRange(cmdLength), 0, cmdLength); String param = Encoding.UTF8.GetString(recvBuilder.ReadRange(paramLength), 0, paramLength); String id = Encoding.UTF8.GetString(recvBuilder.ReadRange(idLength), 0, idLength); byte[] errBinary = new Binary(recvBuilder.ReadRange(errLength)).Buffer; byte[] ipBinary = new Binary(recvBuilder.ReadRange(ipLength)).Buffer; string ipAddress = string.Empty; if (ipBinary.Length > 0) { ipAddress = Encoding.UTF8.GetString(ipBinary); } string lastError = string.Empty; if (errBinary.Length > 0) { lastError = Encoding.UTF8.GetString(errBinary); } ArrayList Param = new JsonSerializer().ToEntity <ArrayList>(Encoding.UTF8.GetString(recvBuilder.ReadRange(pavalLength))); // 实体长 int entityLength = totalLength - ConstLength - cmdLength - paramLength - idLength - errLength - ipLength - pavalLength; // 实体数据 Binary entityBinary = new Binary(recvBuilder.ReadRange(entityLength)); // 校验长 int checkLength = recvBuilder.ReadInt32(); // 检验数据 if (totalLength == checkLength) { // 返回数据事件包给发送者 return(new DataEventArgs() { Binary = entityBinary, Param = Param, LastError = lastError, RemoteIpAddress = ipAddress, TryTimes = tryTimes, StatusCode = (StatusCode)statusCode, TaskId = taskId, ActionCmd = cmd, CallHashCode = hashCode, ActionParam = param, HttpSessionId = id }); } else { Console.WriteLine("无效包 包转换失败 清除"); _log.Error("无效包 包转换失败 清除"); // 无效包 清除 recvBuilder.Clear(); return(new DataEventArgs() { Binary = null, LastError = "包转换失败", ActionCmd = CallActionCmd.Call.ToString(), StatusCode = StatusCode.Error, TaskId = 9999, CallHashCode = hashCode, ActionParam = param, HttpSessionId = id }); } } else { Console.WriteLine("空包 网络丢失 清除"); _log.Error("空包 网络丢失 清除"); recvBuilder.Clear(); return(new DataEventArgs() { Binary = null, LastError = "网络丢失", StatusCode = StatusCode.Error, ActionCmd = CallActionCmd.Call.ToString() }); } }