コード例 #1
0
        // incoming data receive loop
        private async Task _receiveData()
        {
            try
            {
                var     stream = _socket.InputStream;
                IBuffer buffer = new Windows.Storage.Streams.Buffer(10240);
                while (true)
                {
                    IBuffer data = await stream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial);

                    if (data.Length == 0)
                    {
                        break;
                    }
                    _emitData(data);
                }
                // Client disconnected.
                _emitEnd();
                _socket = null;
                if (_server != null)
                {
                    _server._connectionDidClose(this);
                }
            }
            catch (System.IO.IOException ex)
            {
                NKLogging.log(ex.ToString());
            }
            catch (Exception ex)
            {
                NKLogging.log(ex.ToString());
            }
        }
コード例 #2
0
        // incoming data receive loop
        private void _receiveData()
        {
            var task = Task.Factory.FromAsync <int>(_socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null), _socket.EndReceive);

            task.ContinueWith(t =>
            {
                if (task.Result == 0)
                {
                    _emitEnd();
                    _socket = null;
                    if (_server != null)
                    {
                        _server._connectionDidClose(this);
                    }
                }
                else
                {
                    _emitData(buffer, 0, task.Result);
                    _receiveData(); // Receive more data
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }