示例#1
0
        public void Dispose()
        {
            if (_dataWriterOperation != null)
            {
                _dataWriterOperation.Cancel();
            }

            if (_dataReaderOperation != null)
            {
                _dataReaderOperation.Cancel();
            }

            // put connectOperaion at the last one. or _dataReader.DetachStream will raise exception when dispose
            if (_connectOperation != null)
            {
                _connectOperation.Cancel();
            }

            DisposeDataReader();

            DisposeDataWriter();

            if (_clientSocket != null)
            {
                _clientSocket.Dispose();
                _clientSocket = null;
            }
        }
示例#2
0
 public virtual async Task Close()
 {
     if (null != loadOperation)
     {
         await Task.Run(() =>
         {
             cancellationTokenSource.Cancel();
             loadOperation.Cancel();
             loadOperation.Close();
         }
                        ).ConfigureAwait(false);
     }
 }
示例#3
0
#pragma warning disable 1998
        /// <summary>
        /// Helper to read the information into a buffer.
        /// </summary>
        private async Task <Tuple <string, int> > ReadIntoBufferAsync(StreamSocket socket)
        {
            DataReaderLoadOperation loadTask = null;
            string data;

            try
            {
                var reader = new DataReader(socket.InputStream);
                // Set the DataReader to only wait for available data
                reader.InputStreamOptions = InputStreamOptions.Partial;

                loadTask = reader.LoadAsync(0xffff);
                bool succeeded = loadTask.AsTask().Wait(20000);
                if (!succeeded)
                {
                    throw new TimeoutException("Timed out long polling, re-trying.");
                }

                var count = loadTask.GetResults();
                if (count == 0)
                {
                    throw new Exception("No results loaded from reader.");
                }

                data = reader.ReadString(count);
                if (data == null)
                {
                    throw new Exception("ReadString operation failed.");
                }
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine(ex.Message);
                if (loadTask != null && loadTask.Status == Windows.Foundation.AsyncStatus.Started)
                {
                    loadTask.Cancel();
                }
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("[Error] Signaling: Failed to read from socket. " + ex.Message);
                if (loadTask != null && loadTask.Status == Windows.Foundation.AsyncStatus.Started)
                {
                    loadTask.Cancel();
                }
                return(null);
            }

            int  content_length = 0;
            bool ret            = false;
            int  i = data.IndexOf("\r\n\r\n");

            if (i != -1)
            {
                Debug.WriteLine("Signaling: Headers received [i=" + i + " data(" + data.Length + ")" /*=" + data*/ + "]");
                if (GetHeaderValue(data, false, "\r\nContent-Length: ", out content_length))
                {
                    int total_response_size = (i + 4) + content_length;
                    if (data.Length >= total_response_size)
                    {
                        ret = true;
                    }
                    else
                    {
                        // We haven't received everything.  Just continue to accept data.
                        Debug.WriteLine("[Error] Singaling: Incomplete response; expected to receive " + total_response_size + ", received" + data.Length);
                    }
                }
                else
                {
                    Debug.WriteLine("[Error] Signaling: No content length field specified by the server.");
                }
            }
            return(ret ? Tuple.Create(data, content_length) : null);
        }