private static readonly HashSet<string> Searched = new HashSet<string>(); // for debugging
		
		
		protected override void Parse(ref ByteStreamReader bsr) {
			EntData = new ParsedEntData[DataHeader.EntHeaders.Length];
			for (int i = 0; i < DataHeader.EntHeaders.Length; i++) {
				ParsedDataMap entHeader = DataHeader.EntHeaders[i];
				var edictIndex = entHeader.GetFieldOrDefault<int>("edictindex");
				bsr.CurrentByteIndex = entHeader.GetFieldOrDefault<int>("location");
				string? className = entHeader.GetFieldOrDefault<string>("classname");
				if (className == null) // todo
					continue;
				if (!SaveInfo.SDataMapLookup.TryGetValue(className, out DataMap? entMap)) {
					string s = $"{nameof(EntitySaveStateBlock)}.{nameof(Parse)} - datamap for \"{className}\" not found";
					if (bsr.ReadSShort() == 4 && Searched.Add(s)) {
						bsr.DetermineDataMapHierarchy(SaveInfo,
							$"{nameof(EntitySaveStateBlock)}.{nameof(Parse)} - datamap for \"{className}\" not found",
							bsr.AbsoluteByteIndex - 2);
					}
					SaveInfo.AddError(s
#if DEBUG
						,false
#endif
						);
					continue;
				}
				EntData[i] = EntDataFactory.CreateFromName(SaveRef!, entHeader, entMap);
				SaveInfo.ParseContext.CurrentEntity = EntData[i];
				EntData[i]!.ParseStream(ref bsr);
			}
		}
    public void HandleConnectionData(HandshakeMessage type, ByteStreamReader stream, IPEndPoint endpoint)
    {
        Console.WriteLine(type);
        if (type == HandshakeMessage.SYN)
        {
            PendingConnectionEntity ce = new PendingConnectionEntity();
            ce.m_connectionId = m_connectionId.Allocate();
            ce.m_sender       = new Connection();
            int port = stream.ReadInt();
            ce.m_pendingStartTime = DateTime.Now;
            ce.m_sender.Connect(new IPEndPoint(endpoint.Address, port));
            m_pending.Add(ce);

            HandshakeStepTwo(ce);
        }
        else if (type == HandshakeMessage.ACK)
        {
            int id = stream.ReadInt();
            PendingConnectionEntity chosen = m_pending.FirstOrDefault(x => x.m_connectionId == id);
            if (chosen != null)
            {
                m_pending.Remove(chosen);
                m_entities[chosen.m_connectionId] = new ConnectionEntity(chosen);
                Network.Server.World.AddPlayer(chosen.m_connectionId);
            }
        }
        else if (type == HandshakeMessage.Disconnect)
        {
            int id = stream.ReadInt();
            Disconnect(id, false);
        }
    }
Exemple #3
0
        private void ReadResponseBody(ByteStreamReader reader, HttpResponseMessage response)
        {
            HttpContent content = null;

            if (response.Headers.TransferEncodingChunked.GetValueOrDefault(false))
            {
                // read the body with chunked transfer encoding
                var remainingStream = reader.GetRemainingStream();
                var chunkedStream   = new ReadsFromChunksStream(remainingStream);
                content = new StreamContent(chunkedStream);
            }
            else if (response.Content.Headers.ContentLength.HasValue)
            {
                // read the body with a content-length
                var remainingStream = reader.GetRemainingStream();
                var limitedStream   = new LimitedStream(remainingStream, response.Content.Headers.ContentLength.Value);
                content = new StreamContent(limitedStream);
            }
            else
            {
                // TODO: should we immediately close the connection in this case?
            }

            if (content != null)
            {
                // copy over the content headers
                foreach (var header in response.Content.Headers)
                {
                    content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                }

                response.Content = content;
            }
        }
Exemple #4
0
 public ReadsFromChunksStream(Stream innerStream)
 {
     _byteStreamReader = new ByteStreamReader(innerStream, 4096, false);
     _disposed         = false;
     _chunkSize        = -1;
     _remaining        = -1;
 }
