Exemple #1
0
        private bool StopThread()
        {
            _toBreak = true;

            foreach (KeyValuePair<Socket, PeerClientData> dataPair in _receiverTransfers)
            {
                if (dataPair.Value.Socket != null)
                {
                    NackMessageData messageData = new NackMessageData();
                    Message message = new Message(new MessageHeader(ServiceTypes.NACK), messageData);
                    dataPair.Value.Socket.Send(message.Serialize());
                    dataPair.Value.Socket.Close();
                    if (dataPair.Value.FileStream != null)
                    {
                        dataPair.Value.FileStream.Close();
                    }
                }
            }

            foreach (KeyValuePair<Socket, PeerClientData> dataPair in _connectedClients)
            {
                if (dataPair.Value.Socket != null)
                {
                    NackMessageData messageData = new NackMessageData();
                    Message message = new Message(new MessageHeader(ServiceTypes.NACK), messageData);
                    dataPair.Value.Socket.Send(message.Serialize());
                    dataPair.Value.Socket.Close();
                    if (dataPair.Value.FileStream != null)
                    {
                        dataPair.Value.FileStream.Close();
                    }
                }
            }
            _listenSocket.Close();
            return true;
        }
Exemple #2
0
 private void RequestedFileList(IDictionary<int, string> fileList, Socket contactSocket)
 {
     FileListGetMessageData messageData = new FileListGetMessageData(fileList);
     Message newMessage = new Message(new MessageHeader(ServiceTypes.FILE_LIST_GET), messageData);
     contactSocket.Send(newMessage.Serialize());
     PeerClientData data = _connectedClients[contactSocket];
     data.FileId = -1;
     _connectedClients.Remove(contactSocket);
     data.Socket.Close();
 }
Exemple #3
0
        private IDictionary<int, string> GetFileListFromPeer(string contact, string address, ushort port)
        {
            try
            {
                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(new IPAddress(Dns.Resolve(address).AddressList[0].Address), port);
                Message message = new Message(new MessageHeader(ServiceTypes.FILE_LIST_GET), new FileListGetMessageData(new byte[] { 0, 0, 0, 0 }));
                clientSocket.Send(message.Serialize());

                byte[] headerBuffer = new byte[MessageHeader.HEADER_SIZE];
                clientSocket.Receive(headerBuffer);
                ushort contentLen = AMessageData.ToShort(headerBuffer[MessageHeader.HEADER_SIZE - 2], headerBuffer[MessageHeader.HEADER_SIZE - 1]);
                byte[] rawData = new byte[contentLen];
                clientSocket.Receive(rawData);
                byte[] buffer = new byte[headerBuffer.Length + contentLen];
                Array.Copy(headerBuffer, buffer, headerBuffer.Length);
                Array.Copy(rawData, 0, buffer, headerBuffer.Length, rawData.Length);
                FileListGetMessageData messageData = (FileListGetMessageData)Message.GetMessageData(new Message(buffer));
                return messageData.FileList;
            }
            catch (SocketException ex)
            {
                return new Dictionary<int, string>();
            }
        }
Exemple #4
0
        private void ReceiveData()
        {
            ArrayList receiverSockets = new ArrayList();
            inIteration = true;
            foreach (KeyValuePair<Socket, PeerClientData> dataPair in _receiverTransfers)
            {
                if (dataPair.Value.FileStream == null)
                {
                    dataPair.Value.FileStream = new FileStream(dataPair.Value.Alias, FileMode.OpenOrCreate);
                    AMessageData requestMessageData = new FileTransferGetMessageData(dataPair.Value.FileId);
                    Message toSend = new Message(new MessageHeader(ServiceTypes.FILE_TRANSFER_GET), requestMessageData);
                    dataPair.Value.Socket.Send(toSend.Serialize());
                }
                if(dataPair.Value.Socket.Connected)
                    receiverSockets.Add(dataPair.Value.Socket);
            }
            inIteration = false;

            if (receiverSockets.Count == 0)
                return;

            Socket.Select(receiverSockets, null, null, 1);
            for (int i = 0; i < receiverSockets.Count; i++)
            {
                SaveFileBuffer((Socket)receiverSockets[i]);
            }
        }
Exemple #5
0
 private void CancelFileTransfer(string contactName, int fileId)
 {
     lock (_receiverTransfers)
     {
         foreach (KeyValuePair<Socket, PeerClientData> clientDataPair in _receiverTransfers)
         {
             if (clientDataPair.Value.UserName == contactName && clientDataPair.Value.FileId == fileId)
             {
                 NackMessageData messageData = new NackMessageData();
                 Message message = new Message(new MessageHeader(ServiceTypes.NACK), messageData);
                 clientDataPair.Value.Socket.Send(message.Serialize());
                 clientDataPair.Value.Socket.Close();
                 _receiverTransfers.Remove(clientDataPair.Value.Socket);
                 clientDataPair.Value.FileStream.Close();
                 break;
             }
         }
     }
 }