Esempio n. 1
0
        private void ReadDataAsync()
        {
            try
            {
                int         channelDataLength;
                int         frameSignal;
                byte[]      channelNameBuffer = new byte[20];
                BinaryID    channelName       = new BinaryID(channelNameBuffer);
                ChannelType channelType;
                byte[]      buffer = new byte[65536];

                while (true)
                {
                    //read frame signal
                    frameSignal = _baseStream.ReadByte();

                    //read channel name
                    OffsetStream.StreamRead(_baseStream, channelNameBuffer, 0, 20);

                    switch (frameSignal)
                    {
                    case SIGNAL_DATA:
                    {
                        //read channel type
                        channelType = (ChannelType)_baseStream.ReadByte();

                        OffsetStream.StreamRead(_baseStream, buffer, 0, 2);
                        channelDataLength = BitConverter.ToUInt16(buffer, 0) + 1;
                        OffsetStream.StreamRead(_baseStream, buffer, 0, channelDataLength);

                        //switch frame
                        ChannelStream channel = null;

                        try
                        {
                            switch (channelType)
                            {
                            case ChannelType.BitChatNetwork:
                                lock (_bitChatNetworkChannels)
                                {
                                    channel = _bitChatNetworkChannels[channelName];
                                }

                                channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                break;

                            case ChannelType.ProxyTunnel:
                                lock (_proxyTunnelChannels)
                                {
                                    channel = _proxyTunnelChannels[channelName];
                                }

                                channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                break;

                            case ChannelType.VirtualConnection:
                                lock (_virtualConnectionChannels)
                                {
                                    channel = _virtualConnectionChannels[channelName];
                                }

                                channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                break;
                            }
                        }
                        catch
                        {
                            if (channel != null)
                            {
                                channel.Dispose();
                            }
                        }
                    }
                    break;

                    case SIGNAL_CONNECT_BIT_CHAT_NETWORK:
                    {
                        ChannelStream channel = null;

                        try
                        {
                            channel = new ChannelStream(this, channelName, ChannelType.BitChatNetwork);

                            lock (_bitChatNetworkChannels)
                            {
                                _bitChatNetworkChannels.Add(channelName, channel);
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_BIT_CHAT_NETWORK; channel: " + channelName.ToString());

                            _requestHandler.BeginInvoke(this, channelName, ChannelType.BitChatNetwork, channel, null, null);
                        }
                        catch
                        {
                            if (channel != null)
                            {
                                channel.Dispose();
                            }
                        }
                    }
                    break;

                    case SIGNAL_DISCONNECT_BIT_CHAT_NETWORK:
                        try
                        {
                            lock (_bitChatNetworkChannels)
                            {
                                _bitChatNetworkChannels[channelName].Dispose();
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_BIT_CHAT_NETWORK; channel: " + channelName.ToString());
                        }
                        catch
                        { }
                        break;

                    case SIGNAL_PEER_STATUS:
                        try
                        {
                            if (_connectionManager.IsPeerConnectionAvailable(ConvertToIP(channelName.ID)))
                            {
                                WriteSignalFrame(channelName, SIGNAL_PEER_STATUS_AVAILABLE);
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_PEER_STATUS; peerIP: " + ConvertToIP(channelName.ID).ToString());
                        }
                        catch
                        { }
                        break;

                    case SIGNAL_PEER_STATUS_AVAILABLE:
                        try
                        {
                            lock (_peerStatusLockList)
                            {
                                object lockObject = _peerStatusLockList[channelName];

                                lock (lockObject)
                                {
                                    Monitor.Pulse(lockObject);
                                }
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_PEER_STATUS_AVAILABLE; peerIP: " + ConvertToIP(channelName.ID).ToString());
                        }
                        catch
                        { }
                        break;

                    case SIGNAL_CONNECT_PROXY_TUNNEL:
                    {
                        ChannelStream remoteChannel1 = null;
                        Stream        remoteChannel2 = null;

                        try
                        {
                            //get remote peer ep
                            IPEndPoint tunnelToPeerEP = ConvertToIP(channelName.ID);

                            //add first stream into list
                            remoteChannel1 = new ChannelStream(this, channelName, ChannelType.ProxyTunnel);

                            lock (_proxyTunnelChannels)
                            {
                                _proxyTunnelChannels.Add(channelName, remoteChannel1);
                            }

                            //get remote channel service
                            Connection remotePeerConnection = _connectionManager.GetExistingConnection(tunnelToPeerEP);

                            //get remote stream for virtual connection
                            remoteChannel2 = remotePeerConnection.RequestVirtualConnectionChannel(_remotePeerEP);

                            //join current and remote stream
                            Joint joint = new Joint(remoteChannel1, remoteChannel2);
                            joint.Disposed += joint_Disposed;

                            lock (_tunnelJointList)
                            {
                                _tunnelJointList.Add(joint);
                            }

                            joint.Start();

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_PROXY_TUNNEL; tunnel to peerIP: " + tunnelToPeerEP.ToString());
                        }
                        catch
                        {
                            if (remoteChannel1 != null)
                            {
                                remoteChannel1.Dispose();
                            }

                            if (remoteChannel2 != null)
                            {
                                remoteChannel2.Dispose();
                            }
                        }
                    }
                    break;

                    case SIGNAL_DISCONNECT_PROXY_TUNNEL:
                        try
                        {
                            lock (_proxyTunnelChannels)
                            {
                                _proxyTunnelChannels[channelName].Dispose();
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_PROXY_TUNNEL; channel: " + channelName.ToString());
                        }
                        catch
                        { }
                        break;

                    case SIGNAL_CONNECT_VIRTUAL_CONNECTION:
                    {
                        ChannelStream channel = null;

                        try
                        {
                            //add current stream into list
                            channel = new ChannelStream(this, channelName, ChannelType.VirtualConnection);

                            lock (_virtualConnectionChannels)
                            {
                                _virtualConnectionChannels.Add(channelName, channel);
                            }

                            IPEndPoint virtualRemotePeerEP = ConvertToIP(channelName.ID);

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_VIRTUAL_CONNECTION; tunnel from peerIP: " + virtualRemotePeerEP.ToString());

                            //pass channel as virtual connection async
                            ThreadPool.QueueUserWorkItem(AcceptVirtualConnectionAsync, new object[] { channel, virtualRemotePeerEP });
                        }
                        catch
                        {
                            if (channel != null)
                            {
                                channel.Dispose();
                            }
                        }
                    }
                    break;

                    case SIGNAL_DISCONNECT_VIRTUAL_CONNECTION:
                        try
                        {
                            lock (_virtualConnectionChannels)
                            {
                                _virtualConnectionChannels[channelName].Dispose();
                            }

                            Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_VIRTUAL_CONNECTION; channel: " + channelName.ToString());
                        }
                        catch
                        { }
                        break;

                    default:
                        throw new IOException("Invalid ChannelManager frame type.");
                    }
                }
            }
            catch
            { }
            finally
            {
                Dispose();
            }
        }
Esempio n. 2
0
        private void WriteSignalFrame(BinaryID channelName, byte signal)
        {
            Debug.Write("Connection.WriteSignalFrame", _remotePeerEP.ToString() + "; channel:" + channelName.ToString() + "; signal:" + signal);

            lock (_baseStream)
            {
                //write frame signal
                _writeBufferData[0] = signal;

                //write channel name
                Buffer.BlockCopy(channelName.ID, 0, _writeBufferData, 1, 20);

                //output to base stream
                _baseStream.Write(_writeBufferData, 0, 21);
                _baseStream.Flush();
            }
        }
Esempio n. 3
0
        private void WriteSignalFrame(BinaryID channelName, byte signal)
        {
            Debug.Write("Connection.WriteSignalFrame", _remotePeerEP.ToString() + "; channel:" + channelName.ToString() + "; signal:" + signal);

            lock (_baseStream)
            {
                //write frame signal
                _writeBufferData[0] = signal;

                //write channel name
                Buffer.BlockCopy(channelName.ID, 0, _writeBufferData, 1, 20);

                //output to base stream
                _baseStream.Write(_writeBufferData, 0, 21);
                _baseStream.Flush();
            }
        }
Esempio n. 4
0
 public override string ToString()
 {
     return(_nodeID.ToString());
 }
Esempio n. 5
0
        private void ReadDataAsync()
        {
            try
            {
                int channelDataLength;
                int frameSignal;
                byte[] channelNameBuffer = new byte[20];
                BinaryID channelName = new BinaryID(channelNameBuffer);
                ChannelType channelType;
                byte[] buffer = new byte[65536];

                while (true)
                {
                    //read frame signal
                    frameSignal = _baseStream.ReadByte();

                    //read channel name
                    OffsetStream.StreamRead(_baseStream, channelNameBuffer, 0, 20);

                    switch (frameSignal)
                    {
                        case SIGNAL_DATA:
                            {
                                //read channel type
                                channelType = (ChannelType)_baseStream.ReadByte();

                                OffsetStream.StreamRead(_baseStream, buffer, 0, 2);
                                channelDataLength = BitConverter.ToUInt16(buffer, 0) + 1;
                                OffsetStream.StreamRead(_baseStream, buffer, 0, channelDataLength);

                                //switch frame
                                ChannelStream channel = null;

                                try
                                {
                                    switch (channelType)
                                    {
                                        case ChannelType.BitChatNetwork:
                                            lock (_bitChatNetworkChannels)
                                            {
                                                channel = _bitChatNetworkChannels[channelName];
                                            }

                                            channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                            break;

                                        case ChannelType.ProxyTunnel:
                                            lock (_proxyTunnelChannels)
                                            {
                                                channel = _proxyTunnelChannels[channelName];
                                            }

                                            channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                            break;

                                        case ChannelType.VirtualConnection:
                                            lock (_virtualConnectionChannels)
                                            {
                                                channel = _virtualConnectionChannels[channelName];
                                            }

                                            channel.WriteBuffer(buffer, 0, channelDataLength, _channelWriteTimeout);
                                            break;
                                    }
                                }
                                catch
                                {
                                    if (channel != null)
                                        channel.Dispose();
                                }
                            }
                            break;

                        case SIGNAL_CONNECT_BIT_CHAT_NETWORK:
                            {
                                ChannelStream channel = null;

                                try
                                {
                                    channel = new ChannelStream(this, channelName, ChannelType.BitChatNetwork);

                                    lock (_bitChatNetworkChannels)
                                    {
                                        _bitChatNetworkChannels.Add(channelName, channel);
                                    }

                                    Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_BIT_CHAT_NETWORK; channel: " + channelName.ToString());

                                    _requestHandler.BeginInvoke(this, channelName, ChannelType.BitChatNetwork, channel, null, null);
                                }
                                catch
                                {
                                    if (channel != null)
                                        channel.Dispose();
                                }
                            }
                            break;

                        case SIGNAL_DISCONNECT_BIT_CHAT_NETWORK:
                            try
                            {
                                lock (_bitChatNetworkChannels)
                                {
                                    _bitChatNetworkChannels[channelName].Dispose();
                                }

                                Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_BIT_CHAT_NETWORK; channel: " + channelName.ToString());
                            }
                            catch
                            { }
                            break;

                        case SIGNAL_PEER_STATUS:
                            try
                            {
                                if (_connectionManager.IsPeerConnectionAvailable(ConvertToIP(channelName.ID)))
                                    WriteSignalFrame(channelName, SIGNAL_PEER_STATUS_AVAILABLE);

                                Debug.Write("Connection.ReadDataAsync", "SIGNAL_PEER_STATUS; peerIP: " + ConvertToIP(channelName.ID).ToString());
                            }
                            catch
                            { }
                            break;

                        case SIGNAL_PEER_STATUS_AVAILABLE:
                            try
                            {
                                lock (_peerStatusLockList)
                                {
                                    object lockObject = _peerStatusLockList[channelName];

                                    lock (lockObject)
                                    {
                                        Monitor.Pulse(lockObject);
                                    }
                                }

                                Debug.Write("Connection.ReadDataAsync", "SIGNAL_PEER_STATUS_AVAILABLE; peerIP: " + ConvertToIP(channelName.ID).ToString());
                            }
                            catch
                            { }
                            break;

                        case SIGNAL_CONNECT_PROXY_TUNNEL:
                            {
                                ChannelStream remoteChannel1 = null;
                                Stream remoteChannel2 = null;

                                try
                                {
                                    //get remote peer ep
                                    IPEndPoint tunnelToPeerEP = ConvertToIP(channelName.ID);

                                    //add first stream into list
                                    remoteChannel1 = new ChannelStream(this, channelName, ChannelType.ProxyTunnel);

                                    lock (_proxyTunnelChannels)
                                    {
                                        _proxyTunnelChannels.Add(channelName, remoteChannel1);
                                    }

                                    //get remote channel service
                                    Connection remotePeerConnection = _connectionManager.GetExistingConnection(tunnelToPeerEP);

                                    //get remote stream for virtual connection
                                    remoteChannel2 = remotePeerConnection.RequestVirtualConnectionChannel(_remotePeerEP);

                                    //join current and remote stream
                                    Joint joint = new Joint(remoteChannel1, remoteChannel2);
                                    joint.Disposed += joint_Disposed;

                                    lock (_tunnelJointList)
                                    {
                                        _tunnelJointList.Add(joint);
                                    }

                                    joint.Start();

                                    Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_PROXY_TUNNEL; tunnel to peerIP: " + tunnelToPeerEP.ToString());
                                }
                                catch
                                {
                                    if (remoteChannel1 != null)
                                        remoteChannel1.Dispose();

                                    if (remoteChannel2 != null)
                                        remoteChannel2.Dispose();
                                }
                            }
                            break;

                        case SIGNAL_DISCONNECT_PROXY_TUNNEL:
                            try
                            {
                                lock (_proxyTunnelChannels)
                                {
                                    _proxyTunnelChannels[channelName].Dispose();
                                }

                                Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_PROXY_TUNNEL; channel: " + channelName.ToString());
                            }
                            catch
                            { }
                            break;

                        case SIGNAL_CONNECT_VIRTUAL_CONNECTION:
                            {
                                ChannelStream channel = null;

                                try
                                {
                                    //add current stream into list
                                    channel = new ChannelStream(this, channelName, ChannelType.VirtualConnection);

                                    lock (_virtualConnectionChannels)
                                    {
                                        _virtualConnectionChannels.Add(channelName, channel);
                                    }

                                    IPEndPoint virtualRemotePeerEP = ConvertToIP(channelName.ID);

                                    Debug.Write("Connection.ReadDataAsync", "SIGNAL_CONNECT_VIRTUAL_CONNECTION; tunnel from peerIP: " + virtualRemotePeerEP.ToString());

                                    //pass channel as virtual connection async
                                    ThreadPool.QueueUserWorkItem(AcceptVirtualConnectionAsync, new object[] { channel, virtualRemotePeerEP });
                                }
                                catch
                                {
                                    if (channel != null)
                                        channel.Dispose();
                                }
                            }
                            break;

                        case SIGNAL_DISCONNECT_VIRTUAL_CONNECTION:
                            try
                            {
                                lock (_virtualConnectionChannels)
                                {
                                    _virtualConnectionChannels[channelName].Dispose();
                                }

                                Debug.Write("Connection.ReadDataAsync", "SIGNAL_DISCONNECT_VIRTUAL_CONNECTION; channel: " + channelName.ToString());
                            }
                            catch
                            { }
                            break;

                        default:
                            throw new IOException("Invalid ChannelManager frame type.");
                    }
                }
            }
            catch
            { }
            finally
            {
                Dispose();
            }
        }