private void ReadFrameAsync() { try { //frame parameters int signal; BinaryNumber channelId = new BinaryNumber(new byte[32]); byte[] dataLengthBuffer = new byte[2]; OffsetStream dataStream = new OffsetStream(_baseStream, 0, 0, true, false); while (true) { #region read frame from base stream //read frame signal signal = _baseStream.ReadByte(); if (signal == -1) { return; //End of stream } //read channel id _baseStream.ReadBytes(channelId.Value, 0, 32); //read data length _baseStream.ReadBytes(dataLengthBuffer, 0, 2); dataStream.Reset(0, BitConverter.ToUInt16(dataLengthBuffer, 0), 0); #endregion switch ((ConnectionSignal)signal) { case ConnectionSignal.PingRequest: WriteFrame(ConnectionSignal.PingResponse, channelId, null, 0, 0); break; case ConnectionSignal.PingResponse: //do nothing! break; case ConnectionSignal.ConnectChannelMeshNetwork: #region ConnectChannelMeshNetwork lock (_channels) { if (_channels.ContainsKey(channelId)) { WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0); } else { ChannelStream channel = new ChannelStream(this, channelId.Clone()); _channels.Add(channel.ChannelId, channel); ThreadPool.QueueUserWorkItem(delegate(object state) { try { //done async since the call is blocking and will block the current read thread which can cause DOS _connectionManager.Node.MeshNetworkRequest(this, channel.ChannelId, channel); } catch (Exception ex) { Debug.Write(this.GetType().Name, ex); channel.Dispose(); } }); } } //check if tcp relay is hosted for the channel. reply back tcp relay peers list if available Connection[] connections = _connectionManager.GetTcpRelayServerHostedNetworkConnections(channelId); if (connections.Length > 0) { int count = connections.Length; for (int i = 0; i < connections.Length; i++) { if (connections[i].RemotePeerEP.Equals(_remotePeerEP)) { connections[i] = null; count--; break; } } using (MemoryStream mS = new MemoryStream(128)) { BinaryWriter bW = new BinaryWriter(mS); bW.Write(Convert.ToByte(count)); foreach (Connection connection in connections) { if (connection != null) { connection.RemotePeerEP.WriteTo(bW); } } byte[] data = mS.ToArray(); WriteFrame(ConnectionSignal.MeshNetworkPeers, channelId, data, 0, data.Length); } } #endregion break; case ConnectionSignal.ChannelData: #region ChannelData try { ChannelStream channel = null; lock (_channels) { channel = _channels[channelId]; } channel.FeedReadBuffer(dataStream, _channelWriteTimeout); } catch { } #endregion break; case ConnectionSignal.DisconnectChannel: #region DisconnectChannel try { ChannelStream channel; lock (_channels) { channel = _channels[channelId]; _channels.Remove(channelId); } channel.SetDisconnected(); channel.Dispose(); } catch { } #endregion break; case ConnectionSignal.ConnectChannelTunnel: #region ConnectChannelTunnel if (IsStreamVirtualConnection(_baseStream)) { //nesting virtual connections not allowed WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0); } else { ChannelStream remoteChannel1 = null; lock (_channels) { if (_channels.ContainsKey(channelId)) { WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0); } else { //add first stream into list remoteChannel1 = new ChannelStream(this, channelId.Clone()); _channels.Add(remoteChannel1.ChannelId, remoteChannel1); } } if (remoteChannel1 != null) { EndPoint tunnelToRemotePeerEP = ConvertChannelIdToEp(channelId); //get remote peer ep Connection remotePeerConnection = _connectionManager.GetExistingConnection(tunnelToRemotePeerEP); //get remote channel service if (remotePeerConnection == null) { remoteChannel1.Dispose(); } else { try { //get remote proxy connection channel stream ChannelStream remoteChannel2 = remotePeerConnection.MakeVirtualConnection(_remotePeerEP); //join current and remote stream Joint joint = new Joint(remoteChannel1, remoteChannel2); joint.Disposed += delegate(object sender, EventArgs e) { lock (_tunnelJointList) { _tunnelJointList.Remove(sender as Joint); } }; lock (_tunnelJointList) { _tunnelJointList.Add(joint); } joint.Start(); } catch (Exception ex) { Debug.Write(this.GetType().Name, ex); remoteChannel1.Dispose(); } } } } #endregion break; case ConnectionSignal.ConnectChannelVirtualConnection: #region ConnectChannelVirtualConnection if (IsStreamVirtualConnection(_baseStream)) { //nesting virtual connections not allowed WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0); } else { lock (_channels) { if (_channels.ContainsKey(channelId)) { WriteFrame(ConnectionSignal.DisconnectChannel, channelId, null, 0, 0); } else { //add proxy channel stream into list ChannelStream channel = new ChannelStream(this, channelId.Clone()); _channels.Add(channel.ChannelId, channel); //pass channel as connection async ThreadPool.QueueUserWorkItem(delegate(object state) { try { _connectionManager.AcceptConnectionInitiateProtocol(channel, ConvertChannelIdToEp(channel.ChannelId)); } catch (Exception ex) { Debug.Write(this.GetType().Name, ex); channel.Dispose(); } }); } } } #endregion break; case ConnectionSignal.TcpRelayServerRegisterHostedNetwork: #region TcpRelayServerRegisterHostedNetwork _connectionManager.TcpRelayServerRegisterHostedNetwork(this, channelId.Clone()); _tcpRelayServerModeEnabled = true; #endregion break; case ConnectionSignal.TcpRelayServerUnregisterHostedNetwork: #region TcpRelayServerUnregisterHostedNetwork _connectionManager.TcpRelayServerUnregisterHostedNetwork(this, channelId); #endregion break; case ConnectionSignal.MeshNetworkPeers: #region MeshNetworkPeers { BinaryReader bR = new BinaryReader(dataStream); int count = bR.ReadByte(); List <EndPoint> peerEPs = new List <EndPoint>(count); for (int i = 0; i < count; i++) { peerEPs.Add(EndPointExtension.Parse(bR)); } _connectionManager.Node.ReceivedMeshNetworkPeersViaTcpRelay(this, channelId, peerEPs); } #endregion break; default: throw new IOException("Invalid frame signal."); } //discard any unread data if (dataStream.Length > dataStream.Position) { dataStream.CopyTo(Stream.Null, 1024, Convert.ToInt32(dataStream.Length - dataStream.Position)); } } } catch (ThreadAbortException) { //stopping } catch (Exception ex) { Debug.Write(this.GetType().Name, ex); } finally { Dispose(); } }
private void ReadFrameAsync() { try { //frame parameters int signalType; BinaryID channelName = new BinaryID(new byte[20]); int dataLength; byte[] dataBuffer = new byte[BUFFER_SIZE]; while (true) { #region Read frame from base stream //read frame signal signalType = _baseStream.ReadByte(); if (signalType == -1) { return; //End of stream } //read channel name OffsetStream.StreamRead(_baseStream, channelName.ID, 0, 20); //read data length OffsetStream.StreamRead(_baseStream, dataBuffer, 0, 2); dataLength = BitConverter.ToUInt16(dataBuffer, 0); //read data if (dataLength > 0) { OffsetStream.StreamRead(_baseStream, dataBuffer, 0, dataLength); } #endregion switch ((SignalType)signalType) { case SignalType.NOOP: break; case SignalType.ConnectChannelBitChatNetwork: #region ConnectChannelBitChatNetwork lock (_bitChatNetworkChannels) { if (_bitChatNetworkChannels.ContainsKey(channelName)) { WriteFrame(SignalType.DisconnectChannelBitChatNetwork, channelName, null, 0, 0); } else { ChannelStream channel = new ChannelStream(this, channelName.Clone(), ChannelType.BitChatNetwork); _bitChatNetworkChannels.Add(channel.ChannelName, channel); BitChatNetworkChannelRequest.BeginInvoke(this, channel.ChannelName, channel, null, null); } } //check if tcp relay is hosted for the channel. reply back tcp relay peers list if available try { List <IPEndPoint> peerEPs = TcpRelayService.GetPeerEPs(channelName, this); if ((peerEPs != null) && (peerEPs.Count > 0)) { using (MemoryStream mS = new MemoryStream(128)) { mS.WriteByte(Convert.ToByte(peerEPs.Count)); foreach (IPEndPoint peerEP in peerEPs) { IPEndPointParser.WriteTo(peerEP, mS); } byte[] data = mS.ToArray(); WriteFrame(SignalType.TcpRelayResponsePeerList, channelName, data, 0, data.Length); } } } catch { } #endregion break; case SignalType.DataChannelBitChatNetwork: #region DataChannelBitChatNetwork try { ChannelStream channel = null; lock (_bitChatNetworkChannels) { channel = _bitChatNetworkChannels[channelName]; } channel.WriteBuffer(dataBuffer, 0, dataLength, _channelWriteTimeout); } catch { } #endregion break; case SignalType.DisconnectChannelBitChatNetwork: #region DisconnectChannelBitChatNetwork try { ChannelStream channel; lock (_bitChatNetworkChannels) { channel = _bitChatNetworkChannels[channelName]; _bitChatNetworkChannels.Remove(channelName); } channel.SetDisconnected(); channel.Dispose(); } catch { } #endregion break; case SignalType.ConnectChannelProxyTunnel: #region ConnectChannelProxyTunnel { ChannelStream remoteChannel1 = null; lock (_proxyChannels) { if (_proxyChannels.ContainsKey(channelName)) { WriteFrame(SignalType.DisconnectChannelProxy, channelName, null, 0, 0); } else { //add first stream into list remoteChannel1 = new ChannelStream(this, channelName.Clone(), ChannelType.ProxyTunnel); _proxyChannels.Add(remoteChannel1.ChannelName, remoteChannel1); } } if (remoteChannel1 != null) { IPEndPoint tunnelToRemotePeerEP = ConvertChannelNameToEp(channelName); //get remote peer ep Connection remotePeerConnection = _connectionManager.GetExistingConnection(tunnelToRemotePeerEP); //get remote channel service if (remotePeerConnection == null) { remoteChannel1.Dispose(); } else { try { //get remote proxy connection channel stream ChannelStream remoteChannel2 = remotePeerConnection.RequestProxyConnection(_remotePeerEP); //join current and remote stream Joint joint = new Joint(remoteChannel1, remoteChannel2); joint.Disposed += joint_Disposed; lock (_proxyTunnelJointList) { _proxyTunnelJointList.Add(joint); } joint.Start(); } catch { remoteChannel1.Dispose(); } } } } #endregion break; case SignalType.ConnectChannelProxyConnection: #region ConnectChannelProxyConnection lock (_proxyChannels) { if (_proxyChannels.ContainsKey(channelName)) { WriteFrame(SignalType.DisconnectChannelProxy, channelName, null, 0, 0); } else { //add proxy channel stream into list ChannelStream channel = new ChannelStream(this, channelName.Clone(), ChannelType.ProxyTunnel); _proxyChannels.Add(channel.ChannelName, channel); //pass channel as connection async ThreadPool.QueueUserWorkItem(AcceptProxyConnectionAsync, channel); } } #endregion break; case SignalType.DataChannelProxy: #region DataChannelProxy try { ChannelStream channel = null; lock (_proxyChannels) { channel = _proxyChannels[channelName]; } channel.WriteBuffer(dataBuffer, 0, dataLength, _channelWriteTimeout); } catch { } #endregion break; case SignalType.DisconnectChannelProxy: #region DisconnectChannelProxy try { ChannelStream channel; lock (_proxyChannels) { channel = _proxyChannels[channelName]; _proxyChannels.Remove(channelName); } channel.SetDisconnected(); channel.Dispose(); } catch { } #endregion break; case SignalType.PeerStatusQuery: #region PeerStatusQuery try { if (_connectionManager.IsPeerConnectionAvailable(ConvertChannelNameToEp(channelName))) { WriteFrame(SignalType.PeerStatusAvailable, channelName, null, 0, 0); } } catch { } #endregion break; case SignalType.PeerStatusAvailable: #region PeerStatusAvailable try { lock (_peerStatusLockList) { object lockObject = _peerStatusLockList[channelName]; lock (lockObject) { Monitor.Pulse(lockObject); } } } catch { } #endregion break; case SignalType.StartTcpRelay: #region StartTcpRelay { BinaryID[] networkIDs; Uri[] trackerURIs; using (MemoryStream mS = new MemoryStream(dataBuffer, 0, dataLength, false)) { //read network id list networkIDs = new BinaryID[mS.ReadByte()]; byte[] XORnetworkID = new byte[20]; for (int i = 0; i < networkIDs.Length; i++) { mS.Read(XORnetworkID, 0, 20); byte[] networkID = new byte[20]; for (int j = 0; j < 20; j++) { networkID[j] = (byte)(channelName.ID[j] ^ XORnetworkID[j]); } networkIDs[i] = new BinaryID(networkID); } //read tracker uri list trackerURIs = new Uri[mS.ReadByte()]; byte[] data = new byte[255]; for (int i = 0; i < trackerURIs.Length; i++) { int length = mS.ReadByte(); mS.Read(data, 0, length); trackerURIs[i] = new Uri(Encoding.UTF8.GetString(data, 0, length)); } } lock (_tcpRelays) { foreach (BinaryID networkID in networkIDs) { if (!_tcpRelays.ContainsKey(networkID)) { TcpRelayService relay = TcpRelayService.StartTcpRelay(networkID, this, _connectionManager.LocalPort, _connectionManager.DhtClient, trackerURIs); _tcpRelays.Add(networkID, relay); } } } WriteFrame(SignalType.TcpRelayResponseSuccess, channelName, null, 0, 0); } #endregion break; case SignalType.StopTcpRelay: #region StopTcpRelay { BinaryID[] networkIDs; using (MemoryStream mS = new MemoryStream(dataBuffer, 0, dataLength, false)) { //read network id list networkIDs = new BinaryID[mS.ReadByte()]; byte[] XORnetworkID = new byte[20]; for (int i = 0; i < networkIDs.Length; i++) { mS.Read(XORnetworkID, 0, 20); byte[] networkID = new byte[20]; for (int j = 0; j < 20; j++) { networkID[j] = (byte)(channelName.ID[j] ^ XORnetworkID[j]); } networkIDs[i] = new BinaryID(networkID); } } lock (_tcpRelays) { foreach (BinaryID networkID in networkIDs) { if (_tcpRelays.ContainsKey(networkID)) { _tcpRelays[networkID].StopTcpRelay(this); _tcpRelays.Remove(networkID); } } } WriteFrame(SignalType.TcpRelayResponseSuccess, channelName, null, 0, 0); } #endregion break; case SignalType.TcpRelayResponseSuccess: #region TcpRelayResponseSuccess try { lock (_tcpRelayRequestLockList) { object lockObject = _tcpRelayRequestLockList[channelName]; lock (lockObject) { Monitor.Pulse(lockObject); } } } catch { } #endregion break; case SignalType.TcpRelayResponsePeerList: #region TcpRelayResponsePeerList using (MemoryStream mS = new MemoryStream(dataBuffer, 0, dataLength, false)) { int count = mS.ReadByte(); List <IPEndPoint> peerEPs = new List <IPEndPoint>(count); for (int i = 0; i < count; i++) { peerEPs.Add(IPEndPointParser.Parse(mS)); } TcpRelayPeersAvailable.BeginInvoke(this, channelName.Clone(), peerEPs, null, null); } #endregion break; case SignalType.DhtPacketData: #region DhtPacketData byte[] response = _connectionManager.DhtClient.ProcessPacket(dataBuffer, 0, dataLength, _remotePeerEP.Address); if (response != null) { WriteFrame(SignalType.DhtPacketData, BinaryID.GenerateRandomID160(), response, 0, response.Length); } #endregion break; case SignalType.BitChatNetworkInvitation: #region ChannelInvitationBitChatNetwork if (_connectionManager.Profile.AllowInboundInvitations) { BitChatNetworkInvitation.BeginInvoke(channelName.Clone(), _remotePeerEP, Encoding.UTF8.GetString(dataBuffer, 0, dataLength), null, null); } #endregion break; default: throw new IOException("Invalid frame signal type."); } } } catch { } finally { Dispose(); } }