private void BeginRead(IAsyncResult ar) { int read = pipe.EndRead(ar); if (read == 0) { return; } int length = BitConverter.ToInt16(buffer, 0) + 2; if (length <= 2) { return; } byte[] received = new byte[length]; Array.Copy(buffer, received, 2); int pos = 2; while (length - pos > 0) { pos += pipe.Read(received, pos, length - pos); } // Call OnReceive asynchronously if (OnReceive != null) { OnReceive.BeginInvoke(new NetworkMessage(client, received, read), null, null); } PipePacketType type = (PipePacketType)received[2]; switch (type) { case PipePacketType.OnClickContextMenu: if (OnContextMenuClick != null) { if (length == 7) { OnContextMenuClick.BeginInvoke(new NetworkMessage(client, received, length), null, null); } } break; case PipePacketType.HookReceivedPacket: if (OnSocketRecv != null) { OnSocketRecv.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; case PipePacketType.HookSentPacket: if (OnSocketSend != null) { OnSocketSend.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; case PipePacketType.OnClickIcon: if (OnIconClick != null) { OnIconClick.BeginInvoke(new NetworkMessage(client, received, length), null, null); } break; default: break; } pipe.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(BeginRead), null); }
/// <summary> /// Start building a packet of the desired type. /// </summary> /// <param name="type"></param> public PipePacketBuilder(ConnectionProvider connection, PipePacketType type) : this(connection) { Type = type; index++; }
/// <summary> /// Parses the data. /// </summary> /// <param name="packet">The packet.</param> /// <returns></returns> public bool ParseData(byte[] packet) { data = packet; if (data.Length > 3) { pipetype = (PipePacketType)data[2]; specifiedLength = BitConverter.ToInt16(Data, 0); return true; } else { return false; } }
private void BeginRead(IAsyncResult ar) { NamedPipeServerStream pipe = ar.AsyncState as NamedPipeServerStream; int length = pipe.EndRead(ar); try { if (length > 1) { byte[] tmpBytes = new byte[length]; Array.Copy(buffer, 0, tmpBytes, 0, length); NetworkMessage msg = new NetworkMessage(tmpBytes); msg.Position = 0; UInt16 packLen = msg.GetUInt16(); PipePacketType type = (PipePacketType)msg.GetByte(); switch (type) { case PipePacketType.RecivedOutgoingPacket: byte[] buf = new byte[msg.Data.Length - 3]; Array.Copy(msg.Data, 3, buf, 0, buf.Length); HookProxy_OutgoingPacket(buf); break; case PipePacketType.RecivedIncommingPacket: byte[] buf2 = new byte[msg.Data.Length - 3]; Array.Copy(msg.Data, 3, buf2, 0, buf2.Length); if (IncommingPacket != null) { IncommingPacket.Invoke(buf2); } break; case PipePacketType.ParsedPacket: ParseIncomingPacket(msg); break; case PipePacketType.ConnectionStatusChanged: System.Windows.Forms.MessageBox.Show(msg.GetUInt32().ToString()); break; case PipePacketType.FullIncommingPacket: if (IncommingPacket != null) { byte[] data = new byte[msg.Data.Length - 3]; Array.Copy(msg.Data, 3, data, 0, msg.Data.Length - 3); IncommingPacket.Invoke(data); } break; case PipePacketType.test: uint adr = msg.GetUInt32(); System.Windows.Forms.MessageBox.Show(adr.ToString("X")); break; } } pipe.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(BeginRead), pipe); } catch (Exception ex) { pipe.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(BeginRead), pipe); } }