Exemple #1
0
        void Socket_DataReceived(object sender, ReceiveArgs e)
        {
            long t = DateTime.Now.Ticks;
            try
            {
                Log.Info("Socket_DataReceived:开始触发接收事件处理数据的时间点:" + DateTime.Now.Ticks);
                DataHoldingUserToken token = e.Token;
                int len = token.lengthOfCurrentIncomingMessage;
                string userToken = string.Empty;
                int authoLen = 0;
                int headStart = 0;
                int headDataStart = headerLength;
                DACService dac = new DACService();
                int status = 1;
                if (Config.AuthoType == 1)
                {
                    Log.Info("Socket_DataReceived:开始认证:" + DateTime.Now.Ticks);

                    authoLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, 0);
                    userToken = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength, authoLen);
                    //检查是否登录 
                    status = dac.CheckLogin(userToken);

                    //结束检查登录
                    headStart = headerLength + authoLen;
                    headDataStart = headerLength + headerLength + authoLen;
                    Log.Info("Socket_DataReceived: 认证结束:" + DateTime.Now.Ticks);

                }
                int headDataLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, headStart);
                string methodName = Encoding.ASCII.GetString(token.theDataHolder.dataMessageReceived, headDataStart, headDataLen);
                if (status != 1 && methodName != "Login" && methodName != "Regist")
                {
                    e.DataToSend = encode.GetBytes("-99");
                    return;
                }
                int dataStart = headDataStart + headDataLen;
                len = len - dataStart;
                Log.Info("Socket_DataReceived:解析处理收到数据执行方法:" + methodName + " 时间点:" + DateTime.Now.Ticks);
                int fileNameLen = 0;
                string fileName = string.Empty;
                string data = "";
                switch (methodName)
                {
                    case "ExecutePagedDataList":
                        e.DataToSend = encode.GetBytes(dac.ExecutePagedDataList(JsonConvert.DeserializeObject<PagedRecordParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteDataList":
                        data = encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len);
                        Log.Write("收到数据:" + data);
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataList(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(data)));
                        break;
                    case "ExecuteNonQuery":
                        e.DataToSend = BitConverter.GetBytes(dac.ExecuteNonQuery(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteScalar":
                        e.DataToSend = encode.GetBytes(dac.ExecuteScalar(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).ToString());
                        break;
                    case "ExecuteDataTable":
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataTable(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteDataSet":
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataSet(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).GetXml());
                        break;
                    case "ExecutePagedDataSet":
                        e.DataToSend = encode.GetBytes(dac.ExecutePagedDataSet(JsonConvert.DeserializeObject<PagedRecordParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).GetXml());
                        break;
                    case "Login":
                        e.DataToSend = encode.GetBytes(dac.Login(JsonConvert.DeserializeObject<LoginReq>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "Logout":
                        e.DataToSend = BitConverter.GetBytes(dac.Logout(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len)));
                        break;
                    case "CheckLogin":
                        e.DataToSend = BitConverter.GetBytes(dac.CheckLogin(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len)));
                        break;
                    case "Regist":
                        e.DataToSend = BitConverter.GetBytes(dac.Regist(JsonConvert.DeserializeObject<LoginReq>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "GetFile":
                        fileNameLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, dataStart);
                        fileName = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength + dataStart, fileNameLen);
                        e.DataToSend = dac.GetFile(fileName);
                        break;
                    case "SaveFile":
                        fileNameLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, dataStart);
                        fileName = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength + dataStart, fileNameLen);
                        int fileStart = dataStart + headerLength + fileNameLen;
                        len = len - headerLength - fileNameLen;
                        byte[] fileData = new byte[len];
                        Log.Write("收到文件长度:" + fileData.Length);
                        Buffer.BlockCopy(token.theDataHolder.dataMessageReceived, fileStart, fileData, 0, len);
                        e.DataToSend = encode.GetBytes(dac.SaveFile(fileName, fileData));

                        break;
                }
                Log.Write(LogAction.Info, className, "Socket_DataReceived", "end", "end", DateTime.Now.Ticks - t, "Socket_DataReceived:接口方法:" + methodName + "处理完成");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }