private ushort readVariableLengthUShort(ByteArrayReaderWriter reader) { ushort num = 0; byte b = reader.ReadByte(); num = (ushort)(num | (ushort)(b & 0x7F)); if ((b & 0x80) != 0) { num = (ushort)(num | (ushort)(reader.ReadByte() << 7)); } return(num); }
private ushort readVariableLengthUShort(ByteArrayReaderWriter reader) { ushort val = 0; byte b1 = reader.ReadByte(); val |= (ushort)(b1 & 0x7F); if ((b1 & 0x80) != 0) { val |= (ushort)(reader.ReadByte() << 7); } return(val); }
public void ReadData(ByteArrayReaderWriter stream) { byte addressVal = stream.ReadByte(); // if address type is not 0 or 1, data is not valid if (addressVal != 0 && addressVal != 1) { throw new FormatException(); } this.AddressType = (NetcodeAddressType)addressVal; IPAddress ip = null; if (this.AddressType == NetcodeAddressType.IPv4) { stream.ReadBytesIntoBuffer(tempIPV4, 4); ip = new IPAddress(tempIPV4); } else { stream.ReadBytesIntoBuffer(tempIPV6, 16); ip = new IPAddress(tempIPV6); } var port = stream.ReadUInt16(); this.Endpoint = new IPEndPoint(ip, port); }
/// <summary> /// Reads a packet from the stream. /// If packet is a connection request packet, stream read position lies at version info /// Otherwise, stream read position lies at packet-specific data /// </summary> public void Read(ByteArrayReaderWriter stream) { byte prefixByte = stream.ReadByte(); this.ReadSequenceByte = prefixByte; // read in packet type int packetTypeNibble = prefixByte & 0x0F; if (packetTypeNibble >= 7) { this.PacketType = NetcodePacketType.InvalidPacket; return; } else { this.PacketType = (NetcodePacketType)packetTypeNibble; } // read in the sequence number // high 4 bits of prefix byte are number of bytes used to encode sequence number if (this.PacketType != NetcodePacketType.ConnectionRequest) { int numSequenceBytes = (prefixByte >> 4); // num sequence bytes is between 1 and 8. // if it is outside this range, we have an invalid packet if (numSequenceBytes < 1 || numSequenceBytes > 8) { this.PacketType = NetcodePacketType.InvalidPacket; return; } ulong sequenceNumber = 0; for (int i = 0; i < numSequenceBytes; i++) { sequenceNumber |= ((ulong)stream.ReadByte() << (i * 8)); } this.SequenceNumber = sequenceNumber; } }