Example #1
0
 private void FabiGUI_Load(object sender, EventArgs e)
 {
     this.stringReceivedDelegate = new StringReceivedDelegate(stringReceived);
     BeginInvoke(this.stringReceivedDelegate, new Object[] { "VALUES:512,512,512,512,512" });
 }
        //private void connected(Task t) {
        //    if (!t.IsFaulted) {
        //        bytesReceived = 0;
        //        bytesExpected = 4; // length of incoming string
        //        receiveMode = 0; // string length
        //        try {
        //            if (buffer.Length < bytesExpected) buffer = new byte[bytesExpected];
        //            pipe.ReadAsync(buffer, bytesReceived, bytesExpected - bytesReceived).ContinueWith(received);
        //        } catch { }
        //    }
        //}
        private void asyncReceived(IAsyncResult ar)
        {
            int bytesReceivedAsync = pipe.EndRead(ar);

            if (ar.IsCompleted)
            {
                bytesReceived += bytesReceivedAsync;
                if (bytesReceived > bytesExpected)
                {
                    // Does this happen? I hope not
                    Debug.WriteLine("Ack");
                }
                else if (bytesReceived == bytesExpected)
                {
                    // handle message
                    switch (receiveMode)
                    {
                    case 0:
                        int strLen = BitConverter.ToInt32(buffer, 0);
                        if (strLen > 0)
                        {
                            receiveMode   = 1;   // string data
                            bytesExpected = strLen;
                            bytesReceived = 0;
                        }
                        break;

                    case 1:
                        string s = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesReceived);
                        Debug.WriteLine(">>>" + s);
                        StringReceivedDelegate eh = StringReceived;
                        if (eh != null)
                        {
                            try {
                                eh(this, s);
                            } catch { }
                        }
                        bytesReceived = 0;
                        bytesExpected = 4;   // length of incoming string
                        receiveMode   = 0;   // string length
                        break;
                    }
                }
            }

            if (pipe.IsConnected)
            {
                // request more data
                try {
                    //pipe.ReadAsync(buffer, bytesReceived, bytesExpected - bytesReceived).ContinueWith(received);
                    pipe.BeginRead(buffer, bytesReceived, bytesExpected - bytesReceived, asyncReceived, null);
                } catch { }
            }
            else
            {
                // pipe broken
                try {
                    pipe.Disconnect();
                    pipe.BeginWaitForConnection(asyncConnected, null);
                    //pipe.WaitForConnectionAsync().ContinueWith(connected);
                } catch { }
            }
        }
 public StringDownloader(string url, StringDownloader.StringReceivedDelegate received)
 {
     this.url = url;
     this.received = received;
 }
Example #4
0
 private void LipmouseGUI_Load(object sender, EventArgs e)
 {
     this.stringReceivedDelegate = new StringReceivedDelegate(stringReceived);
     BeginInvoke(this.stringReceivedDelegate, new Object[] { "VALUES:512,512,512,512,512" });
 }
Example #5
0
 public StringDownloader(string url, StringDownloader.StringReceivedDelegate received)
 {
     this.url      = url;
     this.received = received;
 }