Esempio n. 1
0
 private void HandleOneRequest(OnDemandData request)
 {
     waiting = true;
     byte[] data = null;
     if (clientInstance.caches[0] != null)
     {
         data = clientInstance.caches[request.dataType + 1].decompress((int)request.id);
     }
     if (!crcMatches(versions[request.dataType][request.id],
                     crcs[request.dataType][request.id], data))
     {
         data = null;
     }
     lock (mandatoryRequests)
     {
         if (data == null)
         {
             unrequested.pushBack(request);
         }
         else
         {
             request.InitializeBuffer(data);
             lock (complete)
             {
                 complete.pushBack(request);
             }
         }
     }
 }
        public void passiveRequest(int id, int type)
        {
            if (clientInstance.caches[0] == null)
            {
                return;
            }
            if (versions[type][id] == 0)
            {
                return;
            }
            if (filePriorities[type][id] == 0)
            {
                return;
            }
            if (highestPriority == 0)
            {
                return;
            }
            OnDemandData onDemandData = new OnDemandData();

            onDemandData.dataType   = type;
            onDemandData.id         = id;
            onDemandData.incomplete = false;
            lock (passiveRequests)
            {
                passiveRequests.pushBack(onDemandData);
            }
        }
Esempio n. 3
0
        protected void closeRequest(OnDemandData request)
        {
            //HelloKitty: Removed remote content downloading. Not something I'm interested in supporting.

            socket       = null;
            inputStream  = null;
            outputStream = null;
            expectedSize = 0;
            failedRequests++;
        }
        public void request(int i, int j)
        {
            if (i < 0 || i > versions.Length || j < 0 || j > versions[i].Length)
            {
                throw new InvalidOperationException($"Failed to request map chunk: {i},{j}");
            }

            if (versions[i][j] == 0)
            {
                throw new InvalidOperationException($"Failed to request map chunk: {i},{j} Versions are 0");
            }

            lock (nodeSubList)
            {
                for (OnDemandData onDemandData = (OnDemandData)nodeSubList
                                                 .peek();
                     onDemandData != null;
                     onDemandData = (OnDemandData)nodeSubList
                                    .getNext())
                {
                    if (onDemandData.dataType == i && onDemandData.id == j)
                    {
                        return;
                    }
                }

                OnDemandData onDemandData_1 = new OnDemandData();
                onDemandData_1.dataType   = i;
                onDemandData_1.id         = j;
                onDemandData_1.incomplete = true;
                lock (mandatoryRequests)
                {
                    mandatoryRequests.pushBack(onDemandData_1);
                }

                nodeSubList.push(onDemandData_1);
            }
        }
        private void handleFailed()
        {
            uncompletedCount = 0;
            completedCount   = 0;
            for (OnDemandData onDemandData = (OnDemandData)requested
                                             .peekFront();
                 onDemandData != null;
                 onDemandData = (OnDemandData)requested.getPrevious())
            {
                if (onDemandData.incomplete)
                {
                    uncompletedCount++;
                }
                else
                {
                    completedCount++;
                }
            }

            while (uncompletedCount < 10)
            {
                OnDemandData onDemandData_1 = (OnDemandData)unrequested.popFront();
                if (onDemandData_1 == null)
                {
                    break;
                }
                if (filePriorities[onDemandData_1.dataType][onDemandData_1.id] != 0)
                {
                    filesLoaded++;
                }
                filePriorities[onDemandData_1.dataType][onDemandData_1.id] = 0;
                requested.pushBack(onDemandData_1);
                uncompletedCount++;
                closeRequest(onDemandData_1);
                waiting = true;
            }
        }
        public void run()
        {
            try
            {
                while (running)
                {
                    onDemandCycle++;
                    int i = 20;
                    if (highestPriority == 0 && clientInstance.caches[0] != null)
                    {
                        i = 50;
                    }
                    try
                    {
                        Thread.Sleep(i);
                    }
                    catch (Exception _ex)
                    {
                    }

                    waiting = true;
                    for (int j = 0; j < 100; j++)
                    {
                        if (!waiting)
                        {
                            break;
                        }
                        waiting = false;
                        checkReceived();
                        handleFailed();
                        if (uncompletedCount == 0 && j >= 5)
                        {
                            break;
                        }
                        method568();
                        if (inputStream != null)
                        {
                            readData();
                        }
                    }

                    bool flag = false;
                    for (OnDemandData onDemandData = (OnDemandData)requested
                                                     .peekFront();
                         onDemandData != null;
                         onDemandData = (OnDemandData)requested.getPrevious())
                    {
                        if (onDemandData.incomplete)
                        {
                            flag = true;
                            onDemandData.loopCycle++;
                            if (onDemandData.loopCycle > 50)
                            {
                                onDemandData.loopCycle = 0;
                                closeRequest(onDemandData);
                            }
                        }
                    }

                    if (!flag)
                    {
                        for (OnDemandData onDemandData_1 = (OnDemandData)requested
                                                           .peekFront();
                             onDemandData_1 != null;
                             onDemandData_1 = (OnDemandData)requested
                                              .getPrevious())
                        {
                            flag = true;
                            onDemandData_1.loopCycle++;
                            if (onDemandData_1.loopCycle > 50)
                            {
                                onDemandData_1.loopCycle = 0;
                                closeRequest(onDemandData_1);
                            }
                        }
                    }

                    if (flag)
                    {
                        loopCycle++;
                        if (loopCycle > 750)
                        {
                            try
                            {
                                socket.Close();
                            }
                            catch (Exception _ex)
                            {
                            }

                            socket       = null;
                            inputStream  = null;
                            outputStream = null;
                            expectedSize = 0;
                        }
                    }
                    else
                    {
                        loopCycle    = 0;
                        statusString = "";
                    }

                    if (clientInstance.isLoggedIn && socket != null && outputStream != null &&
                        (highestPriority > 0 || clientInstance.caches[0] == null))
                    {
                        writeLoopCycle++;
                        if (writeLoopCycle > 500)
                        {
                            writeLoopCycle = 0;
                            payload[0]     = 0;
                            payload[1]     = 0;
                            payload[2]     = 0;
                            payload[3]     = 10;
                            try
                            {
                                outputStream.Write(payload, 0, 4);
                            }
                            catch (IOException _ex)
                            {
                                loopCycle = 5000;
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                signlink.reporterror($"od_ex {exception.Message}\nStack: {exception.StackTrace}");
            }
        }
        private void readData()
        {
            try
            {
                int j = socket.Available;
                if (expectedSize == 0 && j >= 6)
                {
                    waiting = true;
                    for (int k = 0; k < 6; k += inputStream.Read(payload, k, 6 - k))
                    {
                        ;
                    }
                    int l  = payload[0] & 0xff;
                    int j1 = ((payload[1] & 0xff) << 8) + (payload[2] & 0xff);
                    int l1 = ((payload[3] & 0xff) << 8) + (payload[4] & 0xff);
                    int i2 = payload[5] & 0xff;
                    current = null;
                    for (OnDemandData onDemandData = (OnDemandData)requested
                                                     .peekFront();
                         onDemandData != null;
                         onDemandData = (OnDemandData)requested.getPrevious())
                    {
                        if (onDemandData.dataType == l && onDemandData.id == j1)
                        {
                            current = onDemandData;
                        }
                        if (current != null)
                        {
                            onDemandData.loopCycle = 0;
                        }
                    }

                    if (current != null)
                    {
                        loopCycle = 0;
                        if (l1 == 0)
                        {
                            signlink.reporterror("Rej: " + l + "," + j1);
                            current.ClearBuffer();
                            if (current.incomplete)
                            {
                                lock (complete)
                                {
                                    complete.pushBack(current);
                                }
                            }
                            else
                            {
                                current.unlink();
                            }

                            current = null;
                        }
                        else
                        {
                            if (current.buffer == null && i2 == 0)
                            {
                                current.InitializeBuffer(new byte[l1]);
                            }
                            if (current.buffer == null && i2 != 0)
                            {
                                throw new IOException("missing start of file");
                            }
                        }
                    }

                    completedSize = i2 * 500;
                    expectedSize  = 500;
                    if (expectedSize > l1 - i2 * 500)
                    {
                        expectedSize = l1 - i2 * 500;
                    }
                }

                if (expectedSize > 0 && j >= expectedSize)
                {
                    waiting = true;
                    byte[] abyte0 = payload;
                    int    i1     = 0;
                    if (current != null)
                    {
                        abyte0 = current.buffer;
                        i1     = completedSize;
                    }

                    for (int k1 = 0; k1 < expectedSize; k1 += inputStream.Read(abyte0, k1 + i1, expectedSize - k1))
                    {
                        ;
                    }
                    if (expectedSize + completedSize >= abyte0.Length && current != null)
                    {
                        if (clientInstance.caches[0] != null)
                        {
                            clientInstance.caches[current.dataType + 1].put(abyte0.Length, abyte0, (int)current.id);
                        }
                        if (!current.incomplete && current.dataType == 3)
                        {
                            current.incomplete = true;
                            current.dataType   = 93;
                        }

                        if (current.incomplete)
                        {
                            lock (complete)
                            {
                                complete.pushBack(current);
                            }
                        }
                        else
                        {
                            current.unlink();
                        }
                    }

                    expectedSize = 0;
                }
            }
            catch (IOException ioexception)
            {
                try
                {
                    socket.Close();
                }
                catch (Exception _ex)
                {
                }

                socket       = null;
                inputStream  = null;
                outputStream = null;
                expectedSize = 0;
            }
        }
        private void method568()
        {
            while (uncompletedCount == 0 && completedCount < 10)
            {
                if (highestPriority == 0)
                {
                    break;
                }
                OnDemandData onDemandData;
                lock (passiveRequests)
                {
                    onDemandData = (OnDemandData)passiveRequests.popFront();
                }

                while (onDemandData != null)
                {
                    if (filePriorities[onDemandData.dataType][onDemandData.id] != 0)
                    {
                        filePriorities[onDemandData.dataType][onDemandData.id] = 0;
                        requested.pushBack(onDemandData);
                        closeRequest(onDemandData);
                        waiting = true;
                        if (filesLoaded < totalFiles)
                        {
                            filesLoaded++;
                        }
                        statusString = "Loading extra files - " + (filesLoaded * 100) / totalFiles + "%";
                        completedCount++;
                        if (completedCount == 10)
                        {
                            return;
                        }
                    }

                    lock (passiveRequests)
                    {
                        onDemandData = (OnDemandData)passiveRequests.popFront();
                    }
                }

                for (int j = 0; j < 4; j++)
                {
                    byte[] abyte0 = filePriorities[j];
                    int    k      = abyte0.Length;
                    for (int l = 0; l < k; l++)
                    {
                        if (abyte0[l] == highestPriority)
                        {
                            abyte0[l] = 0;
                            OnDemandData onDemandData_1 = new OnDemandData();
                            onDemandData_1.dataType   = j;
                            onDemandData_1.id         = l;
                            onDemandData_1.incomplete = false;
                            requested.pushBack(onDemandData_1);
                            closeRequest(onDemandData_1);
                            waiting = true;
                            if (filesLoaded < totalFiles)
                            {
                                filesLoaded++;
                            }
                            statusString = "Loading extra files - " + (filesLoaded * 100) / totalFiles + "%";
                            completedCount++;
                            if (completedCount == 10)
                            {
                                return;
                            }
                        }
                    }
                }

                highestPriority--;
            }
        }
        private void closeRequest(OnDemandData request)
        {
            try
            {
                if (socket == null)
                {
                    long currentTime = TimeService.CurrentTimeInMilliseconds();
                    if (currentTime - lastRequestTime < 4000L)
                    {
                        return;
                    }
                    lastRequestTime = currentTime;
                    socket          = clientInstance.openSocket(43594).ConfigureAwait(false).GetAwaiter().GetResult();
                    inputStream     = socket.GetStream();
                    outputStream    = socket.GetStream();
                    outputStream.WriteByte(15);
                    for (int j = 0; j < 8; j++)
                    {
                        inputStream.ReadByte();
                    }

                    loopCycle = 0;
                }

                payload[0] = (byte)request.dataType;
                payload[1] = (byte)(request.id >> 8);
                payload[2] = (byte)request.id;
                if (request.incomplete)
                {
                    payload[3] = 2;
                }
                else if (!clientInstance.isLoggedIn)
                {
                    payload[3] = 1;
                }
                else
                {
                    payload[3] = 0;
                }
                outputStream.Write(payload, 0, 4);
                writeLoopCycle = 0;
                failedRequests = -10000;
                return;
            }
            catch (SocketException ioexception)
            {
            }

            try
            {
                socket.Close();
            }
            catch (Exception _ex)
            {
            }

            socket       = null;
            inputStream  = null;
            outputStream = null;
            expectedSize = 0;
            failedRequests++;
        }
        public IEnumerator RunCoroutine()
        {
            while (running)
            {
                onDemandCycle++;
                int i = 20;
                if (highestPriority == 0 && clientInstance.caches[0] != null)
                {
                    i = 50;
                }

                yield return(new WaitForSeconds((float)i / 1000.0f));

                waiting = true;
                for (int j = 0; j < 100; j++)
                {
                    if (!waiting)
                    {
                        break;
                    }
                    waiting = false;
                    checkReceived();
                    handleFailed();
                    if (uncompletedCount == 0 && j >= 5)
                    {
                        break;
                    }
                    method568();
                    if (inputStream != null)
                    {
                        readData();
                    }
                }

                bool flag = false;
                for (OnDemandData onDemandData = (OnDemandData)requested
                                                 .peekFront();
                     onDemandData != null;
                     onDemandData = (OnDemandData)requested.getPrevious())
                {
                    if (onDemandData.incomplete)
                    {
                        flag = true;
                        onDemandData.loopCycle++;
                        if (onDemandData.loopCycle > 50)
                        {
                            onDemandData.loopCycle = 0;
                            closeRequest(onDemandData);
                        }
                    }
                }

                if (!flag)
                {
                    for (OnDemandData onDemandData_1 = (OnDemandData)requested
                                                       .peekFront();
                         onDemandData_1 != null;
                         onDemandData_1 = (OnDemandData)requested
                                          .getPrevious())
                    {
                        flag = true;
                        onDemandData_1.loopCycle++;
                        if (onDemandData_1.loopCycle > 50)
                        {
                            onDemandData_1.loopCycle = 0;
                            closeRequest(onDemandData_1);
                        }
                    }
                }

                if (flag)
                {
                    loopCycle++;
                    if (loopCycle > 750)
                    {
                        try
                        {
                            socket.Close();
                        }
                        catch (Exception _ex)
                        {
                        }

                        socket       = null;
                        inputStream  = null;
                        outputStream = null;
                        expectedSize = 0;
                    }
                }
                else
                {
                    loopCycle    = 0;
                    statusString = "";
                }

                if (clientInstance.isLoggedIn && socket != null && outputStream != null &&
                    (highestPriority > 0 || clientInstance.caches[0] == null))
                {
                    writeLoopCycle++;
                    if (writeLoopCycle > 500)
                    {
                        writeLoopCycle = 0;
                        payload[0]     = 0;
                        payload[1]     = 0;
                        payload[2]     = 0;
                        payload[3]     = 10;
                        try
                        {
                            outputStream.Write(payload, 0, 4);
                        }
                        catch (IOException _ex)
                        {
                            loopCycle = 5000;
                        }
                    }
                }
            }
        }