protected override async Task WriteAsync(byte[] buffer, int offset, int count) { using (_commandsClient = NetworkClientFactory.CreateTcpClient()) { Uri connectionUri = ConnectionParameters.ConnectionUri; int httpPort = connectionUri.Port != -1 ? connectionUri.Port : Constants.DefaultHttpPort; await _commandsClient.ConnectAsync(connectionUri.Host, httpPort); string base64CodedCommandString = Convert.ToBase64String(buffer, offset, count); byte[] base64CommandBytes = Encoding.ASCII.GetBytes(base64CodedCommandString); string request = ComposePostRequest(base64CommandBytes); byte[] requestBytes = Encoding.ASCII.GetBytes(request); ArraySegment <byte>[] sendList = { new ArraySegment <byte>(requestBytes), new ArraySegment <byte>(base64CommandBytes) }; await _commandsClient.SendAsync(sendList, SocketFlags.None); } }
public override async Task ConnectAsync(CancellationToken token) { _tcpClient = NetworkClientFactory.CreateTcpClient(); Uri connectionUri = ConnectionParameters.ConnectionUri; int rtspPort = connectionUri.Port != -1 ? connectionUri.Port : Constants.DefaultRtspPort; await _tcpClient.ConnectAsync(connectionUri.Host, rtspPort); _remoteEndPoint = _tcpClient.RemoteEndPoint; _networkStream = new NetworkStream(_tcpClient, false); }
public override async Task ConnectAsync(CancellationToken token) { _commandCounter = 0; _sessionCookie = Guid.NewGuid().ToString("N").Substring(0, 10); _streamDataClient = NetworkClientFactory.CreateTcpClient(); Uri connectionUri = ConnectionParameters.ConnectionUri; int httpPort = connectionUri.Port != -1 ? connectionUri.Port : Constants.DefaultHttpPort; await _streamDataClient.ConnectAsync(connectionUri.Host, httpPort); _remoteEndPoint = _streamDataClient.RemoteEndPoint; _dataNetworkStream = new NetworkStream(_streamDataClient, false); string request = ComposeGetRequest(); byte[] requestBytes = Encoding.ASCII.GetBytes(request); await _dataNetworkStream.WriteAsync(requestBytes, 0, requestBytes.Length, token); var buffer = new byte[Constants.MaxResponseHeadersSize]; int read = await ReadUntilEndOfHeadersAsync(_dataNetworkStream, buffer); var ms = new MemoryStream(buffer, 0, read); var streamReader = new StreamReader(ms, Encoding.ASCII); string responseLine = streamReader.ReadLine(); if (responseLine == null) { throw new HttpBadResponseException("Empty response"); } string[] tokens = responseLine.Split(' '); if (tokens.Length != 3) { throw new HttpRequestException("Invalid first response line"); } HttpStatusCode statusCode = (HttpStatusCode)int.Parse(tokens[1]); if (statusCode == HttpStatusCode.OK) { return; } if (statusCode == HttpStatusCode.Unauthorized && !ConnectionParameters.Credentials.IsEmpty() && _authenticator == null) { NameValueCollection headers = HeadersParser.ParseHeaders(streamReader); string authenticateHeader = headers.Get(WellKnownHeaders.WwwAuthenticate); if (string.IsNullOrEmpty(authenticateHeader)) { throw new HttpBadResponseCodeException(statusCode); } _authenticator = Authenticator.Create(ConnectionParameters.Credentials, authenticateHeader); _streamDataClient.Dispose(); await ConnectAsync(token); return; } throw new HttpBadResponseCodeException(statusCode); }