Exemple #1
0
        /// <summary>
        /// Receive the specified amount of bytes.
        /// </summary>

        public bool OnReceive(byte[] bytes, int offset, int byteCount)
        {
            lastReceivedTime = DateTime.UtcNow.Ticks / 10000;

            if (byteCount == 0)
            {
                Close(true);
                return(false);
            }
            else if (ProcessBuffer(bytes, offset, byteCount))
            {
                return(true);
            }
            else
            {
#if UNITY_EDITOR
                if (bytes != null && bytes.Length > 0)
                {
                    var temp = new byte[byteCount];
                    for (int i = 0; i < byteCount; ++i)
                    {
                        temp[i] = bytes[i];
                    }

                    var fn = "error_" + lastReceivedTime + ".packet";
                    Tools.WriteFile(fn, temp);
                    Debug.Log("Packet saved as " + fn);
                }
#endif
                Close(true);
                return(false);
            }
        }
        /// <summary>
        /// Save the specified file.
        /// </summary>

        public bool SaveFile(string fileName, byte[] data)
        {
            if (Tools.WriteFile(string.IsNullOrEmpty(rootDirectory) ? fileName : Path.Combine(rootDirectory, fileName), data, true))
            {
                mSavedFiles[fileName] = data;
                return(true);
            }
            return(false);
        }
Exemple #3
0
        /// <summary>
        /// Save the specified file.
        /// </summary>

        public bool SaveFile(string fileName, byte[] data)
        {
            if (Tools.WriteFile(fileName, data, true))
            {
                mSavedFiles[fileName] = data;
                return(true);
            }
            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Write the node hierarchy to the specified filename.
        /// </summary>

        public void Write(string path, bool binary)
        {
            MemoryStream stream = new MemoryStream();

            if (binary)
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.WriteObject(this);
                Tools.WriteFile(path, stream.ToArray(), false);
                writer.Close();
            }
            else
            {
                StreamWriter writer = new StreamWriter(stream);
                Write(writer, 0);
                Tools.WriteFile(path, stream.ToArray(), false);
                writer.Close();
            }
        }
Exemple #5
0
        /// <summary>
        /// Write the node hierarchy to the specified filename.
        /// </summary>

        public bool Write(string path, SaveType type = SaveType.Text, bool allowConfigAccess = false)
        {
            bool         retVal = false;
            MemoryStream stream = new MemoryStream();

            if (type == SaveType.Binary)
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.WriteObject(this);
                retVal = Tools.WriteFile(path, stream, false, allowConfigAccess);
                writer.Close();
            }
            else if (type == SaveType.Compressed)
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.WriteObject(this);

                stream.Position = 0;
                MemoryStream comp = LZMA.Compress(stream, mLZMA);

                if (comp != null)
                {
                    retVal = Tools.WriteFile(path, comp, false, allowConfigAccess);
                    comp.Close();
                }
                else
                {
                    retVal = Tools.WriteFile(path, stream, false, allowConfigAccess);
                }
                writer.Close();
            }
            else
            {
                StreamWriter writer = new StreamWriter(stream);
                Write(writer, 0);
                retVal = Tools.WriteFile(path, stream, false, allowConfigAccess);
                writer.Close();
            }
            return(retVal);
        }
