Пример #1
0
    // .Net Thread
    void ReadCallback(IAsyncResult result)
    {
        totalPackets++;
        if (_socket != null && _socket.Connected)
        {
            int read = 0;
            try
            {
                read = _socket.EndReceive(result);
                if (read <= 0)
                {
                    if (endCB != null)
                    {
                        endCB();
                    }
                    return;
                }
            }
            catch (Exception e)
            {
                int errorCode = 0;
                if (e is SocketException)
                {
                    errorCode = (e as SocketException).ErrorCode;
                }
                Debug.LogWarning("!!!Socket receive exception. Error msg: " + e.Message + "  |||Error code:" + errorCode);
                if (endCB != null)
                {
                    endCB();
                }
                return;
            }
            totalBytes += read;

            // parse bytes to packets
            parser.Parse(buffer, 0, read);

            // read recurse
            _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReadCallback, null);
        }
        else
        {
            Debug.LogWarning("!!!Socket receive Disconnected!");
        }
    }