Beispiel #1
0
        private void processClient(TcpClient client)
        {
            Task <string>           result = null;
            CancellationTokenSource cts    = null;
            int emptySteps         = 0;
            int emptyStepsTrashold = 1000;

            while (doWork)
            {
                if (result == null)
                {
                    cts    = new CancellationTokenSource();
                    result = NetFunctions.ReadString(client.GetStream(), cts);
                }
                if (result.IsCompleted)
                {
                    if (result.Result != null)
                    {
                        emptySteps = 0;
                        bool finish = !_proc(NetMessage.FromJson(result.Result), client);
                        result = null;
                        if (finish)
                        {
                            break;
                        }
                    }
                    result = null;
                }
                emptySteps += 1;
                Thread.Sleep(0);
                if (emptySteps > emptyStepsTrashold)
                {
                    emptySteps = 0;
                    Thread.Sleep(1);
                }
            }
            if (result != null)
            {
                cts.Cancel(false);
            }
            client.Close();
        }
Beispiel #2
0
        public void CloseConnection()
        {
            if (ruler != null)
            {
                if (!resultOfConnection.IsCompleted)
                {
                    resultOfConnection.AsyncWaitHandle.Close();
                }
                else
                {
                    //send close request
                    NetMessage closeMsg = new NetMessage();
                    closeMsg.typeRequest = NetMessage.Type.Close;
                    NetFunctions.WriteString(ruler.GetStream(), NetMessage.ToJson(closeMsg));

                    //read remoute clippboard data
//                    MessageBox.Show("read clippboard text");
                    //new Action(() => MessageBox.Show("read clippboard text")).BeginInvoke(null, null);
                    System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
                    var task = NetFunctions.ReadString(ruler.GetStream(), cts);
                    if (task.Wait(10000)) //magic ten seconds
                    {
                        //MessageBox.Show("msg readed");
                        //new Action(() => MessageBox.Show("msg readed : " + task.Result)).BeginInvoke(null, null);
                        //set new clippboard
                        var msg = NetMessage.FromJson(task.Result);
                        if (!string.IsNullOrEmpty(msg.textClippboard))
                        {
                            //MessageBox.Show("clippboard not empty : " + msg.TextClippboard);
                            //new Action(() => MessageBox.Show("clippboard not empty : " + msg.textClippboard)).BeginInvoke(null, null);
                            Clipboard.SetText(msg.textClippboard);
                        }
                    }
                }

                resultOfConnection = null;
                ruler.Close();
                ruler = null;
            }
        }