Exemple #5
0
        /// <summary>
        /// Creates a new VCDIFF Encoder. The input streams will not be closed once this object is disposed.
        /// </summary>
        /// <param name="source">The dictionary (sourceStream file).</param>
        /// <param name="target">The target to create the diff from.</param>
        /// <param name="outputStream">The stream to write the diff into.</param>
        /// <param name="maxBufferSize">The maximum buffer size for window chunking in megabytes (MiB).</param>
        /// <param name="blockSize">
        /// The block size to use. Must be a power of two. No match smaller than this block size will be identified.
        /// Increasing blockSize by a factor of two will halve the amount of memory needed for the next block table, and will halve the setup time
        /// for a new BlockHash.  However, it also doubles the minimum match length that is guaranteed to be found.
        ///
        /// Blocksizes that are n mod 32 = 0 are AVX2 accelerated. Blocksizes that are n mod 16 = 0 are SSE2 accelerated, if supported. 16 is a good default
        /// for most scenarios, but you should use a block size of 32 or 64 for very similar data, or to optimize for speed.
        /// </param>
        /// <param name="chunkSize">
        /// The minimum size of a string match that is worth putting into a COPY. This must be bigger than twice the block size.</param>
        /// <param name="rollingHash">
        /// Manually provide a <see cref="RollingHash"/> instance that can be reused for multiple encoding instances
        /// of the same block size.
        ///
        /// If you provide a <see cref="RollingHash"/> instance, you must dispose of it yourself.
        /// </param>
        /// <exception cref="ArgumentException">If an invalid blockSize or chunkSize is used..</exception>
        public VcEncoder(Stream source, Stream target, Stream outputStream,
                         int maxBufferSize = 1, int blockSize = 16, int chunkSize = 0, RollingHash?rollingHash = null)
        {
            if (maxBufferSize <= 0)
            {
                maxBufferSize = 1;
            }
            this.blockSize    = blockSize;
            this.chunkSize    = chunkSize < 2 ? this.blockSize * 2 : chunkSize;
            this.sourceStream = source;
            this.targetData   = new ByteStreamReader(target);
            this.outputStream = outputStream;
            if (rollingHash == null)
            {
                this.disposeRollingHash = true;
                this.hasher             = new RollingHash(this.blockSize);
            }
            else
            {
                this.hasher = rollingHash;
            }
            if (this.hasher.WindowSize != this.blockSize)
            {
                throw new ArgumentException("Supplied RollingHash instance has a different window size than blocksize!");
            }
            this.bufferSize = maxBufferSize * 1024 * 1024;

            if (this.blockSize % 2 != 0 || this.chunkSize < 2 || this.chunkSize < 2 * this.blockSize)
            {
                throw new ArgumentException($"{this.blockSize} can not be less than 2 or twice the blocksize of the dictionary {this.blockSize}.");
            }
        }
 public override void Deserialize(ByteStreamReader reader)
 {
     base.Deserialize(reader);
     m_who       = reader.ReadInt();
     m_direction = reader.ReadVector2();
     m_point     = reader.ReadVector2();
 }
        public static ServerTcpMessage[] Unpack(/*ServerType serverType,*/ byte[] responseData)
        {
            DebugUtils.Assert(responseData != null && responseData.Length > 0, "Response data is null!");

            using (ByteStreamReader reader = new ByteStreamReader(responseData))
            {
                long serverId = reader.ReadLong();
                ClientTcpMessage.sessionId = reader.ReadInt(); //if sessionId is 4 bytes, that's OK.
                int msgNum = reader.ReadByte();
                ServerTcpMessage[] responseMessages = new ServerTcpMessage[msgNum];
                for (int i = 0; i < msgNum; i++)
                {
                    int  len  = reader.ReadShort();
                    int  code = reader.ReadInt();
                    long seq  = reader.ReadLong();

                    if (Enum.IsDefined(typeof(MsgCode), code))
                    {
                        responseMessages[i] = new ServerTcpMessage((MsgCode)code, seq, reader.ReadBytes(len));
                        DebugUtils.Log(DebugUtils.Type.Protocol, "Receive network message, protocol code " + code);
                    }
                    else
                    {
                        DebugUtils.LogError(DebugUtils.Type.Protocol, "For now, the client can't recognize the received protocol code " + code);
                    }
                }

                return(responseMessages);
            }
        }
        internal static ActivityData Restore(TypeDesc typeDesc, SaveInfo info, ref ByteStreamReader bsr)
        {
            uint len = bsr.ReadUInt();

            return((len & 0xFFFF0000) != ActivityFileTag
                                ? new ActivityData(typeDesc, (int)len)
                                : new ActivityData(typeDesc, bsr.ReadStringOfLength(len & 0xFFFF)));
        }
 protected override void Parse(ref ByteStreamReader bsr)
 {
     try {
         ParsedFields = bsr.ReadDataMapRecursive(ClassMap, SaveInfo);
     } catch (Exception e) {
         SaveInfo.AddError($"exception while parsing map \"{ClassMap.ClassName}\": {e.Message}");
     }
 }
        protected override void Parse(ref ByteStreamReader bsr)
        {
            base.Parse(ref bsr);
            // todo if not landmark, set local origin to spawn
            //m_angRotation = p1.vangle;

            // if get flags & ducking, ,m_Local.m_bDucked = try, else false
        }
 protected override void Parse(ref ByteStreamReader bsr)
 {
     IdString    = bsr.ReadStringOfLength(4);
     SaveVersion = bsr.ReadSInt();
     TokenTableFileTableOffset = bsr.ReadSInt();
     TokenCount     = bsr.ReadSInt();
     TokenTableSize = bsr.ReadSInt();
     // offset += table size?
 }
