예제 #1
0
        /// <summary>
        /// Trims a response from the server and splits into an array so the actual data can be used/read.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] TrimAndSplitTcpResponse(DataPrefix prefix, string str)
        {
            // Trim the prefix and delimiter from the start of the response
            string response = str.Remove(0, prefix.GetDescription().Length).TrimStart(Constants.Delimiter.ToCharArray());

            // Trim the end of file tag
            response = response.TrimEnd("<EOF>".ToCharArray());

            // Split the data into an array
            string[] responseArray = response.Split(Convert.ToChar(Constants.Delimiter));

            return(responseArray);
        }
예제 #2
0
        /// <summary>
        /// Determines the type of data from the server and handles accordingly
        /// </summary>
        /// <param name="data"></param>
        private static void HandleData(string data)
        {
            DataPrefix type   = DataPrefix.None;
            StatusCode status = StatusCode.None;
            string     error  = string.Empty;

            // If the data is prefixed with -msg
            if (data.StartsWith(DataPrefix.Message.GetDescription()))
            {
                //Rebuild message object
                //Message message = Message.BuildMessageFromTcpString(content);

                //Raise message received event
                // MessageReceivedEventArgs args = Message.BuildMessageReceivedEvent(message);
                //message.OnMessageReceived(args);
            }

            // If the data is prefixed with -reguser
            else if (data.StartsWith(DataPrefix.RegisterUser.GetDescription()))
            {
                // Trims and splits response
                string[] response = StringHelper.TrimAndSplitTcpResponse(DataPrefix.RegisterUser, data);

                type   = DataPrefix.RegisterUser;
                status = (StatusCode)Enum.Parse(typeof(StatusCode), response[0]);
                error  = response[1];
            }

            // If the data is prefixed with -login
            else if (data.StartsWith(DataPrefix.LoginUser.GetDescription()))
            {
                string[] response = StringHelper.TrimAndSplitTcpResponse(DataPrefix.LoginUser, data);

                type   = DataPrefix.LoginUser;
                status = (StatusCode)Enum.Parse(typeof(StatusCode), response[0]);
                error  = response[1];
            }

            if (type != DataPrefix.None)
            {
                ServerResponseEventArgs args = new ServerResponseEventArgs
                {
                    Type   = type,
                    Status = status,
                    Error  = error
                };

                // Raise server response event
                OnServerResponse(args);
            }
        }
예제 #3
0
파일: Server.cs 프로젝트: leonkouz/ChatApp
        /// <summary>
        /// Builds a response to the client based on the results from the database
        /// </summary>
        /// <param name="prefix">The type of request that has been made</param>
        /// <param name="dbResponse">The response from the database</param>
        /// <returns></returns>
        private string BuildTcpResponse(DataPrefix prefix, Response dbResponse)
        {
            StringBuilder sb = new StringBuilder();

            if (dbResponse.Status == StatusCode.Success)
            {
                sb.Append(prefix.GetDescription());
                sb.Append(Constants.Delimiter);
                sb.Append(StatusCode.Success);
                sb.Append(Constants.Delimiter);
                sb.Append(Constants.EndOfFile);
            }
            else
            {
                sb.Append(prefix.GetDescription());
                sb.Append(Constants.Delimiter);
                sb.Append(StatusCode.Failure);
                sb.Append(Constants.Delimiter);
                sb.Append(dbResponse.Error);
                sb.Append(Constants.EndOfFile);
            }

            return(sb.ToString());
        }