private void OnReceive(IAsyncResult ar) { var received = _socket.EndReceive(ar); var buffer = new ArraySegment<byte>(ar.AsyncState as byte[], 0, 32*1024); var ipHeader = new IPHeader(buffer); var packet = ParseData(ipHeader); ProcessPacket(ipHeader.ProtocolType, packet); var header = new byte[32 * 1024]; _socket.BeginReceive(header, 0, 32 * 1024, SocketFlags.None, OnReceive, header); }
private void OnReceive(IAsyncResult ar) { var received = _socket.EndReceive(ar); var buffer = new ArraySegment <byte>(ar.AsyncState as byte[], 0, 32 * 1024); var ipHeader = new IPHeader(buffer); var packet = ParseData(ipHeader); ProcessPacket(ipHeader.ProtocolType, packet); var header = new byte[32 * 1024]; _socket.BeginReceive(header, 0, 32 * 1024, SocketFlags.None, OnReceive, header); }
private object ParseData(IPHeader ipHeader) { switch (ipHeader.ProtocolType) { case ProtocolType.Tcp: return new TcpHeader(ipHeader); case ProtocolType.Udp: return new UdpHeader(ipHeader); case ProtocolType.Unknown: return null; } throw new NotSupportedException(); }
private object ParseData(IPHeader ipHeader) { switch (ipHeader.ProtocolType) { case ProtocolType.Tcp: return(new TcpHeader(ipHeader)); case ProtocolType.Udp: return(new UdpHeader(ipHeader)); case ProtocolType.Unknown: return(null); } throw new NotSupportedException(); }
public UdpHeader(IPHeader ipHeader) { IpHeader = ipHeader; var buffer = IpHeader.Data; using(var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { using (var binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Length = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); } } Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + 8, buffer.Count - 8); }
public UdpHeader(IPHeader ipHeader) { IpHeader = ipHeader; var buffer = IpHeader.Data; using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { using (var binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Length = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); } } Data = new ArraySegment <byte>(buffer.Array, buffer.Offset + 8, buffer.Count - 8); }
public TcpHeader(IPHeader ipHeader) { IpHeader = ipHeader; var buffer = IpHeader.Data; using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { using (var binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); SequenceNumber = (uint) IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _uiAcknowledgementNumber = (uint) IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _usDataOffsetAndFlags = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); WindowSize = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = (short) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); _usUrgentPointer = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); HeaderLength = (byte) (_usDataOffsetAndFlags >> 12); HeaderLength *= 4; MessageLength = buffer.Count - HeaderLength; } } Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength); }
public TcpHeader(IPHeader ipHeader) { IpHeader = ipHeader; var buffer = IpHeader.Data; using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { using (var binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); SequenceNumber = (uint)IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _uiAcknowledgementNumber = (uint)IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _usDataOffsetAndFlags = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); WindowSize = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = (short)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); _usUrgentPointer = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); HeaderLength = (byte)(_usDataOffsetAndFlags >> 12); HeaderLength *= 4; MessageLength = buffer.Count - HeaderLength; } } Data = new ArraySegment <byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength); }