Exemple #12
0
 public void HandleConnectionData(HandshakeMessage type, ByteStreamReader stream, IPEndPoint endpoint)
 {
     Console.WriteLine(type);
     if (type == HandshakeMessage.SYNACK && State != ConnectionState.Connected)
     {
         int connectionId = stream.ReadInt();
         HandshakeStepThree(connectionId);
     }
 }
Exemple #13
0
 public override void Deserialize(ByteStreamReader reader)
 {
     m_sessionId       = reader.ReadInt();
     m_actuallySpawned = reader.ReadBool();
     if (m_actuallySpawned)
     {
         m_startPosition = reader.ReadVector2();
     }
 }
        protected override void Parse(ref ByteStreamReader bsr)
        {
            int nEntities = bsr.ReadSInt();

            EntHeaders = new ParsedDataMap[nEntities];
            for (int i = 0; i < nEntities; i++)
            {
                EntHeaders[i] = bsr.ReadDataMap("ETABLE", SaveInfo);
            }
        }
Exemple #15
0
        public static Icon GetIcon(MemoryObject mo)
        {
            byte[]           data   = mo.ReadData();
            ByteStreamReader reader = new ByteStreamReader(data);

            using (Bitmap b = new Bitmap(reader))
            {
                return(Icon.FromHandle(b.GetHicon()));
            }
        }
    /// <summary>
    /// Called when stream is identified as a ConnectionRequest object.
    /// </summary>
    /// <param name="stream">Data stream.</param>
    /// <param name="flags">Stream flags.</param>
    /// <param name="endpoint">Data source.</param>
    /// <returns><c>true</c> if stream was handled correctly, <c>false</c> otherwise.</returns>
    bool HandleConnectionMsg(ByteStreamReader stream, byte flags, IPEndPoint endpoint)
    {
        if (HasFlag(flags, ( byte )MsgFlags.ConnectionRequest))
        {
            byte content = stream.ReadByte();
            m_connectionMessagesReceiver.HandleConnectionData(( HandshakeMessage )content, stream, endpoint);
            return(true);
        }

        return(false);
    }