Exemple #6
0
        /// <summary>
        /// See if the received packet can be processed and split it up into different ones.
        /// </summary>

        bool ProcessBuffer(byte[] bytes, int offset, int byteCount)
        {
            if (offset + byteCount > bytes.Length)
            {
                LogError("ProcessBuffer(" + bytes.Length + " bytes, offset " + offset + ", count " + byteCount);
                return(false);
            }

            if (mReceiveBuffer == null)
            {
                // Create a new packet buffer
                mReceiveBuffer = Buffer.Create();
                mReceiveBuffer.BeginWriting(false).Write(bytes, offset, byteCount);
                mExpected = 0;
                mOffset   = 0;
            }
            else
            {
                // Append this data to the end of the last used buffer
                mReceiveBuffer.BeginWriting(true).Write(bytes, offset, byteCount);
            }

            for (mAvailable = mReceiveBuffer.size - mOffset; mAvailable > 4;)
            {
                // Figure out the expected size of the packet
                if (mExpected == 0)
                {
                    mExpected = mReceiveBuffer.PeekInt(mOffset);

                    // "GET " -- HTTP GET request sent by a web browser
                    if (mExpected == 542393671)
                    {
                        if (httpGetSupport)
                        {
                            if (stage == Stage.Verifying || stage == Stage.WebBrowser)
                            {
                                stage = Stage.WebBrowser;
                                string request = Encoding.ASCII.GetString(mReceiveBuffer.buffer, mOffset, mAvailable);
                                mReceiveBuffer.BeginPacket(Packet.RequestHTTPGet).Write(request);
                                mReceiveBuffer.EndPacket();
                                mReceiveBuffer.BeginReading(4);

                                lock (mIn)
                                {
                                    mIn.Enqueue(mReceiveBuffer);
                                    mReceiveBuffer = null;
                                    mExpected      = 0;
                                    mOffset        = 0;
                                }
                            }
                            return(true);
                        }

                        mReceiveBuffer.Recycle();
                        mReceiveBuffer = null;
                        mExpected      = 0;
                        mOffset        = 0;
                        Disconnect();
                        return(false);
                    }
                    else if (mExpected < 0 || mExpected > 16777216)
                    {
#if UNITY_EDITOR
                        LogError("Malformed data packet: " + mOffset + ", " + mAvailable + " / " + mExpected);

                        var temp = new byte[mReceiveBuffer.size];
                        for (int i = 0; i < byteCount; ++i)
                        {
                            temp[i] = mReceiveBuffer.buffer[i];
                        }

                        var fn = "error_" + lastReceivedTime + ".full";
                        Tools.WriteFile(fn, temp);
                        Debug.Log("Packet saved as " + fn);
#else
                        LogError("Malformed data packet: " + mOffset + ", " + mAvailable + " / " + mExpected);
#endif
                        mReceiveBuffer.Recycle();
                        mReceiveBuffer = null;
                        mExpected      = 0;
                        mOffset        = 0;
                        Disconnect();
                        return(false);
                    }
                }

                // The first 4 bytes of any packet always contain the number of bytes in that packet
                mAvailable -= 4;

                // If the entire packet is present
                if (mAvailable == mExpected)
                {
                    // Reset the position to the beginning of the packet
                    mReceiveBuffer.BeginReading(mOffset + 4);

                    // This packet is now ready to be processed
                    lock (mIn)
                    {
                        mIn.Enqueue(mReceiveBuffer);
                        mReceiveBuffer = null;
                        mExpected      = 0;
                        mOffset        = 0;
                    }
                    break;
                }
                else if (mAvailable > mExpected)
                {
                    // There is more than one packet. Extract this packet fully.
                    int realSize = mExpected + 4;
                    var temp     = Buffer.Create();

                    // Extract the packet and move past its size component
                    var bw = temp.BeginWriting();
                    bw.Write(mReceiveBuffer.buffer, mOffset, realSize);
                    temp.BeginReading(4);

                    // This packet is now ready to be processed
                    lock (mIn)
                    {
                        mIn.Enqueue(temp);

                        // Skip this packet
                        mAvailable -= mExpected;
                        mOffset    += realSize;
                        mExpected   = 0;
                    }
                }
                else
                {
                    break;
                }
            }
            return(true);
        }
Exemple #7
0
        /// <summary>
        /// Save the specified file.
        /// </summary>

        public void SaveFile(string fileName, byte[] data)
        {
            mSavedFiles[fileName] = data;
            Tools.WriteFile(fileName, data);
        }