示例#1
0
        private void Client_DataReceived(EventDrivenTCPClient sender, object data)
        {
            byte[]          newdata;
            BinaryFormatter bf = new BinaryFormatter();

            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, data);
                newdata = ms.ToArray();
            }
            buffer.Write(newdata);
            byte[] message;
            byte[] timestamp;
            byte   signalLevel;

            for (int i = 0; i < buffer.Count; i++)
            {
                if (buffer[i] == 0x1a)
                {
                    buffer.Read(i);
                    i = 0;
                    switch (buffer[1])
                    {
                    case 0x31:
                        buffer.Read(1);
                        timestamp   = buffer.Read(6);
                        signalLevel = buffer.Read(1)[0];
                        message     = buffer.Read(2);
                        break;

                    case 0x32:
                        buffer.Read(31);
                        timestamp   = buffer.Read(6);
                        signalLevel = buffer.Read(1)[0];
                        message     = buffer.Read(7);
                        ParseModeS(message);
                        break;

                    case 0x33:
                        buffer.Read(1);
                        timestamp   = buffer.Read(6);
                        signalLevel = buffer.Read(1)[0];
                        message     = buffer.Read(14);
                        ParseModeS(message);
                        break;

                    default:
                        buffer.Read(1);
                        break;
                    }
                }
            }
        }
        private void AppendBuffer(byte[] newBuffer, int length)
        {
            // we need to keep a running buffer here, the last 1024 bytes?  how best to find the end boundary?  need to determine when the stream is finished!
            m_TempBuffer.Write(newBuffer, length);
            if (m_IsFileUpload)
            {
                if (m_EndBoundaryIndex < 0)
                {
                    m_EndBoundaryIndex = StreamHelper.LastIndexOf(m_TempBuffer.RawBytes, m_TempBuffer.Count, m_EndBoundaryBytes);

                    if (!IsLargeFileUpload && m_EndBoundaryIndex > -1)
                    {
                        m_EndBoundaryIndex = (m_FullBuffer.Length + length) - (m_TempBuffer.Count - m_EndBoundaryIndex);
                    }
                }
                if (m_StartBoundaryIndex < 0)
                {
                    m_StartBoundaryIndex = StreamHelper.IndexOf(m_FullBuffer, m_StartBoundaryBytes);

                    if (m_StartBoundaryIndex > -1)
                    {
                        m_StartBoundaryIndex = StreamHelper.IndexOf(m_FullBuffer, Encoding.UTF8.GetBytes("\r\n\r\n"), m_StartBoundaryIndex + m_StartBoundaryBytes.Length) + 4;
                    }
                }
            }

            if (m_StartBoundaryIndex == -1 || !IsLargeFileUpload)     // if this is not a file upload because no start boundary has been found then write buffer to memory
            {
                m_FullBuffer.Write(newBuffer, 0, length);
            }
            else
            {
                if (!m_FileCreated)     // we have never written to the file, dump the contents of the full buffer now
                {
                    bool exists = true;

                    while (exists)
                    {
                        m_TempFilename = Config.StaticConfig.TempFolder + "/" + Path.GetRandomFileName();
                        exists         = File.Exists(m_TempFilename);
                    }

                    m_FileStream = new FileStream(m_TempFilename, FileMode.Create, FileAccess.Write);

                    m_FullBuffer.Position = m_StartBoundaryIndex;
                    m_FullBuffer.CopyTo(m_FileStream);
                    m_FileStream.Write(newBuffer, 0, length);

                    m_FileCreated = true;
                }
                else     // we have previously written to the file, append new bytes
                {
                    if (m_EndBoundaryIndex == -1)
                    {
                        m_FileStream.Write(newBuffer, 0, length);
                    }
                    else
                    {
                        m_FileStream.Write(newBuffer, 0, length - m_EndBoundaryBytes.Length);
                    }
                }
            }
        }