Exemple #17
0
        // CSaveRestore::RestoreClientState
        // id = 'V' 'A' 'L' 'V', version = 0x73 = 115, section header = 2
        protected override unsafe void Parse(ref ByteStreamReader bsr)
        {
            base.Parse(ref bsr);
            var version = bsr.ReadSInt();
            var magic   = bsr.ReadSInt();
            // if ( magicnumber == SECTION_MAGIC_NUMBER )
            var sectionHeaderVersion = bsr.ReadSInt();

            bsr.ReadStruct(out BaseClientSections sections, sizeof(BaseClientSections));
            //var symbolTable = bsr.ReadSymbolTable(sections.SymbolCount, sections.SymbolSize);
            var symbolTable = bsr.ReadNullSeparatedStrings(sections.SymbolCount);
        }
Exemple #18
0
    public override void Deserialize(ByteStreamReader reader)
    {
        int count = reader.ReadInt();

        for (int i = 0; i < count; i++)
        {
            PlayerState ps = new PlayerState();
            ps.Deserialize(reader);
            states.Add(ps);
        }
        //Network.Log("ID: " + state.id + ", position: " + state.position);
    }
Exemple #19
0
        public static EventsSave Restore(TypeDesc typeDesc, SaveInfo info, ref ByteStreamReader bsr)
        {
            int count = bsr.ReadSInt();

            // inline version of reading embedded field of type CBaseEntityOutput (it only contains 1 field)

            if (bsr.ReadSShort() != 4)
            {
                throw new ConstraintException("first entry in data map should be 4");
            }
            string?mapSym = bsr.ReadSymbol(info);

            if (mapSym != "Value")
            {
                throw new ConstraintException($"bad symbol, expected \"Value\" but read \"{mapSym}\"");
            }
            int             fieldsSaved = bsr.ReadSInt();
            ParsedSaveField?psf         = null;

            if (fieldsSaved == 1)
            {
                bsr.StartBlock(info, out string?sym);
                if (sym != "m_Value")
                {
                    throw new ConstraintException($"bad symbol, expected \"m_Value\" but read \"{sym}\"");
                }
                FieldType type = (FieldType)bsr.ReadSInt();
                string?   s    = FieldNameFromType(type);
                if (s != null)
                {
                    TypeDesc t  = new TypeDesc(s, type);
                    DataMap  m  = new DataMap("m_Value", new [] { t });
                    var      pm = bsr.ReadDataMap(m, info);
                    if (pm.ParsedFields.Any())
                    {
                        psf = pm.ParsedFields.Single().Value;
                    }
                }
                bsr.EndBlock(info);
            }
            else if (fieldsSaved != 0)
            {
                throw new ConstraintException($"expected 0 fields, got {fieldsSaved}");
            }

            ParsedDataMap[] events = new ParsedDataMap[count];
            for (int i = 0; i < count; i++)
            {
                events[i] = bsr.ReadDataMap("EntityOutput", info);
            }

            return(new EventsSave(typeDesc, psf, events));
        }
        protected override void Parse(ref ByteStreamReader bsr)
        {
            base.Parse(ref bsr);

            /*
             * for ( i = 0; i < size; i++ )
             * {
             * g_pSaveRestoreFileSystem->Read( &entityId, sizeof(int), pFile );
             * pSaveData->GetEntityInfo(entityId)->flags = FENTTABLE_REMOVED;
             * }
             */
        }
Exemple #21
0
        public static UtilMap <ParsedDataMap, ParsedDataMap> RestoreEmbedded(
            string utlMapName,
            SaveInfo info,
            ref ByteStreamReader bsr,
            string?keyMapName,
            string?valMapName)
        {
            object?[] customParams = { EMBEDDED, null, keyMapName, EMBEDDED, null, valMapName };
            TypeDesc  utlMapDesc   = new TypeDesc(utlMapName, DescFlags.FTYPEDESC_SAVE, Restore, customParams);

            return(UtilMap <ParsedDataMap, ParsedDataMap> .Restore(utlMapDesc, info, ref bsr));
        }
