Esempio n. 1
0
        private void sendClipboardDatas()
        {
            //MessageBox.Show("Sending clipboard to server:" + i.ToString() + " .\nYou will be advised when the transfer is completed.", "Starting Transfer", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Socket s = _host.ds(i);

            if (s == null)
            {
                EnableCB();
                return;
            }

            IDataObject clipboardData = Clipboard.GetDataObject();

            string[] formats = clipboardData.GetFormats();

            try{
                foreach (string format in formats)
                {
                    //Console.WriteLine("Analizing clipboard. FORMAT FOUND: " + format);
                    if (format == DataFormats.FileDrop)
                    {
                        continue;
                    }
                    MsgStream.Send(new DataMsgCBP(format, clipboardData.GetData(format)), _host.ds(i));
                }

                /* SENDING FILE TYPE */

                if (Clipboard.ContainsFileDropList())
                {
                    long size = ClipboardFiles.GetCBFilesSize();
                    if (size > ClipboardFiles.MaxSize)
                    {
                        if (MessageBox.Show("The size of clipboard's content is greater than MaxSize: " + ClipboardFiles.MaxSize
                                            + " \nConfirm the transfer?", "Closing Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                        {
                            EnableCB();
                            return;
                        }
                    }

                    MsgStream.Send(new InitFileCBP(), s);
                    ClipboardFiles.SendClipboardFiles(s);
                    MsgStream.Send(new StopFileCBP(), s);
                    ConfirmCBP m = (ConfirmCBP)MsgStream.Receive(s);
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Ops...\nSomething goes wrong during Clipboard Transfer[on Sending].\nThe connection will be closed.\nTry again later.",
                                                     "Clipboard Transfer Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                this.DisconnectionReq();
                Console.WriteLine("Clipboard file transfer error: " + e.Message);
            }


            EnableCB();
            return;
        }
Esempio n. 2
0
        private void Connect()
        {
            Socket tmpE = new Socket(SocketType.Stream, ProtocolType.Tcp); //event socket
            Socket tmpD = new Socket(SocketType.Stream, ProtocolType.Tcp); //clipboard socket
            object o    = null;

            try
            {
                Console.WriteLine("Connecting EventSocket To -> " + IP + ":" + EP.ToString());
                tmpE.Connect(IP, EP);

                /* Authentication on EventSocket*/

                MsgStream.Send(new AuthMsg(PSW), tmpE);
                o = MsgStream.Receive(tmpE);

                if ((o is AckMsg) && (((AckMsg)o).ack))
                {
                    Console.WriteLine("Connection completed successful on EventSocket.");
                }
                else
                {
                    throw new Exception();
                }


                Console.WriteLine("Connecting DataSocket To -> " + IP + ":" + DP.ToString());
                tmpD.Connect(IP, DP);

                /* Authentication on DataSocket */
                Console.WriteLine("Starting authentication protocol.");
                MsgStream.Send(new AuthMsg(PSW), tmpD);
                o = MsgStream.Receive(tmpD);

                if ((o is AckMsg) && (((AckMsg)o).ack))
                {
                    Console.WriteLine("Connection completed successful on DataSocket.");
                }
                else
                {
                    throw new Exception();
                }

                _host.es(tmpE, i);
                _host.ds(tmpD, i);
                this.Connected();
            }
            catch (Exception)
            {
                tmpE.Close();
                tmpD.Close();
                this.Disconnected();
            }
        }
Esempio n. 3
0
            public override void Update()
            {
                base.Update();

                if (_obj is DataMsgCBP)
                {
                    DataMsgCBP dataMsgCBP = (DataMsgCBP)_obj;
                    Clipboard.SetData(dataMsgCBP.format, dataMsgCBP.content);
                }
                else if (_obj is InitFileCBP)
                {
                    ClipboardFiles.RecvClipboardFiles(Server.CommSocket);
                }
                else if (_obj is GetMsgCBP)
                {
                    IDataObject clipboardData = Clipboard.GetDataObject();
                    string[]    formats       = clipboardData.GetFormats();
                    foreach (string format in formats)
                    {
                        if (format == DataFormats.FileDrop)
                        {
                            continue;
                        }
                        Console.WriteLine("Format: " + format);
                        MsgStream.Send(new DataMsgCBP(format, clipboardData.GetData(format)),
                                       Server.CommSocket);
                    }

                    if (Clipboard.ContainsFileDropList())
                    {
                        if (ClipboardFiles.GetCBFilesSize() > ClipboardFiles.MaxSize)
                        {
                            MsgStream.Send(new MaxSizeCBP(), Server.CommSocket);
                            Object response = MsgStream.Receive(Server.CommSocket);
                            if (!(response is ConfirmCBP))
                            {
                                return;
                            }
                        }
                        MsgStream.Send(new InitFileCBP(), Server.CommSocket);
                        ClipboardFiles.SendClipboardFiles(Server.CommSocket);
                        MsgStream.Send(new StopFileCBP(), Server.CommSocket);
                        MsgStream.Receive(Server.CommSocket); // wait for an ack
                    }
                    else
                    {
                        MsgStream.Send(new StopFileCBP(), Server.CommSocket);
                    }
                }
            }
Esempio n. 4
0
            public override void Update()
            {
                base.Update();

                if (_obj is AuthMsg)
                {
                    // authentication
                    AuthMsg authMsg = (AuthMsg)_obj;
                    if (authMsg.psw == Server._password)
                    {
                        MsgStream.Send(new AckMsg(true), Server._commSocket);
                        Server.SetAuthenticated();
                    }
                    else
                    {
                        MsgStream.Send(new AckMsg(false), Server._commSocket);
                        Server.State = new WaitingState();
                    }
                }
            }
Esempio n. 5
0
        public void SendMsg()
        {
            Message m;
            int     i = 0;

            while (true)
            {
                while (_ee.WaitOne())
                {
                    lock (_eq)
                    {
                        while (true)
                        {
                            try
                            {
                                m = _eq.Dequeue();
                                _eqToSend.Enqueue(m);
                                i++;
                            }
                            catch (InvalidOperationException)
                            {
                                break;
                            }
                        }
                    }

                    while (i != 0)
                    {
                        m = _eqToSend.Dequeue();
                        i--;

                        /* checking type */

                        if ((m is StopComm) && (_eas == ((StopComm)m).i))
                        {
                            continue;
                        }

                        if (m is InitComm)
                        {
                            if (_eas != -1)
                            {
                                if (_es[_eas] == null)
                                {
                                    _eas = -1;
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            _eas = ((InitComm)m).i; // changing active socket
                        }

                        if ((_eas != -1) && (_es[_eas] != null))
                        {
                            try
                            {
                                MsgStream.Send(m, _es[_eas]); //sending data
                                //Console.WriteLine("Sent");
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Error during event comunication: " + e.Message);
                                System.Windows.Forms.MessageBox.Show("Ops...\nSomething goes wrong during Events Transfer.\nThe connection will be closed.\nTry again later.",
                                                                     "Event Transfer Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                                _sp[_eas].DisconnectionReq();
                                _eas = -1;
                                continue;
                            }

                            if (m is InitComm)
                            {
                                _sp[_eas].Activation();
                                continue;
                            }

                            if (m is StopComm)
                            {
                                //Console.WriteLine("StopComm Msg Processed Well");
                                _sp[_eas].Deactivation();
                                _eas = -1;  // no active socket from now till a new InitComm Message
                            }
                        }
                    } //end sending loop
                }     //end wait on event
            }         //end infinite loop
        }             //end SendMsg
Esempio n. 6
0
        public void ReceiveCBMsg()
        {
            Message m;
            int     i = 0;

            while (true)
            {
                while (_de.WaitOne())
                {
                    lock (_dq) { m = _dq.Dequeue(); }
                    i = ((GetMsgCBP)m).i;

                    //Console.WriteLine("Processing Getting RQ to: " + i);

                    if ((i != 0) && (i != 1) && (i != 2) && (i != 3))
                    {
                        continue;
                    }

                    if (_ds[i] != null)
                    {
                        _sp[i].CBGetting();

                        try
                        {
                            MsgStream.Send(m, _ds[i]);
                            //Console.WriteLine("richiesta di ricezione inviata");

                            do
                            {
                                m = (Message)MsgStream.Receive(_ds[i]);

                                if (m is DataMsgCBP)
                                {
                                    DataMsgCBP dataMsgCBP = (DataMsgCBP)m;
                                    //Console.WriteLine("ricevuto pacchetto: " + dataMsgCBP.format);
                                    System.Windows.Forms.Clipboard.SetData(dataMsgCBP.format, dataMsgCBP.content);
                                }


                                if (m is InitFileCBP) //FILEMSG
                                {
                                    /*
                                     * System.Windows.Forms.MessageBox.Show("Receiving file/s from server: " + i.ToString()
                                     + " .\nYou will be advised when the transfer is completed.", "Starting File/s Transfer",
                                     +  System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                                     */

                                    ClipboardFiles.RecvClipboardFiles(_ds[i]);
                                    Console.WriteLine("CBP : Received Files.");

                                    /*
                                     *
                                     * System.Windows.Forms.MessageBox.Show("File/s received from server: " + i.ToString()
                                     + " .", "Transfer completed",
                                     +  System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                                     */
                                    _sp[i].EnableCB();
                                    break;
                                }

                                if (m is MaxSizeCBP)
                                {
                                    if (System.Windows.Forms.MessageBox.Show("The size of clipboard's content is greater than MaxSize: " + ClipboardFiles.MaxSize
                                                                             + " \nConfirm the transfer?", "File size exceeding", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Receiving file/s from server: " + i.ToString()
                                                                             + " .\nYou will be advised when the transfer is completed.", "Starting File/s Transfer",
                                                                             System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

                                        MsgStream.Send(new ConfirmCBP(), _ds[i]);
                                        ClipboardFiles.RecvClipboardFiles(_ds[i]);

                                        System.Windows.Forms.MessageBox.Show("File/s received from server: " + i.ToString()
                                                                             + " .", "Transfer completed",
                                                                             System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

                                        _sp[i].EnableCB();
                                        break;
                                    }
                                    else
                                    {
                                        MsgStream.Send(new StopFileCBP(), _ds[i]);
                                        _sp[i].EnableCB();
                                        break;
                                    }
                                }
                            }while(!(m is StopFileCBP));

                            _sp[i].EnableCB();
                        }
                        catch (Exception e)
                        {
                            //Console.WriteLine("Errore nella richiesta CB: chiusura sockets e disconnessione.");
                            System.Windows.Forms.MessageBox.Show("Ops...\nSomething goes wrong during Clipboard Transfer[on Receiving].\nThe connection will be closed.\nTry again later.",
                                                                 "Clipboard Transfer Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                            _sp[i].DisconnectionReq();
                            Console.WriteLine("Clipboard file transfer error: " + e.Message);
                        }
                    } //end checking socket nullity
                }     //end wait condition
            }         //end infinite loop
        }             //end ReceiveCBMsg