Beispiel #1
0
        public string LuaConfig(int slnId, int contractId, string[] keyNames, MessageReader reader)
        {
            DataTable table = GetList("paramType=2 and SlnID=" + slnId + " and ContractID=" + contractId).Tables[0];

            return BuildLuaFile(table, reader, keyNames);
        }
Beispiel #2
0
        private string BuildLuaFile(DataTable table, MessageReader msgReader, string[] keyNames)
        {
            StringBuilder containerBuilder = new StringBuilder();
            int loopDepth = 0;//循环深度
            List<DataRow> recordQueue = new List<DataRow>();
            foreach (DataRow record in table.Rows)
            {
                string fieldName = record["Field"].ToString();
                FieldType fieldType = (FieldType)Enum.Parse(typeof(FieldType), record["FieldType"].ToString());
                string fieldValue = "";

                if (loopDepth > 0 && fieldType == FieldType.End)
                {
                    loopDepth--;
                    recordQueue.Add(record);
                }
                if (loopDepth == 0 && recordQueue.Count > 0)
                {
                    //处理循环记录
                    ParseRecordEnd(containerBuilder, msgReader, recordQueue, loopDepth, 0, keyNames);
                    recordQueue.Clear();
                }

                if (loopDepth == 0)
                {
                    if (msgReader.GetFieldValue(fieldType, ref fieldValue))
                    {
                        //自动登录
                    }
                    if (fieldType == FieldType.Record)
                    {
                        loopDepth++;
                        recordQueue.Add(record);
                    }
                }
                else if (fieldType != FieldType.End)
                {
                    if (fieldType == FieldType.Record)
                    {
                        loopDepth++;
                    }
                    recordQueue.Add(record);
                }
            }
            return containerBuilder.ToString();
        }
Beispiel #3
0
        private void ParseRecordEnd(StringBuilder itemBuilder, MessageReader reader, List<DataRow> queue, int depth, int recordNum, string[] keyNames)
        {
            MessageReader msgReader = reader;
            string keyValue = string.Empty;
            string keyName = keyNames.Length > depth ? keyNames[depth] : string.Empty;
            List<LuaConfig> builderList = new List<LuaConfig>();
            int recordCount = 0;
            try
            {
                recordCount = msgReader.RecordCount();
            }
            catch { }
            for (int i = 0; i < recordCount; i++)
            {
                try
                {
                    msgReader.RecordStart();
                    int loopDepth = 0; //循环深度
                    StringBuilder recordBuilder = new StringBuilder();
                    List<DataRow> recordQueue = new List<DataRow>();

                    int columnNum = 0;
                    int childNum = 0;

                    #region 遍历列取数据
                    for (int r = 1; r < queue.Count - 1; r++)
                    {
                        DataRow record = queue[r];
                        string fieldName = record["Field"].ToString();
                        FieldType fieldType = (FieldType)Enum.Parse(typeof(FieldType), record["FieldType"].ToString());
                        string fieldValue = "";
                        try
                        {
                            if (loopDepth > 0 && fieldType == FieldType.End)
                            {
                                loopDepth--;
                                recordQueue.Add(record);
                            }
                            if (loopDepth == 0 && recordQueue.Count > 0)
                            {
                                //处理循环记录
                                childNum++;
                                var childBuilder = new StringBuilder();
                                ParseRecordEnd(childBuilder, msgReader, recordQueue, depth + 1, childNum, keyNames);
                                //
                                recordQueue.Clear();
                                //选择输出格式
                                FormatChildToLua(recordBuilder, childBuilder, columnNum);
                            }

                            if (loopDepth == 0)
                            {
                                if (msgReader.GetFieldValue(fieldType, ref fieldValue))
                                {
                                    if (columnNum > 0)
                                    {
                                        recordBuilder.Append(",");
                                    }
                                    if (fieldName.Trim().ToLower() == keyName.Trim().ToLower())
                                    {
                                        keyValue = fieldValue;
                                    }
                                    if (fieldType == FieldType.Byte || fieldType == FieldType.Short || fieldType == FieldType.Int)
                                    {
                                        recordBuilder.AppendFormat("{0}={1}", fieldName, fieldValue);
                                    }
                                    else
                                    {
                                        recordBuilder.AppendFormat("{0}=\"{1}\"", fieldName, fieldValue);
                                    }
                                    columnNum++;
                                }
                                if (fieldType == FieldType.Record)
                                {
                                    loopDepth++;
                                    recordQueue.Add(record);
                                }
                            }
                            else if (fieldType != FieldType.End)
                            {
                                if (fieldType == FieldType.Record)
                                {
                                    loopDepth++;
                                }
                                recordQueue.Add(record);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("recordindex:{0},fieldName:{1} error:", i, fieldName), ex);
                        }
                    }

                    #endregion
                    //读取行结束
                    msgReader.RecordEnd();
                    builderList.Add(new LuaConfig { Key = keyValue, Builder = recordBuilder });
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("recordindex:{0}error:", i), ex);
                }
            }

            FormatListToLua(itemBuilder, builderList, keyName, depth, recordNum);
        }