Exemple #22
0
        public async Task <HttpResponseMessage> ReceiveResponseAsync(Stream stream, HttpRequestMessage request)
        {
            ByteStreamReader reader = new ByteStreamReader(stream, BufferSize, false);

            var response = await ReadResponseHeadAsync(reader, request).ConfigureAwait(false);

            if (!MethodsWithoutResponseBody.Contains(request.Method))
            {
                ReadResponseBody(reader, response);
            }

            return(response);
        }
Exemple #23
0
        public static UtilMap <TK, ParsedDataMap> RestoreEmbeddedValue(
            string utlMapName,
            SaveInfo info,
            ref ByteStreamReader bsr,
            string?valMapName,
            FieldType keyType,
            CustomReadFunc?keyReadFunc = null)
        {
            object?[] customParams = { keyType, keyReadFunc, null, EMBEDDED, null, valMapName };
            TypeDesc  utlMapDesc   = new TypeDesc(utlMapName, DescFlags.FTYPEDESC_SAVE, Restore, customParams);

            return(UtilMap <TK, ParsedDataMap> .Restore(utlMapDesc, info, ref bsr));
        }
Exemple #24
0
        // this was confusing but convenient for UtilVector, but here it's really stupid

        public static UtilMap <ParsedDataMap, TV> RestoreEmbeddedKey(
            string utlMapName,
            SaveInfo info,
            ref ByteStreamReader bsr,
            string?keyMapName,
            FieldType valType,
            CustomReadFunc?valReadFunc = null)
        {
            object?[] customParams = { EMBEDDED, null, keyMapName, valType, valReadFunc, null };
            TypeDesc  utlMapDesc   = new TypeDesc(utlMapName, DescFlags.FTYPEDESC_SAVE, Restore, customParams);

            return(UtilMap <ParsedDataMap, TV> .Restore(utlMapDesc, info, ref bsr));
        }
Exemple #25
0
        public PDFContent(byte[] data, IPDFDictionary page)
        {
            this.Data    = data;
            this.Options = page.Dict;
            ByteStreamReader reader    = new ByteStreamReader(Data);
            PDFTokenizer     tokenizer = new PDFTokenizer(reader);

            PDFContentTokenStack stack = new PDFContentTokenStack();

            foreach (IPDFToken token in tokenizer)
            {
                stack.ProcessToken(token);
            }

            this.Tokens = stack.Reverse().ToList();
        }
 protected override void Parse(ref ByteStreamReader bsr)
 {
     base.Parse(ref bsr);
     if (ParsedFields != null)
     {
         Vector3 parentSpaceOffset = default;                 // todo modelSpaceOffset
         if (!ParsedFields.ParsedFields.ContainsKey("m_pParent"))
         {
             parentSpaceOffset += SaveInfo.LandmarkPos;                     // parent is the world
         }
         var       origin     = ParsedFields.GetFieldOrDefault <Vector3>("m_vecAbsOrigin");
         Matrix3X4?coordFrame = ParsedFields.GetFieldOrDefault <Matrix3X4>("m_rgflCoordinateFrame");
         coordFrame?.SetColumn(3, in origin.Field);
         origin.Field += parentSpaceOffset;
     }
 }
    /// <summary>
    /// Takes care of data from a byte array, calling all the necessary deserializations and dispatches.
    /// </summary>
    /// <param name="data">The data.</param>
    /// <param name="endpoint">The data source.</param>
    /// <returns><c>true</c> if data was successfully handles, <c>false</c> otherwise.</returns>
    public bool HandleData(byte[] data, IPEndPoint endpoint = null)
    {
        System.Diagnostics.Debug.Assert(data.Length < c_maxPacketSize);
        m_stream = new ByteStreamReader(data);

        byte flags = m_stream.ReadByte();

        if (HandleEvent(m_stream, flags))
        {
            return(true);
        }
        if (HandleConnectionMsg(m_stream, flags, endpoint))
        {
            return(true);
        }

        return(false);
    }
        protected override void Parse(ref ByteStreamReader bsr)
        {
            SourceFileHeader = new SourceFileHeader(this);
            SourceFileHeader.ParseStream(ref bsr);
            SaveInfo.ParseContext.CurrentSymbolTable = bsr.ReadSymbolTable(SourceFileHeader.TokenCount, SourceFileHeader.TokenTableSize) !;
            GameHeader = bsr.ReadDataMap("GameHeader", SaveInfo);
            Globals    = bsr.ReadDataMap("GLOBAL", SaveInfo);
            StateFiles = new EmbeddedStateFile[bsr.ReadSInt()];

            for (int i = 0; i < StateFiles.Length; i++)
            {
                StateFiles[i] = EmbeddedStateFile.CreateFromName(this, bsr.ReadCharArray(260));
                int fileLength = bsr.ReadSInt();
                StateFiles[i].ParseStream(bsr.SplitAndSkip(fileLength));
            }

            SaveInfo.Cleanup();
            Debug.Assert(bsr.BytesRemaining == 0);
            SaveInfo.PrintDeterminedDatamaps();
        }
