Пример #1
0
        private void receiveAnnouncements(IAsyncResult result)
        {
            if (this.disposed)
            {
                return;
            }

            lock (this.socket)
            {
                SocketStateObject state = result.AsyncState as SocketStateObject;
                if (state == null || this.socket == null || !this.socket.Connected)
                {
                    return;
                }

                int read = 0;
                try
                {
                    read = this.socket.EndReceive(result);
                }
                catch (Exception ex)
                {
                    this.client.LogErrorMessage("Could not read the TCP socket", ex);
                    this.Close();
                    this.onAborted();
                }

                if (read > 0)
                {
                    state.Builder.Append(Encoding.UTF8.GetString(state.Buffer, 0, read));

                    this.receive(state);
                }

                string data = state.Builder.ToString();
                if (data.Length > 0 && data.Contains(AnnouncementEnd) || data.Contains(AnnouncementEndAlternative))
                {
                    this.client.LogMessage("JSON RPC Announcement received: " + data);

                    int pos = data.IndexOf(AnnouncementEnd);
                    if (pos < 0)
                    {
                        pos = data.IndexOf(AnnouncementEndAlternative) + AnnouncementEndAlternative.Length;
                    }
                    else
                    {
                        pos += AnnouncementEnd.Length;
                    }
                    state.Builder.Remove(0, pos);
                    this.onAnnouncement(data.Substring(0, pos));
                }

                this.receive(state);
            }
        }
Пример #2
0
        private void receive(SocketStateObject state)
        {
            if (state == null || this.socket == null || !this.socket.Connected)
            {
                return;
            }

            try
            {
                this.socket.BeginReceive(state.Buffer, 0, SocketStateObject.BufferSize,
                                         0, new AsyncCallback(this.receiveAnnouncements), state);
            }
            catch (Exception ex)
            {
                this.client.LogErrorMessage("Could not start receiving from the TCP socket", ex);
                this.Close();
                this.onAborted();
            }
        }
        private void receive(SocketStateObject state)
        {
            if (state == null || this.socket == null || !this.socket.Connected)
            {
                return;
            }

            try
            {
                this.socket.BeginReceive(state.Buffer, 0, SocketStateObject.BufferSize,
                    0, new AsyncCallback(this.receiveAnnouncements), state);
            }
            catch (Exception ex)
            {
                this.client.LogErrorMessage("Could not start receiving from the TCP socket", ex);
                this.Close();
                this.onAborted();
            }
        }