Beispiel #1
0
            internal Peer(BitChatNetwork.VirtualPeer virtualPeer, BitChat bitchat)
            {
                _virtualPeer = virtualPeer;
                _bitchat = bitchat;

                _isSelfPeer = (_virtualPeer.PeerCertificate.IssuedTo.EmailAddress.Address == _bitchat._profile.LocalCertificateStore.Certificate.IssuedTo.EmailAddress.Address);

                _virtualPeer.PacketReceived += _virtualPeer_PacketReceived;
                _virtualPeer.StreamStateChanged += _virtualPeer_StreamStateChanged;
            }
Beispiel #2
0
 private void _network_VirtualPeerHasRevokedCertificate(BitChatNetwork sender, InvalidCertificateException ex)
 {
     if (PeerHasRevokedCertificate != null)
         RaiseEventPeerHasRevokedCertificate(ex);
 }
Beispiel #3
0
 private void _network_VirtualPeerSecureChannelException(BitChatNetwork sender, SecureChannelException ex)
 {
     if (PeerSecureChannelException != null)
         RaiseEventPeerSecureChannelException(ex);
 }
Beispiel #4
0
            private void _virtualPeer_PacketReceived(BitChatNetwork.VirtualPeer sender, Stream packetDataStream, IPEndPoint remotePeerEP)
            {
                switch (BitChatMessage.ReadType(packetDataStream))
                {
                    case BitChatMessageType.TypingNotification:
                        #region Typing Notification
                        {
                            if (_bitchat.PeerTyping != null)
                                _bitchat.RaiseEventPeerTyping(this);

                            break;
                        }
                        #endregion

                    case BitChatMessageType.Text:
                        #region Text
                        {
                            if (_bitchat.MessageReceived != null)
                                _bitchat.RaiseEventMessageReceived(this, BitChatMessage.ReadTextMessage(packetDataStream));

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileAdvertisement:
                        #region FileAdvertisement
                        {
                            SharedFile sharedFile = SharedFile.PrepareDownloadFile(BitChatMessage.ReadFileAdvertisement(packetDataStream), _bitchat, this, _bitchat._profile, _bitchat._syncCxt);

                            lock (_bitchat._sharedFiles)
                            {
                                if (_bitchat._sharedFiles.ContainsKey(sharedFile.MetaData.FileID))
                                {
                                    //file already exists
                                    if (sharedFile.IsComplete)
                                    {
                                        //remove the seeder
                                        sharedFile.RemovePeerOrSeeder(this);
                                    }
                                    else
                                    {
                                        sharedFile.AddChat(_bitchat);
                                        sharedFile.AddSeeder(this); //add the seeder

                                        byte[] packetData = BitChatMessage.CreateFileParticipate(sharedFile.MetaData.FileID);
                                        WritePacket(packetData, 0, packetData.Length);
                                    }
                                }
                                else
                                {
                                    //file doesnt exists
                                    _bitchat._sharedFiles.Add(sharedFile.MetaData.FileID, sharedFile);

                                    if (_bitchat.FileAdded != null)
                                        _bitchat.RaiseEventFileAdded(sharedFile);
                                }
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileBlockRequest:
                        #region FileBlockRequest
                        {
                            FileBlockRequest blockRequest = BitChatMessage.ReadFileBlockRequest(packetDataStream);
                            SharedFile sharedFile;

                            lock (_bitchat._sharedFiles)
                            {
                                sharedFile = _bitchat._sharedFiles[blockRequest.FileID];
                            }

                            if (sharedFile.State == FileSharing.SharedFileState.Paused)
                                return;

                            if (!sharedFile.PeerExists(this))
                                return;

                            FileBlockDataPart blockData = sharedFile.ReadBlock(blockRequest.BlockNumber, blockRequest.BlockOffset, blockRequest.Length);

                            byte[] packetData = BitChatMessage.CreateFileBlockResponse(blockData);
                            _virtualPeer.WritePacket(packetData, 0, packetData.Length);

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileBlockResponse:
                        #region FileBlockResponse
                        {
                            FileBlockDataPart blockData = BitChatMessage.ReadFileBlockData(packetDataStream);
                            SharedFile sharedFile;

                            lock (_bitchat._sharedFiles)
                            {
                                sharedFile = _bitchat._sharedFiles[blockData.FileID];
                            }

                            if (sharedFile.State == FileSharing.SharedFileState.Paused)
                                return;

                            if (!sharedFile.PeerExists(this))
                                return;

                            SharedFile.FileBlockDownloadManager downloadingBlock = sharedFile.GetDownloadingBlock(blockData.BlockNumber);
                            if (downloadingBlock != null)
                            {
                                if (downloadingBlock.IsThisDownloadPeerSet(this))
                                {
                                    if (!downloadingBlock.SetBlockData(blockData))
                                    {
                                        byte[] packetData = BitChatMessage.CreateFileBlockRequest(downloadingBlock.GetNextRequest());
                                        _virtualPeer.WritePacket(packetData, 0, packetData.Length);
                                    }
                                }
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileBlockWanted:
                        #region FileBlockWanted
                        {
                            FileBlockWanted blockWanted = BitChatMessage.ReadFileBlockWanted(packetDataStream);
                            SharedFile sharedFile;

                            lock (_bitchat._sharedFiles)
                            {
                                sharedFile = _bitchat._sharedFiles[blockWanted.FileID];
                            }

                            if (sharedFile.State == FileSharing.SharedFileState.Paused)
                                return;

                            if (!sharedFile.PeerExists(this))
                                return;

                            if (sharedFile.IsBlockAvailable(blockWanted.BlockNumber))
                            {
                                byte[] packetData = BitChatMessage.CreateFileBlockAvailable(blockWanted);
                                _virtualPeer.WritePacket(packetData, 0, packetData.Length);
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileBlockAvailable:
                        #region FileBlockAvailable
                        {
                            FileBlockWanted blockWanted = BitChatMessage.ReadFileBlockWanted(packetDataStream);
                            SharedFile sharedFile;

                            lock (_bitchat._sharedFiles)
                            {
                                sharedFile = _bitchat._sharedFiles[blockWanted.FileID];
                            }

                            if (sharedFile.IsComplete)
                                return;

                            if (sharedFile.State == FileSharing.SharedFileState.Paused)
                                return;

                            if (!sharedFile.PeerExists(this))
                                return;

                            SharedFile.FileBlockDownloadManager downloadingBlock = sharedFile.GetDownloadingBlock(blockWanted.BlockNumber);
                            if (downloadingBlock != null)
                            {
                                if (downloadingBlock.SetDownloadPeer(this))
                                {
                                    byte[] packetData = BitChatMessage.CreateFileBlockRequest(downloadingBlock.GetNextRequest());
                                    _virtualPeer.WritePacket(packetData, 0, packetData.Length);
                                }
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileShareParticipate:
                        #region FileShareParticipate
                        {
                            BinaryID fileID = BitChatMessage.ReadFileID(packetDataStream);

                            lock (_bitchat._sharedFiles)
                            {
                                _bitchat._sharedFiles[fileID].AddPeer(this);
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.FileShareUnparticipate:
                        #region FileShareUnparticipate
                        {
                            BinaryID fileID = BitChatMessage.ReadFileID(packetDataStream);

                            lock (_bitchat._sharedFiles)
                            {
                                _bitchat._sharedFiles[fileID].RemovePeerOrSeeder(this);
                            }

                            break;
                        }
                        #endregion

                    case BitChatMessageType.PeerExchange:
                        #region PeerExchange

                        List<PeerInfo> peerList = BitChatMessage.ReadPeerExchange(packetDataStream);

                        lock (_connectedPeerList)
                        {
                            //reason: for the lock to be valid for use
                            _connectedPeerList.Clear();
                            _connectedPeerList.AddRange(peerList);
                        }

                        _bitchat._network.MakeConnection(peerList);

                        //start network status check
                        _bitchat.TriggerUpdateNetworkStatus();
                        break;

                        #endregion

                    case BitChatMessageType.NOOP:
                        Debug.Write("Peer.PacketReceived", "NOOP received from: " + sender.PeerCertificate.IssuedTo.EmailAddress.Address + " [" + remotePeerEP.Address.ToString() + "]");
                        break;
                }
            }
Beispiel #5
0
        private void _network_VirtualPeerAdded(BitChatNetwork sender, BitChatNetwork.VirtualPeer virtualPeer)
        {
            Peer peer = new Peer(virtualPeer, this);

            lock (_peers)
            {
                _peers.Add(peer);
            }

            if (PeerAdded != null)
                RaiseEventPeerAdded(peer);
        }
Beispiel #6
0
        internal BitChat(IBitChatManager manager, BitChatProfile profile, BitChatNetwork network, BitChatProfile.SharedFileInfo[] sharedFileInfoList, Uri[] trackerURIs)
        {
            _manager = manager;
            _profile = profile;
            _network = network;
            _network.VirtualPeerAdded += _network_VirtualPeerAdded;
            _network.VirtualPeerHasRevokedCertificate += _network_VirtualPeerHasRevokedCertificate;
            _network.VirtualPeerSecureChannelException += _network_VirtualPeerSecureChannelException;

            foreach (BitChatNetwork.VirtualPeer virtualPeer in _network.GetVirtualPeerList())
            {
                Peer peer = new Peer(virtualPeer, this);

                if (peer.IsSelf)
                    _selfPeer = peer;

                _peers.Add(peer);
            }

            foreach (BitChatProfile.SharedFileInfo info in sharedFileInfoList)
            {
                try
                {
                    _sharedFiles.Add(info.FileMetaData.FileID, SharedFile.LoadFile(info, this, _syncCxt));
                }
                catch
                { }
            }

            //start tracking
            _manager.StartLocalTracking(_network.NetworkID);
            StartTracking(trackerURIs);

            //start noop timer
            _NOOPTimer = new Timer(NOOPTimerCallback, null, NOOP_PACKET_TIME_SECONDS, Timeout.Infinite);

            //start network update timer
            _updateNetworkStatusTimer = new Timer(UpdateNetworkStatusCallback, null, NETWORK_STATUS_TIMER_INTERVAL, Timeout.Infinite);
            _reCheckNetworkStatusTimer = new Timer(ReCheckNetworkStatusCallback, null, Timeout.Infinite, Timeout.Infinite);
        }
 public void RemoveNetwork(BitChatNetwork network)
 {
     lock (_networks)
     {
         _networks.Remove(network.NetworkID);
     }
 }
            public BitChat CreateBitChat(string networkName, string sharedSecret, BinaryID networkID, Certificate[] knownPeerCerts, BitChatProfile.SharedFileInfo[] sharedFileInfoList, Uri[] trackerURIs)
            {
                BitChatNetwork network = new BitChatNetwork(networkName, sharedSecret, networkID, knownPeerCerts, this, this);

                lock (_networks)
                {
                    _networks.Add(network.NetworkID, network);
                }

                if (trackerURIs == null)
                    trackerURIs = _profile.TrackerURIs;

                return new BitChat(this, _profile, network, sharedFileInfoList, trackerURIs);
            }
Beispiel #9
0
 internal VirtualPeer(Certificate peerCert, BitChatNetwork network)
 {
     _peerCert = peerCert;
     _network  = network;
 }
 internal VirtualPeer(Certificate peerCert, BitChatNetwork network)
 {
     _peerCert = peerCert;
     _network = network;
 }