Ejemplo n.º 1
0
        public void HandleTransferAccepted(object sender, EventArgs e)
        {
            CLanFileTransferRequest req = sender as CLanFileTransferRequest;
            CLanFileTransfer        cft = new CLanFileTransfer(req.From, req.Files, CLanTransferType.RECEIVE);

            cft.Start();    // Start the working thread, that will also ask for the save directory
        }
Ejemplo n.º 2
0
        public void HandleTransferRefused(object sender, EventArgs e)
        {
            // Decline the request
            CLanFileTransferRequest req = sender as CLanFileTransferRequest;

            byte[] toSend = new Message(App.me, MessageType.NACK, "Maybe next time :/").ToByteArray();
            Send(toSend, socketBuffer[req.From]);

            // Flush the buffer
            socketBuffer.Remove(req.From);
        }
Ejemplo n.º 3
0
        public void HandleAccept(Socket client)
        {
            // This method is already being executed in a separate thread
            Trace.WriteLine("New connection!");
            byte[]  data = Receive(client);
            Message m    = Message.GetMessage(data);

            User source = m.sender;

            socketBuffer.Add(source, client);

            CLanFileTransferRequest req = CLanFileTransferRequest.GetRequest(m.message.ToString());

            req.TransferAccepted += HandleTransferAccepted;
            req.TransferRefused  += HandleTransferRefused;
            req.Prompt();
        }
Ejemplo n.º 4
0
        private void WorkerStartSend(object sender, DoWorkEventArgs e)
        {
            Trace.WriteLine("CTF.CS - WORKERSTARTSEND");
            // The sender will see the transfer window with a "waiting state" until the other answers

            try
            {
                currentSocket = TCPManager.GetConnection(Other);
                CLanFileTransferRequest req = new CLanFileTransferRequest(App.me, Other, Files);
                byte[] requestData          = new Message(App.me, MessageType.SEND, req).ToByteArray();

                if (requestData.Length > 65535)
                {
                    // Will not fit a UDP packet, inform the user and make him select less files
                    MessageBoxResult result = MessageBox.Show(
                        "You are trying to send too many files. Cannot complete transfer.",
                        "Oops, something went wrong...",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error
                        );
                    e.Cancel = true;
                    return;
                }

                Store();

                TCPManager.Send(requestData, currentSocket);

                byte[] responseData = TCPManager.Receive(currentSocket);
                if (responseData != null)
                {
                    Message responseMessage = Message.GetMessage(responseData);
                    switch (responseMessage.messageType)
                    {
                    case MessageType.ACK:
                        // Destination accepted the transfer
                        // Show the window with all file transfers
                        TCPManager.SendFiles(this);
                        break;

                    case MessageType.NACK:
                        // Destination refused the transfer
                        Trace.WriteLine("Destination refused the transfer");
                        e.Cancel = true;        // Suicide
                        break;

                    default:
                        Trace.WriteLine("This message should not be here, check your code");
                        break;
                    }
                }
                else
                {
                    Trace.WriteLine("An error occured receiving from Other");
                    e.Cancel = true;    // Suicide
                }
                // CancellationPending is checked by the SendFiles, that terminates when it is set to True
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Socket exception sending request");
                e.Cancel = true;
                return;
            }
        }