Exemple #29
0
        /// <summary>
        /// the functions reading the data from socket.
        /// </summary>
        private void ReadData(byte[] data)
        {
            DebugUtils.Log(DebugUtils.Type.AsyncSocket, "Async tcp socket has received network data, data length " + data.Length);

            using (ByteStreamReader reader = new ByteStreamReader(data))
            {
                try
                {
                    int dataLength = data.Length;
                    int startPos   = 0;
                    while (startPos < dataLength)
                    {
                        int length = reader.ReadShort();
                        if (startPos + length > dataLength)
                        {
                            DebugUtils.LogWarning(DebugUtils.Type.AsyncSocket, "Async tcp socket: There are separated packets!");
                            int separatedLength = dataLength - startPos;
                            unusedBuffer = new byte[separatedLength];
                            Buffer.BlockCopy(data, startPos, unusedBuffer, 0, separatedLength);
                            break;
                        }

                        short version = reader.ReadShort(); //version == 127

                        byte[]             response = reader.ReadBytes(length - 4);
                        ServerTcpMessage[] messages = ServerTcpMessage.Unpack(response);
                        if (ResponseHandler != null)
                        {
                            ResponseHandler(messages);
                        }

                        startPos += length;
                    }
                }
                catch (Exception e)
                {
                    DebugUtils.LogError(DebugUtils.Type.AsyncSocket, e.ToString());
                }
            }
        }
Exemple #30
0
        private async Task <HttpResponseMessage> ReadResponseHeadAsync(ByteStreamReader reader, HttpRequestMessage request)
        {
            // initialize the response
            var response = new HttpResponseMessage {
                RequestMessage = request
            };

            // read the first line of the response
            string line = await reader.ReadLineAsync().ConfigureAwait(false);

            string[] pieces = line.Split(new[] { ' ' }, 3);
            if (pieces[0] != "HTTP/1.1")
            {
                throw new HttpRequestException("The HTTP version the response is not supported.");
            }

            response.StatusCode   = (HttpStatusCode)int.Parse(pieces[1]);
            response.ReasonPhrase = pieces[2];

            // read the headers
            response.Content = new ByteArrayContent(new byte[0]);
            while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null && line != string.Empty)
            {
                pieces = line.Split(new[] { ":" }, 2, StringSplitOptions.None);
                if (pieces[1].StartsWith(" "))
                {
                    pieces[1] = pieces[1].Substring(1);
                }

                if (!response.Headers.TryAddWithoutValidation(pieces[0], pieces[1]))
                {
                    if (!response.Content.Headers.TryAddWithoutValidation(pieces[0], pieces[1]))
                    {
                        throw new InvalidOperationException($"The header '{pieces[0]}' could not be added to the response message or to the response content.");
                    }
                }
            }

            return(response);
        }