예제 #1
0
        private void DataReceived(string data)
        {
            var request = Serialization.FromJson <RpcMessage>(data);

            if (request.Id != null && string.IsNullOrEmpty(request.Method))
            {
                // Response
                requestResponses.TryAdd(request.Id.Value, data);
            }
            else if (request.Id != null && !string.IsNullOrEmpty(request.Method))
            {
                // Request
                RequestReceived?.BeginInvoke(this, new JsonRpcRequestEventArgs()
                {
                    Request = Serialization.FromJson <JsonRpcRequest>(data)
                }, EventEndCallback <JsonRpcRequestEventArgs>, null);
            }
            else if (request.Id == null)
            {
                // Notification
                NotificationReceived?.BeginInvoke(this, new JsonRpcNotificationEventArgs()
                {
                    Notification = Serialization.FromJson <JsonRpcNotification>(data)
                }, EventEndCallback <JsonRpcNotificationEventArgs>, null);
            }
            else
            {
                logger.Error("Recevied invalid RPC message:");
                logger.Error(data);
            }
        }
예제 #2
0
        public void Run()
        {
            Logger.Write(LogLevel.Notice, Strings.Connection_BeginningRun);
            if (socket == null)
            {
                Logger.Write(LogLevel.Notice, Strings.Connection_NoSocketInRun);
                return;
            }
            do
            {
                Record record;

                try {
                    record = new Record(socket, receive_buffers);
                } catch (System.Net.Sockets.SocketException) {
                    StopRun(Strings.Connection_RecordNotReceived);
                    Stop();
                    break;
                }

                Request request = GetRequest(record.RequestID);

                try {
                    if (RequestReceived != null)
                    {
                        RequestReceived.BeginInvoke(this, EventArgs.Empty, null, null);
                    }
                } catch (Exception e) {
                    // We don't care if the event handler has problems
                    Logger.Write(LogLevel.Error, "Error while invoking RequestReceived event:");
                    Logger.Write(e);
                }

                Logger.Write(LogLevel.Debug, "Now handling record (with type {0})", record.Type);

                HandleRequest(record, request);
            }while (!stop && (UnfinishedRequests || keep_alive));

            if (requests.Count == 0)
            {
                lock (connection_teardown_lock) {
                    CloseSocket();

                    if (!stop)
                    {
                        server.EndConnection(this);
                    }

                    send_buffers.Return();
                    receive_buffers.Return();
                }
            }

            Logger.Write(LogLevel.Notice,
                         Strings.Connection_EndingRun);
        }