Beispiel #4
0
        private void DoReceive(object sender, SocketEventArgs e)
        {
            try
            {

                MessageReader writer = new MessageReader(e.Data);
                var head = writer.ReadHead();
                if (head.Success)
                {
                    SuccessCallback(writer, head);
                }
                else
                {
                    FailCallback(head);
                }
            }
            catch (Exception ex)
            {
                new BaseLog().SaveLog(ex);
            }
            finally
            {
                if (client != null)
                    client.StopWait();
            }
        }
Beispiel #5
0
 protected virtual void SuccessCallback(MessageReader writer, Message head)
 {
 }
Beispiel #6
0
        private static void ProcessResult(string contractId, int slnId, StringBuilder respContent, Message msg, MessageReader msgReader, out string sid, out string uid)
        {
            sid = "";
            uid = "";
            //头部消息
            respContent.AppendFormat("<h3>{0}-{1}</h3>", contractId, "基本消息");
            respContent.Append("<table style=\"width:90%; border-color:#999\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">");
            respContent.Append("<tr><td style=\"width:25%;\"><strong>状态值</strong></td>");
            respContent.Append("<td style=\"width:75%;\"><strong>描述</strong></td></tr>");
            respContent.AppendFormat("<tr><td>{0}</td>", msg.ErrorCode);
            respContent.AppendFormat("<td>{0}&nbsp;</td></tr>", msg.ErrorInfo);
            respContent.Append("</table>");

            if (msg.ErrorCode != ErrorCode)
            {
                DataRowCollection respRecords = GetResponseFields(contractId, slnId);

                //消息体
                respContent.AppendFormat("<h3>{0}-{1}</h3>", contractId, "返回结果");
                respContent.Append("<table style=\"width:90%; border-color:#999\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">");
                respContent.Append("<tr><td style=\"width:15%;\"><strong>参数</strong></td><td style=\"width:10%;\"><strong>类型</strong></td>");
                respContent.Append("<td style=\"width:75%;\"><strong>参数值</strong></td></tr>");
                int loopDepth = 0;//循环深度
                List<DataRow> recordQueue = new List<DataRow>();
                #region 循环体
                foreach (DataRow record in respRecords)
                {
                    string fieldName = record["Field"].ToString();
                    FieldType fieldType = (FieldType)Enum.Parse(typeof(FieldType), record["FieldType"].ToString());
                    string fieldValue = "";
                    try
                    {
                        if (loopDepth > 0 && fieldType == FieldType.End)
                        {
                            loopDepth--;
                            recordQueue.Add(record);
                        }
                        if (loopDepth == 0 && recordQueue.Count > 0)
                        {
                            //处理循环记录
                            ProcessLoopRocord(respContent, recordQueue, msgReader);
                            recordQueue.Clear();
                        }

                        if (loopDepth == 0)
                        {
                            if (msgReader.GetFieldValue(fieldType, ref fieldValue))
                            {
                                //自动登录
                                if ("1004".Equals(contractId))
                                {
                                    if ("SessionID".Equals(fieldName)) sid = fieldValue;
                                    if ("UserID".Equals(fieldName)) uid = fieldValue;
                                }
                                respContent.Append("<tr>");
                                respContent.AppendFormat("<td>&nbsp;{0}</td>", fieldName);
                                respContent.AppendFormat("<td>&nbsp;{0}</td>", fieldType);
                                respContent.AppendFormat("<td>&nbsp;{0}</td>", fieldValue);
                                respContent.Append("</tr>");
                            }
                            if (fieldType == FieldType.Record)
                            {
                                loopDepth++;
                                recordQueue.Add(record);
                            }
                        }
                        else if (fieldType != FieldType.End)
                        {
                            if (fieldType == FieldType.Record)
                            {
                                loopDepth++;
                            }
                            recordQueue.Add(record);
                        }
                    }
                    catch (Exception ex)
                    {
                        respContent.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", fieldName, fieldType, ex.ToString());
                    }

                }
                #endregion

                respContent.Append("</table>");

            }
        }
Beispiel #7
0
        /// <summary>
        /// 处理循环记录
        /// </summary>
        /// <param name="respContent"></param>
        /// <param name="recordQueue"></param>
        /// <param name="msgReader"></param>
        private static void ProcessLoopRocord(StringBuilder respContent, List<DataRow> queue, MessageReader reader)
        {
            StringBuilder headContent = new StringBuilder();
            StringBuilder builderContent = new StringBuilder();
            int recordCount = 0;
            try
            {
                recordCount = reader.RecordCount();
            }
            catch (Exception ex)
            {
            }
            respContent.Append("<tr>");
            respContent.Append("<td style=\"width:25%;\" align=\"left\">Record(N)</td>");
            respContent.Append("<td style=\"width:20%;\" align=\"left\">Record</td>");
            respContent.AppendFormat("<td style=\"width:50%;\" align=\"left\">{0}</td>", recordCount);
            respContent.Append("</tr>");

            respContent.Append("<tr><td colspan=\"3\" align=\"center\">");
            respContent.Append("<!--子表开始--><table style=\"width:98%; border-color:#999\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">");
            if (recordCount == 0)
            {
                builderContent.Append("<tr><td align=\"center\">空数据</td></tr>");
            }

            for (int i = 0; i < recordCount; i++)
            {
                try
                {
                    reader.RecordStart();
                    MessageReader msgReader = reader;
                    int loopDepth = 0; //循环深度
                    List<DataRow> recordQueue = new List<DataRow>();

                    headContent.Append("<tr><!--头开始tr-->");
                    builderContent.Append("<tr><!--内容开始tr-->");
                    int columnNum = 0;

                    #region

                    for (int r = 1; r < queue.Count - 1; r++)
                    {
                        DataRow record = queue[r];
                        string fieldName = record["Field"].ToString();
                        FieldType fieldType = (FieldType)Enum.Parse(typeof(FieldType), record["FieldType"].ToString());
                        string fieldValue = "";
                        try
                        {
                            if (loopDepth > 0 && fieldType == FieldType.End)
                            {
                                loopDepth--;
                                recordQueue.Add(record);
                            }
                            if (loopDepth == 0 && recordQueue.Count > 0)
                            {
                                builderContent.Append("</tr><tr>");
                                builderContent.AppendFormat("<td colspan=\"{0}\" align=\"right\">", columnNum);
                                builderContent.Append(
                                    "<!--子表开始--><table style=\"width:95%; border-color:#999\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">");
                                //处理循环记录
                                ProcessLoopRocord(builderContent, recordQueue, msgReader);

                                builderContent.Append("</table><!--子表结束-->");
                                builderContent.Append("</td>");
                                recordQueue.Clear();
                            }

                            if (loopDepth == 0)
                            {
                                if (msgReader.GetFieldValue(fieldType, ref fieldValue))
                                {
                                    if (i == 0)
                                        headContent.AppendFormat("<td align=\"center\"><strong>{0}</strong>({1})</td>",
                                                                 fieldName, fieldType);
                                    builderContent.AppendFormat("<td align=\"center\">&nbsp;{0}</td>", fieldValue);
                                    columnNum++;
                                }
                                if (fieldType == FieldType.Record)
                                {
                                    loopDepth++;
                                    recordQueue.Add(record);
                                }
                            }
                            else if (fieldType != FieldType.End)
                            {
                                if (fieldType == FieldType.Record)
                                {
                                    loopDepth++;
                                }
                                recordQueue.Add(record);
                            }
                        }
                        catch (Exception ex)
                        {
                            builderContent.AppendFormat("<td align=\"center\">{0}列出错{1}</td>", fieldName, ex.Message);
                        }

                    }

                    #endregion

                    headContent.Append("</tr><!--头结束tr-->");
                    builderContent.Append("</tr><!--内容结束tr-->");
                    //读取行结束
                    reader.RecordEnd();
                }
                catch (Exception ex)
                {
                    builderContent.AppendFormat("<tr><td align=\"left\">{0}行出错{1}</td></tr>", (i + 1), ex.Message);
                }
            }

            respContent.Append(headContent.ToString());
            respContent.Append(builderContent.ToString());
            respContent.Append("</table><!--子表结束-->");
            respContent.Append("</td></tr>");
            respContent.Append("<tr>");
            respContent.Append("<td colspan=\"3\" align=\"left\">End</td>");
            respContent.Append("</tr>");
        }
Beispiel #8
0
        public static MessageReader Create(string serverUrl, string requestParams, Message header, bool IsSocket)
        {
            MessageReader msgReader = null;
            if (IsSocket)
            {

                SocketAction socketAction = new SocketAction();
                socketAction.DoSocket(serverUrl, requestParams);
                MemoryStream ms = new MemoryStream(socketAction.result.ReadBuffer());
                BinaryReader reader = new BinaryReader(ms, Encoding.UTF8);
                msgReader = new MessageReader(reader);
            }
            else
            {
                Encoding encode = Encoding.GetEncoding("utf-8");
                string postData = "d=" + GetSign(requestParams);
                byte[] bufferData = encode.GetBytes(postData);

                HttpWebRequest serverRequest = (HttpWebRequest)WebRequest.Create(serverUrl);
                serverRequest.Method = "POST";
                serverRequest.ContentType = "application/x-www-form-urlencoded";
                serverRequest.ContentLength = bufferData.Length;
                Stream requestStream = serverRequest.GetRequestStream();
                requestStream.Write(bufferData, 0, bufferData.Length);
                requestStream.Close();

                //返回流

                WebResponse serverResponse = serverRequest.GetResponse();
                Stream responseStream = serverResponse.GetResponseStream();
                if (responseStream != null)
                {
                    BinaryReader reader = new BinaryReader(responseStream, Encoding.UTF8);
                    msgReader = new MessageReader(reader);

                    header.Length = msgReader.ReadInt();
                    header.ErrorCode = msgReader.ReadInt();
                    header.MsgID = msgReader.ReadInt();
                    header.ErrorInfo = msgReader.ReadString();
                    header.Action = msgReader.ReadInt();
                    header.St = msgReader.ReadString();
                }
            }
            return msgReader;
        }