internal SmmpPacket DataLoop(byte data, int clientID) { _byteCache.Add(data); string s = DataEncoding.GetString((byte[])_byteCache.ToArray(typeof(byte))); if (!_protocol.Received) { SmmpProtocalHeader pheader = _protocol.IsProtocolHeader(ref _byteCache); if (pheader != null) { _savedHeaderInfo = pheader; _protocol.Received = true; } } else if (_protocol.Received) { SmmpPacket packet = _protocol.ReadFormSmProtocol(ref _byteCache, ref _savedHeaderInfo); if (packet != null) { onReceivedData(_localTcpStruct, _remoteTcpStruct, packet, null); _protocol.Received = false; _byteCache.Clear(); } return(packet); } return(null); }
/// <summary> /// 向对方发送数据 /// </summary> /// <param name="byteData">数据内容</param> /// <param name="clientID">连接标识</param> public void SendData(byte[] byteData) { SmmpPacket packet = new SmmpPacket(new Random().Next(1, 999999), -1); packet.MakeByteData(byteData); SendData(packet); }
void _readByBlockInReadMethodHasTimeout() { if (!UseBlockRead) { return; } if (readTimeout) { return; } try { int bit = 0; bool SmmpIsNull = true; while ((bit = _ns.ReadByte()) != -1 && SmmpIsNull) { _blockReadBufferField = DataLoop((byte)bit, -1); if (_blockReadBufferField != null) { SmmpIsNull = false; } } } catch { return; } }
/// <summary> /// 在使用阻断模式的时候读取列队和缓存中的所有数据。 /// </summary> /// <returns></returns> public SmmpPacket Read() { if (!UseBlockRead) { return(null); } SmmpPacket result = null; try { int bit = 0; bool SmmpIsNull = true; while ((bit = _ns.ReadByte()) != -1 && SmmpIsNull) { _blockReadBufferField = DataLoop((byte)bit, -1); if (_blockReadBufferField != null) { SmmpIsNull = false; result = _blockReadBufferField; _blockReadBufferField = null; } } } catch { return(null); } return(result); }
/// <summary> /// 向对方发送数据 /// </summary> /// <param name="byteData">数据内容</param> /// <param name="clientID">连接标识</param> public void SendData(SmmpPacket packet, int clientID) { if (_tcpMode == TcpMode.Client) { if (!Connected) { onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, null); return; } byte[] bytePackect = packet.StartMakeup(); _ns.Write(bytePackect, 0, bytePackect.Length); } else if (_tcpMode == TcpMode.Server) { __listen__readSmmp pclinet = GetListenClient(clientID); if (pclinet != null) { pclinet.SendData(packet); } else { onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, pclinet); } } else { onError(_localTcpStruct, _remoteTcpStruct, TcpError.UnknownModeOrNotConnected, null, TcpOptionType.SendData, null); } }
private unsafe void DataLoop(byte data, int clientID) { _byteCache.Add(data); if (!_protocol.Received) { _savedHeaderInfo = _protocol.IsProtocolHeader(ref _byteCache); if (_savedHeaderInfo != null) { _protocol.Received = true; } else { _protocol.Received = false; } } else if (_protocol.Received) { SmmpPacket packet = _protocol.ReadFormSmProtocol(ref _byteCache, ref _savedHeaderInfo); if (packet != null) { onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, packet, (byte[])_byteCache.ToArray(typeof(byte))); _protocol.Received = false; _byteCache.Clear(); } } }
private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, byte[] rawData) { if (OnReceivedData != null) { OnReceivedData(localTcpInfo, remoteTcpInfo, packet, this); } _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, packet, this); }
/// <summary> /// 根据SM协议头保存的状态读取数据 /// </summary> /// <param name="packetBuffer">在状态缓存中的数据</param> /// <param name="headerInfo">协议头信息</param> /// <returns></returns> public SmmpPacket ReadFormSmProtocol(ref ArrayList packetBuffer, ref SmmpProtocalHeader headerInfo) { byte[] bytes = (byte[])packetBuffer.ToArray(typeof(byte)); if (bytes.Length >= headerInfo.MessagesBytes && contentLength == 0) { byte[] headerMessageBytes = new byte[headerInfo.MessagesBytes]; Memory.Memory.MemCpy(ref headerMessageBytes, ref bytes); string[] messageLines = Encoding.Default.GetString(headerMessageBytes).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); packet = new SmmpPacket(headerInfo.MessageID, headerInfo.ResponseID); packet.Messages = new NameValueCollection(); foreach (string messageLine in messageLines) { string[] messageSplited = messageLine.Split(new string[] { ":" }, 2, StringSplitOptions.None); if (messageSplited.Length == 2) { packet.Messages.Add(messageSplited[0], messageSplited[1]); } } MessageRead = true; packetBuffer.Clear(); if (packet.Messages.Count == 0) { return(null); } if (int.TryParse(packet.Messages["ContentLength"], out contentLength)) { if (contentLength != 0) { return(null); } } Received = false; packet.RawData = bytes; return(packet); } else if (bytes.Length >= contentLength && contentLength > 0) { if (packet == null) { return(null); } packet.ContentBuffer = new byte[contentLength]; for (int i = 0; i < contentLength; i++) { packet.ContentBuffer[i] = bytes[i]; } packetBuffer.Clear(); Received = false; contentLength = 0; packet.RawData = bytes; return(packet); } return(null); }
/// <summary> /// 向对方发送数据 /// </summary> /// <SmmpPacket name="byteData">数据内容</param> public void SendData(SmmpPacket packet) { if (!_tc.Connected) { _tcp.onError(_tcp._localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, this); return; } byte[] bytePackect = packet.StartMakeup(); _ns.Write(bytePackect, 0, bytePackect.Length); }
/// <summary> /// 从接受到的SMMP数据包创建新的响应数据包。 /// </summary> /// <param name="fromRecv">接受到的数据包</param> /// <param name="ID"></param> /// <returns></returns> public static SmmpPacket CreateNew(SmmpPacket fromRecv, int ID = -1) { if (ID == -1) { ID = new Random().Next(0, 99999); } SmmpPacket packet = new SmmpPacket(ID, fromRecv.MessageID, true); return(packet); }
/// <summary> /// 在使用阻断模式的时候读取列队和缓存中的所有数据。 /// </summary> /// <returns></returns> public SmmpPacket Read(int timeoutSpan, int totalCount) { SmmpPacket result = null; Threads.ExecAsync(_readByBlockInReadMethodHasTimeout); int count = 0; while (_blockReadBufferField == null && count < totalCount) { Thread.Sleep(timeoutSpan); count++; } if (_blockReadBufferField == null) { readTimeout = true; } else { readTimeout = false; result = _blockReadBufferField; _blockReadBufferField = null; } return(result); }
/// <summary> /// �ӽ��ܵ���SMMP���ݰ������µ���Ӧ���ݰ��� /// </summary> /// <param name="fromRecv">���ܵ������ݰ�</param> /// <param name="ID"></param> /// <returns></returns> public static SmmpPacket CreateNew(SmmpPacket fromRecv, int ID = -1) { if (ID == -1) ID = new Random().Next(0, 99999); SmmpPacket packet = new SmmpPacket(ID, fromRecv.MessageID, true); return packet; }
/// <summary> /// ����SMЭ��ͷ�����״̬��ȡ���� /// </summary> /// <param name="packetBuffer">��״̬�����е�����</param> /// <param name="headerInfo">Э��ͷ��Ϣ</param> /// <returns></returns> public SmmpPacket ReadFormSmProtocol(ref ArrayList packetBuffer, ref SmmpProtocalHeader headerInfo) { byte[] bytes = (byte[])packetBuffer.ToArray(typeof(byte)); if (bytes.Length >= headerInfo.MessagesBytes && contentLength == 0) { byte[] headerMessageBytes = new byte[headerInfo.MessagesBytes]; Memory.Memory.MemCpy(ref headerMessageBytes, ref bytes); string[] messageLines = Encoding.Default.GetString(headerMessageBytes).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); packet = new SmmpPacket(headerInfo.MessageID, headerInfo.ResponseID); packet.Messages = new NameValueCollection(); foreach (string messageLine in messageLines) { string[] messageSplited = messageLine.Split(new string[] { ":" }, 2, StringSplitOptions.None); if (messageSplited.Length == 2) packet.Messages.Add(messageSplited[0], messageSplited[1]); } MessageRead = true; packetBuffer.Clear(); if (packet.Messages.Count == 0) return null; if (int.TryParse(packet.Messages["ContentLength"], out contentLength)) { if (contentLength != 0) return null; } Received = false; packet.RawData = bytes; return packet; } else if (bytes.Length >= contentLength && contentLength > 0) { if (packet == null) return null; packet.ContentBuffer = new byte[contentLength]; for (int i = 0; i < contentLength; i++) packet.ContentBuffer[i] = bytes[i]; packetBuffer.Clear(); Received = false; contentLength = 0; packet.RawData = bytes; return packet; } return null; }
void _readByBlockInReadMethodHasTimeout() { if (!UseBlockRead) return; if (readTimeout) return; try { int bit = 0; bool SmmpIsNull = true; while ((bit = _ns.ReadByte()) != -1 && SmmpIsNull) { _blockReadBufferField = DataLoop((byte)bit, -1); if (_blockReadBufferField != null) SmmpIsNull = false; } } catch { return; } }
internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, __listen__readSmmp tcpReader) { if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, packet, tcpReader); }
/// <summary> /// ��Է��������� /// </summary> /// <param name="byteData">��������</param> /// <param name="clientID">���ӱ�ʶ</param> public void SendData(SmmpPacket packet, int clientID) { if (_tcpMode == TcpMode.Client) { if (!Connected) { onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, null); return; } byte[] bytePackect = packet.StartMakeup(); _ns.Write(bytePackect, 0, bytePackect.Length); } else if (_tcpMode == TcpMode.Server) { __listen__readSmmp pclinet = GetListenClient(clientID); if (pclinet != null) pclinet.SendData(packet); else onError(_localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, pclinet); } else onError(_localTcpStruct, _remoteTcpStruct, TcpError.UnknownModeOrNotConnected, null, TcpOptionType.SendData, null); }
/// <summary> /// ��Է��������ݣ����ڿͻ������� /// </summary> /// <param name="byteData">��������</param> /// <param name="clientID">���ӱ�ʶ</param> public void SendData(byte[] byteData, int clientID) { SmmpPacket packet = new SmmpPacket(new Random().Next(1, 999999), -1); packet.MakeByteData(byteData); SendData(packet, clientID); }
/// <summary> /// ��Է��������� /// </summary> /// <param name="packet">SMMP���ݰ�</param> public void SendData(SmmpPacket packet) { SendData(packet, -1); }
/// <summary> /// ��ʹ�����ģʽ��ʱ���ȡ�жӺͻ����е��������ݡ� /// </summary> /// <returns></returns> public SmmpPacket Read(int timeoutSpan, int totalCount) { SmmpPacket result = null; Threads.ExecAsync(_readByBlockInReadMethodHasTimeout); int count = 0; while (_blockReadBufferField == null && count < totalCount) { Thread.Sleep(timeoutSpan); count++; } if (_blockReadBufferField == null) readTimeout = true; else { readTimeout = false; result = _blockReadBufferField; _blockReadBufferField = null; } return result; }
/// <summary> /// ��ʹ�����ģʽ��ʱ���ȡ�жӺͻ����е��������ݡ� /// </summary> /// <returns></returns> public SmmpPacket Read() { if (!UseBlockRead) return null; SmmpPacket result = null; try { int bit = 0; bool SmmpIsNull = true; while ((bit = _ns.ReadByte()) != -1 && SmmpIsNull) { _blockReadBufferField = DataLoop((byte)bit, -1); if (_blockReadBufferField != null) { SmmpIsNull = false; result = _blockReadBufferField; _blockReadBufferField = null; } } } catch { return null; } return result; }
private void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, byte[] rawData) { if (OnReceivedData != null) OnReceivedData(localTcpInfo, remoteTcpInfo, packet, this); _tcp.onReceivedData(_tcp._localTcpStruct, _remoteTcpStruct, packet, this); }
/// <summary> /// ��Է��������� /// </summary> /// <SmmpPacket name="byteData">��������</param> public void SendData(SmmpPacket packet) { if (!_tc.Connected) { _tcp.onError(_tcp._localTcpStruct, _remoteTcpStruct, TcpError.TcpClientNotConnected, null, TcpOptionType.SendData, this); return; } byte[] bytePackect = packet.StartMakeup(); _ns.Write(bytePackect, 0, bytePackect.Length); }
/// <summary> /// 向对方发送数据 /// </summary> /// <param name="packet">SMMP数据包</param> public void SendData(SmmpPacket packet) { SendData(packet, -1); }
internal void onReceivedData(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, SmmpPacket packet, __listen__readSmmp tcpReader) { if (OnReceivedData != null) { OnReceivedData(localTcpInfo, remoteTcpInfo, packet, tcpReader); } }