public void ReceiveCommand(ReciveData dispatchCommand) { if (dispatchCommand != null) { Excute(); } }
public void SendAsynToStreamTest() { HttpWeb target = new HttpWeb(); HttpWebRequest request = null; ReciveData recive = null; target.SendAsynToStream(request, recive); Assert.Inconclusive("无法验证不返回值的方法。"); }
public void SendAsynToTextTest1() { HttpWeb target = new HttpWeb(); string url = string.Empty; ReciveData recive = null; target.SendAsynToText(url, recive); Assert.Inconclusive("无法验证不返回值的方法。"); }
public void Append(ReciveData reciveData) { if (!IsRuning) return; if (queue.IsAddingCompleted) return; if (queue.Count > 4096) { TraceManagerForCommand.AppendErrMsg( "命令消费队列到达上限无法插入"); return; } queue.Add(reciveData); }
public RequestData(HttpWebRequest request, RequestDataType type, ReciveData re) { _Request = request; _DataType = type; _ReciveData = re; if (type == RequestDataType.Text) { _BufferRead = new byte[BuffSize]; } else { _BufferRead = null; } _Stream = null; }
/// <summary> /// 命令消费 /// </summary> /// <param name="reciveData"></param> private void Excute(ReciveData reciveData) { //将接受到数据反序列化成命令对象 RequestCommand request; try { request = ByteUtil.ToDeserializeObject<RequestCommand>(reciveData.data.body); } catch (Exception e) { ResponseCommand response = new ResponseCommand() { errMsg = "序列化请求对象失败," + e.Message + "堆栈:" + e.StackTrace, statusCode = "440", info = "" }; string data = ByteUtil.ToSerializeObject(response); reciveData.FinshCallBack(reciveData.sessionID, data,true); TraceManagerForCommand.AppendErrMsg(response.errMsg); return; } // 序列化检查 if (request == null) { ResponseCommand response = new ResponseCommand() { errMsg = "序列化请求对象失败,", statusCode = "440", info = "" }; string data = ByteUtil.ToSerializeObject(response); reciveData.FinshCallBack(reciveData.sessionID, data, true); TraceManagerForCommand.AppendErrMsg(response.errMsg); return; } // 将委托和会话ID传递给命令对象 request.FinshCallBack = reciveData.FinshCallBack; request.sessionID = reciveData.sessionID; //命令记录 TraceManagerForCommand.AppendDebug("已获取命令ID:"+ request.ID); // 让调度开始分发给对应的子服务 this.DoRequestCommand(request); }
private void Recive() { while (true) { try { if (Connected && tcpclient != null) { if (CommandLenLength > 0) { byte[] datalen = new byte[CommandLenIndex + CommandLenLength]; int rnum = tcpclient.Receive(datalen, datalen.Length, SocketFlags.None); if (rnum == datalen.Length) { int dataalllen = 0; try { dataalllen = GetDataAllLength(datalen.Skip(CommandLenIndex).ToArray()); } catch { dataalllen = 0; } if (dataalllen > 0) { byte[] data = new byte[dataalllen]; int datarevcount = rnum; datalen.CopyTo(data, datarevcount - datalen.Length); do { datarevcount += tcpclient.Receive(data, datarevcount, dataalllen - datarevcount, SocketFlags.None); } while (datarevcount < dataalllen); if (ReciveData != null) { foreach (EventHandler <ReciveEventArgs> deleg in ReciveData.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveEventArgs() { Data = data }, null, null); } } if (ReciveDataHex != null) { foreach (EventHandler <ReciveHexEventArgs> deleg in ReciveDataHex.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveHexEventArgs() { DataHex = BitConverter.ToString(data).ToUpper().Trim().Replace("-", string.Empty) }, null, null); } } } } } else { byte[] datalen = new byte[CommandLenMaxLength]; int rnum = tcpclient.Receive(datalen, datalen.Length, SocketFlags.None); if (rnum > 0) { byte[] data = new byte[rnum]; Array.Copy(datalen, 0, data, 0, data.Length); if (ReciveData != null) { foreach (EventHandler <ReciveEventArgs> deleg in ReciveData.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveEventArgs() { Data = data }, null, null); } } if (ReciveDataHex != null) { foreach (EventHandler <ReciveHexEventArgs> deleg in ReciveDataHex.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveHexEventArgs() { DataHex = BitConverter.ToString(data).ToUpper().Trim().Replace("-", string.Empty) }, null, null); } } } } } else { Thread.Sleep(2000); } } catch (Exception ex) { if (ex is SocketException) { if (Connected) { if (tcpclient != null && tcpclient.Connected) { lock (lock_Connect) { tcpclient.Shutdown(SocketShutdown.Both); tcpclient.Close(); tcpclient = null; } } Connected = false; ConnectStateChange(); } } Thread.Sleep(2000); } } }
public void SendAsynToStream(HttpWebRequest request, ReciveData recive) { RequestData data = new RequestData(request, RequestDataType.Stream, recive); request.BeginGetRequestStream(new AsyncCallback(RequestCallback), data); }
public void SendAsynToText(string url, ReciveData recive) { SendAsynToText(CreateHttpWebRequest(url), recive); }
private void Recive() { while (true) { try { if (udpclient != null) { EndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0); if (CommandLenLength > 0) { byte[] datalen = new byte[CommandLenIndex + CommandLenLength]; int rnum = udpclient.ReceiveFrom(datalen, datalen.Length, SocketFlags.None, ref RemoteEndPoint); if (rnum == datalen.Length) { int dataalllen = 0; try { dataalllen = GetDataAllLength(datalen.Skip(CommandLenIndex).ToArray()); } catch { dataalllen = 0; } if (dataalllen > 0) { byte[] data = new byte[dataalllen]; int datarevcount = rnum; datalen.CopyTo(data, datarevcount - datalen.Length); do { datarevcount += udpclient.ReceiveFrom(data, datarevcount, dataalllen - datarevcount, SocketFlags.None, ref RemoteEndPoint); } while (datarevcount < dataalllen); if (ReciveData != null) { foreach (EventHandler <ReciveClientEventArgs> deleg in ReciveData.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveClientEventArgs() { RemoteEndPoint = RemoteEndPoint, Data = data }, null, null); } } if (ReciveDataHex != null) { foreach (EventHandler <ReciveClientHexEventArgs> deleg in ReciveDataHex.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveClientHexEventArgs() { RemoteEndPoint = RemoteEndPoint, DataHex = BitConverter.ToString(data).ToUpper().Trim().Replace("-", string.Empty) }, null, null); } } } } } else { byte[] datalen = new byte[CommandLenMaxLength]; int rnum = udpclient.ReceiveFrom(datalen, datalen.Length, SocketFlags.None, ref RemoteEndPoint); if (rnum > 0) { byte[] data = new byte[rnum]; Array.Copy(datalen, 0, data, 0, data.Length); if (ReciveData != null) { foreach (EventHandler <ReciveClientEventArgs> deleg in ReciveData.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveClientEventArgs() { RemoteEndPoint = RemoteEndPoint, Data = data }, null, null); } } if (ReciveDataHex != null) { foreach (EventHandler <ReciveClientHexEventArgs> deleg in ReciveDataHex.GetInvocationList()) { deleg.BeginInvoke(this, new ReciveClientHexEventArgs() { RemoteEndPoint = RemoteEndPoint, DataHex = BitConverter.ToString(data).ToUpper().Trim().Replace("-", string.Empty) }, null, null); } } } } } else { Thread.Sleep(2000); } } catch (Exception ex) { Thread.Sleep(2000); } } }
public void Parse(byte[] array, int srcOffset, int reciveLength) { if (_buffs != null) { byte[] tempBytes = new byte[_buffs.Length + reciveLength]; Buffer.BlockCopy(_buffs, 0, tempBytes, 0, _buffs.Length); Buffer.BlockCopy(array, srcOffset, tempBytes, _buffs.Length, reciveLength); array = tempBytes; _buffs = null; srcOffset = 0; reciveLength = tempBytes.Length; } if (reciveLength < 2) { Fill(array, srcOffset, reciveLength); return; } if (reciveLength > 8388608)//最大值也是8388608bytes(8M) { throw new Exception("WebSocket Error: Recvive Data Too Long..."); } int index = srcOffset; //第一个字节 最高位用于描述消息是否结束,如果为1则该消息为消息尾部,如果为零则还有后续数据包 int firstByte = array[0] >> 4;//向右移动四位 array[0]=128 if (firstByte != 8 && firstByte != 0) { IsEof = (true); return; } bool flag = firstByte == 8; int num5 = array[index] & 15;//00001111 1111 0000 0001 0011 0111 1111 0010 0111 0110 0101 清掉X的高4位,而保留 低 4位 最低4位用于描述消息类型,消息类型暂定有15种 index++; bool flag2 = array[index] >> 7 > 0; ushort num6 = (ushort)(array[index] & 127); //datalength index++; //如果 == 126 start = start + 2 == 127 start = start + 8 ulong num7; if (num6 < 126) { num7 = num6; } else { if (num6 != 126) { abool = (true); IsEof = (true); return; } if (Nokori(srcOffset, reciveLength, index) < 2) { Fill(array, srcOffset, reciveLength); return; } num7 = (ulong)((array[index] << 8) + array[index + 1]); index += 2; } byte[] array3 = null; if (flag2)//是否有 第二位 masks 如果存在掩码的情况下获取4位掩码值: { if (Nokori(srcOffset, reciveLength, index) < 4) { Fill(array, srcOffset, reciveLength); return; } array3 = new byte[4]; Buffer.BlockCopy(array, index, array3, 0, 4); index += 4; } if (Nokori(srcOffset, reciveLength, index) < (long)num7) { _buffs = new byte[reciveLength]; Buffer.BlockCopy(array, srcOffset, _buffs, 0, reciveLength);//数据分批读取 return; } byte[] array4 = new byte[num7]; Buffer.BlockCopy(array, index, array4, 0, (int)num7); index += (int)num7; if (flag2) { for (int i = 0; i < array4.Length; i++) { array4[i] ^= array3[i % 4];//获取消息体 } } if (num5 == 8) //1000 { IsEof = (true); } if (num5 == 9) // 1001 { IsEof = (true); return; } if (num5 != 10) //1010 { lock (_reciveDataQueue) { ReciveData item = new ReciveData { Data = array4, IsEof = flag, BytesLength = (int)num7, Type = num5 }; _reciveDataQueue.Enqueue(item); } } int num8 = Nokori(srcOffset, reciveLength, index); if (num8 < 1) { return; } Parse(array, index, num8); }