Ejemplo n.º 1
0
        private void CompleteReceive(Message message)
        {
            CommunicatorResponseEventArgs args = new CommunicatorResponseEventArgs(this,
                                                                                   RdlTagCollection.FromString(message.GetBody <string>()));

            if (_altResponse != null)
            {
                _altResponse(args);
            }
            else
            {
                this.Response(args);
            }

            // Signal the thread pool to start another single asynchronous request to Receive messages from the server
            _waitObject.Set();
        }
Ejemplo n.º 2
0
        private static void BeginResponse(IAsyncResult ar)
        {
            HttpCommunicator communicator = ar.AsyncState as HttpCommunicator;

            if (communicator != null)
            {
                HttpWebResponse response = (HttpWebResponse)communicator.Request.EndGetResponse(ar);
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream stream = response.GetResponseStream();
                        if (stream != null)
                        {
                            byte[] buffer = new byte[(int)stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);
                            RdlTagCollection tags = RdlTagCollection.FromBytes(buffer);
                            CommunicatorResponseEventArgs args = new CommunicatorResponseEventArgs(communicator, tags);

                            if (communicator.AltCallback != null)
                            {
                                communicator.AltCallback(args);
                            }
                            else
                            {
                                communicator.Response(args);
                            }
                            stream.Close();
                        }
                    }
                    else
                    {
                        communicator.Failed(new CommunicatorEventArgs(communicator));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void OnSocketReceive(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                this.Error(new CommunicatorErrorEventArgs(this, new SocketException((int)e.SocketError)));
                return;
            }

            try
            {
                if (e.BytesTransferred == 0)
                {
                    // TODO: Raise an event when the socket is disconnected.
                    _socket.Close();
                    return;
                }

                _receiveBuffer.AddRange(e.Buffer.Take(e.BytesTransferred));
                if (_receiveBuffer.Count >= 4)
                {
                    int index = 0;

                    byte[] data = _receiveBuffer.ToArray();

                    bool readComplete = false;
                    while (!readComplete)
                    {
                        int length = BitConverter.ToInt32(data, index);

                        // Increment index to account for the length value bytes.
                        index += 4;

                        if (_receiveBuffer.Count >= length + 4)
                        {
                            byte[] buffer = new byte[length];
                            Array.Copy(data, index, buffer, 0, length);

                            var args = new CommunicatorResponseEventArgs(this, RdlTagCollection.FromBytes(buffer));
                            if (AltCallback != null)
                            {
                                AltCallback(args);
                            }
                            else
                            {
                                Response(args);
                            }

                            index += length;

                            if (index >= data.Length)
                            {
                                _receiveBuffer.Clear();
                                readComplete = true;
                            }
                        }
                        else
                        {
                            readComplete = true;
                        }
                    }
                }

                _socket.ReceiveAsync(e);
            }
            catch (Exception ex)
            {
                Error(new CommunicatorErrorEventArgs(this, ex));
            }
        }