示例#1
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;
                }
            }