Beispiel #1
0
        internal DataPacket Request(DataPacket packet, string ip, DataTcp dataTcp)
        {
            this.Initialize(packet.OnlyID, ip, packet.Obj, packet.Bytes);

            object[] paras = this.GetForm(dataTcp.Parameters);

            DataPacket dataPacket = new()
            {
                ClassID  = packet.ClassID,
                ActionID = packet.ActionID,
                OnlyId   = packet.OnlyId,

                //OnlyID = packet.OnlyID,
                //ObjType = packet.ObjType,
                IsSend = false,
                //IsErr = false,
                IsServer = !packet.IsServer,
                IsAsync  = packet.IsAsync,
                //IsIpIdea = packet.IsIpIdea,
                IpPort = packet.IpPort,
            };

            try
            {
                if (this.Initialize(dataTcp))
                {
                    IGoOut obj = dataTcp.Action.Execute(this, paras) as IGoOut;
                    if (obj != null)
                    {
                        dataPacket.Bytes = obj.Bytes;
                        dataPacket.Obj   = obj.Text;
                        //switch (dataTcp.ObjType)
                        //{
                        //    case DataTcpState.Byte:
                        //        if (obj is byte[])
                        //        {
                        //            dataPacket.Bytes = obj.ToVar<byte[]>();
                        //        }
                        //        break;
                        //    case DataTcpState.Json:
                        //        if (obj is string)
                        //        {
                        //            dataPacket.Obj = obj.ToString();
                        //        }
                        //        else
                        //        {
                        //            dataPacket.Obj = obj.ToJson();
                        //        }
                        //        break;
                        //    case DataTcpState.String:
                        //        dataPacket.Obj = obj.ToString();
                        //        break;
                        //}

                        //Type type = obj.GetType();
                        //string strobj;
                        //if (type.IsType())
                        //{
                        //    strobj = obj.ToString();
                        //}
                        //else
                        //{
                        //    strobj = obj.ToJson();
                        //}

                        //byte[] strbytes = Encoding.UTF8.GetBytes(strobj);

                        //string sd = Convert.ToString(strbytes[11], 16);

                        //string as1 = DataPacket.StringHex(strobj);
                        //hex.Length

                        //byte by = Convert.ToByte("73", 16);
                        //if (strobj.Contains("}{"))
                        //{
                        //    strobj = strobj.Replace("}{", "} {");
                        //}
                        //if (strobj.Contains("\""))
                        //{
                        //    strobj = strobj.Replace("\"", "\\\"");//解析
                        //}

                        //int listData = Encoding.UTF8.GetByteCount(strobj);//给定空包为200

                        //if (listData > BufferSize)
                        //{
                        //    double count = (double)listData / BufferSize;
                        //    if (count > (int)count)
                        //    {
                        //        count++;
                        //    }

                        //    dataPacket.Many = string.Concat("0/", (byte)count); //$"0/{(int)(count)}";
                        //}

                        //dataPacket.SetMany(listData, BufferSize);
                        //dataPacket.Obj = strobj;
                    }
                    //dataPacket.Obj = strobj;
                    dataPacket.IsErr = false;
                }
            }
            catch (Exception ex)
            {
                dataPacket.IsErr = true;
                dataPacket.Obj   = "接口调用异常";
                this.TcpException(ex);
            }
            //string as1 = dataPacket.StringData();
            //DataPacket packet1 = DataPacket.DataString(as1);
            return(dataPacket);
        }
Beispiel #2
0
 /// <summary>
 /// 当消息真实有效时被执行,默认返回执行。(该方法是用于给使用者重写的)
 /// </summary>
 /// <param name="dataTcp">调用方法信息</param>
 protected virtual bool Initialize(DataTcp dataTcp)
 {
     return(true);
 }