Example #1
0
    IEnumerator RunRecieve()
    {
        byte[]        outbuf = new byte[PACKET_SIZE];
        int           nbytes = 0;
        NetworkStream stream = mClient.GetStream();

        while (isWaitReceive)
        {
            yield return(new WaitForEndOfFrame());

            if (!stream.DataAvailable)
            {
                continue;
            }

            try
            {
                nbytes = stream.Read(outbuf, 0, outbuf.Length);
                mFifoBuffer.Push(outbuf, nbytes);

                while (true)
                {
                    byte[]       buf     = mFifoBuffer.readSize(mFifoBuffer.GetSize());
                    bool         isError = false;
                    ICD.stHeader msg     = ICD.stHeader.Parse(buf, ref isError);
                    if (isError)
                    {
                        mFifoBuffer.Clear();
                    }

                    if (msg == null)
                    {
                        break;
                    }
                    else
                    {
                        mFifoBuffer.Pop(msg.head.len);
                    }

                    IPEndPoint ep        = (IPEndPoint)mClient.Client.RemoteEndPoint;
                    string     ipAddress = ep.Address.ToString();
                    int        port      = ep.Port;
                    string     info      = ipAddress + ":" + port.ToString();
                    if (mOnRecv != null)
                    {
                        mOnRecv.Invoke(msg, info);
                    }
                }
            }
            catch (Exception ex)
            { Debug.Log(ex.ToString()); }
        }
        mFifoBuffer.Clear();
        stream.Close();
    }