public void Connect() { try { _isDuplexMode = true; var settings = ConnectionSettings.Default; _client = new TcpClient(); _client.Connect(settings.Host, settings.DataPort); var stream = _client.GetStream(); stream.Write(new AceptHeader { Username = settings.Username, Password = settings.Password, Flag = 17767, SocketType = 2, Misc = 0 }.ToBytes()); stream.Flush(); var commHeader = CommHeader.Read(stream); switch (commHeader.ErrorCode) { case 10: case 8: throw new InvalidOperationException("Failed to open audio backchannel."); } var tcpInfoHeader = TalkInfoHeader.Read(stream); AudioSamples = tcpInfoHeader.AudioSamples; AudioChannels = tcpInfoHeader.AudioChannels; AudioBits = tcpInfoHeader.AudioBits; _frameFactory = new ExtFrameAudioPacketFactory(tcpInfoHeader.AudioEncodeType, tcpInfoHeader.AudioSamples, tcpInfoHeader.AudioChannels); IsOpen = true; } catch { Disconnect(); throw; } }
private WorkSpace ReceiveVariableLengthReply(Type expectedType) { List <byte> byteList = new List <byte>(); int count = Marshal.SizeOf(typeof(CommHeader)); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (byteList.Count < count && stopwatch.ElapsedMilliseconds < 45000L) { byteList.AddRange((IEnumerable <byte>) this._connection.Receive()); } if (byteList.Count < count) { throw new ConnectionLostException(string.Format("Type: {0} size is incorrect. Received size is {1}, but expected size is {2}.", (object)expectedType, (object)byteList.Count, (object)count)); } byte[] numArray = new byte[count]; byteList.CopyTo(0, numArray, 0, count); CommHeader structure = new CommHeader(); IvaUtils.BytesToStructure <CommHeader>(ref structure, numArray); int num1 = structure.lBodyLength + count; while (byteList.Count < num1 && stopwatch.ElapsedMilliseconds < 45000L) { byteList.AddRange((IEnumerable <byte>) this._connection.Receive()); } if (byteList.Count != num1) { throw new ConnectionLostException(string.Format("Type: {0} size is incorrect. Received size is {1}, but expected size is {2}.", (object)expectedType, (object)byteList.Count, (object)num1)); } byte[] array = byteList.ToArray(); IntPtr num2 = Marshal.AllocHGlobal(array.Length); Marshal.Copy(array, 0, num2, array.Length); return(new WorkSpace(expectedType, num2, array.Length)); }