コード例 #1
0
        /// <summary>
        /// Sends a request without reading a response.
        /// </summary>
        /// <param name="Request">The request to be sent</param>
        /// <exception cref="ArgumentNullException">when <paramref name="Request"/> is null</exception>
        private void SendWithoutResponse(ServerRequest Request)
        {
            #region Error checking
            if (Request == null)
            {
                throw new ArgumentNullException(nameof(Request));
            }
            #endregion

            this.Send(
                JsonConvert.SerializeObject(Request)
                );
        }
コード例 #2
0
        /// <summary>
        /// Sends a request without reading a response asynchronously.
        /// </summary>
        /// <param name="Request">The request to be sent</param>
        /// <exception cref="ArgumentNullException">when <paramref name="Request"/> is null</exception>
        /// <returns>A <see cref="Task"/> that represents the asynchronous process.</returns>
        private async Task SendWithoutResponseAsync(ServerRequest Request)
        {
            #region Error checking
            if (Request == null)
            {
                throw new ArgumentNullException(nameof(Request));
            }
            #endregion

            await this.SendAsync(
                JsonConvert.SerializeObject(Request)
                );
        }
コード例 #3
0
        /// <summary>
        /// Sends a request to the server asynchronously.
        /// </summary>
        /// <param name="Request">The request to be sent</param>
        /// <returns>
        ///		A <see cref="Task"/> that represents the asynchronous process.
        ///		A <see cref="ServerResponse{T}"/> instance is returned by the <see cref="Task"/> that represents the
        ///		data sent by the server.
        ///	</returns>
        ///	<exception cref="ArgumentNullException">when <paramref name="Request"/> is null</exception>
        internal async Task <ServerResponse <T> > SendAsync <T>(ServerRequest Request)
        {
            #region Error checking
            if (Request == null)
            {
                throw new ArgumentNullException(nameof(Request));
            }
            #endregion

            await this.SendAsync(
                JsonConvert.SerializeObject(Request)
                );

            var Result = JsonConvert.DeserializeObject <ServerResponse <T> >(
                await this.ReadAsync()
                );

            return(Result);
        }
コード例 #4
0
        /// <summary>
        /// Sends a request to the server.
        /// </summary>
        /// <param name="Request">The request to be sent</param>
        /// <exception cref="ArgumentNullException">when <paramref name="Request"/> is null</exception>
        internal ServerResponse Send(ServerRequest Request)
        {
            #region Error checking
            if (Request == null)
            {
                throw new ArgumentNullException(nameof(Request));
            }
            #endregion

            this.Send(
                JsonConvert.SerializeObject(Request)
                );

            var Result = JsonConvert.DeserializeObject <ServerResponse>(
                this.Read()
                );

            return(Result);
        }
コード例 #5
0
        /// <summary>
        /// Adatot fogad a kapcsolódott klienstől, majd bontja a kapcsolatot.
        /// </summary>
        /// <param name="client">A kliens-kapcsolatot reprezentáló TcpClient példány.</param>
        private void ClientThread(object client)
        {
            TcpClient Client = client as TcpClient;

            #region Error checking
            if (Client == null)
            {
                return;
            }
            #endregion

            bool KeepAliveConnection = false;

            string Data = "";

            using (StreamReader ClientReader = new StreamReader(Client.GetStream())) {
                try {
                    StreamWriter ClientWriter = new StreamWriter(Client.GetStream())
                    {
                        AutoFlush = true
                    };

                    do
                    {
                        Data = ClientReader.ReadLine();

                        #region Error checking
                        if (String.IsNullOrWhiteSpace(Data))
                        {
                            ClientWriter.WriteLine("ERROR: Nothing received.");
                            continue;
                        }
                        #endregion

                        ServerRequest Request = JsonConvert.DeserializeObject <ServerRequest>(Data);

                        bool HandledByInternalCommandHandler = !this.InternalCommandHandler(ClientWriter, ref KeepAliveConnection, Request.Module, Request.Command);

                        if (HandledByInternalCommandHandler)
                        {
                            if (!this.ExecuteHandlerMethod(ClientWriter, Request.Module, Request.Command, Request.Arguments?.ToArray()))
                            {
                                string Response = JsonConvert.SerializeObject(
                                    ServerResponse <object> .GetFailed($"Failed to execute: Module={Request.Module} Command={Request.Command}")
                                    );

                                ClientWriter.WriteLine(Response);
                            }
                        }
                    }while (KeepAliveConnection && Client.Connected);
                }
                catch (ObjectDisposedException) { }
                catch (IOException) { }
                catch (ThreadInterruptedException) { }
                catch (JsonReaderException) {
                    Trace.WriteLine(Data);
                }
            }

            Client.Close();
            Trace.TraceInformation("[Server] The connection is closed to the client.");
        }