Beispiel #1
0
 /// <summary>
 /// 缓存Session
 /// </summary>
 /// <param name="Provider"></param>
 /// <param name="Event"></param>
 public static void ExecutePacketCache(SocketMiddleData Provider, PacketDecodeCompletedEventArgs Event)
 {
     if (Provider.SendType == SendTypeEnum.Init)
     {
         var Keys = Provider.MiddleResult.SocketJsonData.ToModel <Dictionary <string, List <string> > >().Keys.ToList();
         PacketCache.Add(string.Join(",", Keys), Event);
     }
 }
Beispiel #2
0
 /// <summary>
 /// 处理数据然后回发数据
 /// </summary>
 /// <param name="Param"></param>
 /// <param name="CallHandle"></param>
 public static void CallBackHandler(SocketMiddleData Param, CallHandleAbstract CallHandle)
 {
     if (Param.SendType == SendTypeEnum.RequestInfo)
     {
         var ResultData = CallHandle.ExecuteCallFuncHandler(Param);
         CallBackInternalInfo(ResultData, Param.SendPort);
     }
 }
Beispiel #3
0
        /// <summary>
        /// 逆向解析方法
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public virtual ISocketResult ExecuteCallFuncHandler(SocketMiddleData Param)
        {
            //一定是其它服务
            List <Type> SourceTypes = DependencyLibrary.Dependency.Where(item => item.GetCustomAttribute(typeof(SocketRouteAttribute)) != null).ToList();
            var         Routes      = Param.MiddleResult.Router.Split("/").ToList();//接受的数据路由

            foreach (var Items in SourceTypes)
            {
                SocketRouteAttribute SocketRoute = (Items.GetCustomAttribute(typeof(SocketRouteAttribute)) as SocketRouteAttribute);
                //比较接受的路由和当前程序的路由是否一直
                //全部得小写
                if (SocketRoute.InternalServer.ToLower() == Routes[0] && (SocketRoute.ControllerName.IsNullOrEmpty() ? Items.Name.ToLower().Contains(Routes[1]) : Items.Name.ToLower().Contains(SocketRoute.ControllerName.ToLower())))
                {
                    //查询到了这个类
                    //开始处理所有方法
                    var SoucreMethods = Items.GetMethods().Where(x => x.GetCustomAttribute(typeof(SocketMethodAttribute)) != null).ToList();
                    foreach (var Item in SoucreMethods)
                    {
                        SocketMethodAttribute SocketMethod = (Item.GetCustomAttribute(typeof(SocketMethodAttribute)) as SocketMethodAttribute);
                        //找到对应方法
                        if (SocketMethod.MethodName.IsNullOrEmpty() ? Routes[2] == Item.Name.ToLower() : SocketMethod.MethodName.ToLower() == Routes[2])
                        {
                            //路由数量大于等于4说明有版本号
                            if (Routes.Count >= 4 && !SocketMethod.MethodVersion.IsNullOrEmpty() && SocketMethod.MethodVersion.ToLower() == Routes.LastOrDefault())
                            {
                                //1.如果启用了Session需要用户实现ISocketSessionHandler处理
                                //2.Invoke方法
                                if (!ExecuteCallSessionHandler(Item, Param.Session))
                                {
                                    return new SocketResultDefault {
                                               SocketJsonData = ExecuteNoAuthor().ToJson()
                                    }
                                }
                                ;
                                return(ExecuteCallDataHandler(Items, Item, Param.MiddleResult));
                            }
                            else
                            {
                                //1.如果启用了Session需要用户实现ISocketSessionHandler处理
                                //2.Invoke方法
                                if (!ExecuteCallSessionHandler(Item, Param.Session))
                                {
                                    return new SocketResultDefault {
                                               SocketJsonData = ExecuteNoAuthor().ToJson()
                                    }
                                }
                                ;
                                else
                                {
                                    return(ExecuteCallDataHandler(Items, Item, Param.MiddleResult));
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// 发送内部通信
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="Session"></param>
        public static void SendInternalInfo(SocketSerializeData SerializeData, ISocketSession Session = null)
        {
            ISocketResult Param = new SocketResultDefault()
            {
                Router = SerializeData.Route, SocketJsonData = SerializeData.Providor?.ToJson()
            };

            SocketClient.Send(SocketMiddleData.Middle(SendTypeEnum.InternalInfo, Param, Session, SocketConstConfig.ClientPort).ToJson());
        }
Beispiel #5
0
        /// <summary>
        /// 转化结果
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public SocketMiddleData ExecuteMapper(object Param)
        {
            JObject        Obj      = (Param as string).ToModel <JObject>();
            SendTypeEnum   SendType = Enum.Parse <SendTypeEnum>(Obj["SendType"].ToString());
            int?           SendPort = Convert.ToInt32(Obj["SendPort"].ToString());
            ISocketResult  Result   = Obj["MiddleResult"].ToJson().ToModel <SocketResultDefault>();
            ISocketSession Session  = Obj["Session"].ToJson().ToModel <SocketSessionDefault>();

            return(SocketMiddleData.Middle(SendType, Result, Session, SendPort));
        }
Beispiel #6
0
        /// <summary>
        /// 转发数据
        /// </summary>
        /// <param name="Param"></param>
        /// <param name="PacketCache"></param>
        internal static void ExecuteSocketIniternalInfo(SocketMiddleData Param, Dictionary <String, PacketDecodeCompletedEventArgs> PacketCache)
        {
            var Router = Param.MiddleResult.Router.Split("/").FirstOrDefault();

            Param.SendType = SendTypeEnum.RequestInfo;
            var Keys     = PacketCache.Keys.Where(t => t.Contains(Router)).FirstOrDefault();
            var Event    = PacketCache[Keys];
            var NewEvent = Event.SetInfo(Event.Session, Param);

            Event.Session.Server.Handler.SessionPacketDecodeCompleted(Event.Server, NewEvent);
        }
Beispiel #7
0
        /// <summary>
        /// 查询需要生成APIJson的接口
        /// </summary>
        public SocketMiddleData FindLibrary()
        {
            List <Type> SourceType = DependencyLibrary.Dependency.Where(item => item.GetCustomAttribute(typeof(SocketRouteAttribute)) != null).ToList();

            foreach (var Items in SourceType)
            {
                List <string>        Route       = new List <string>();
                SocketRouteAttribute SocketRoute = (Items.GetCustomAttribute(typeof(SocketRouteAttribute)) as SocketRouteAttribute);
                string ControllerName            = string.Empty;
                if (SocketRoute.ControllerName.IsNullOrEmpty())
                {
                    ControllerName = Items.Name;
                }
                else
                {
                    ControllerName = SocketRoute.ControllerName;
                }
                Items.GetMethods().Where(x => x.GetCustomAttribute(typeof(SocketMethodAttribute)) != null).ToList().ForEach(Item =>
                {
                    SocketMethodAttribute SocketMethod = (Item.GetCustomAttribute(typeof(SocketMethodAttribute)) as SocketMethodAttribute);
                    string SocketUrl = string.Empty;
                    if (SocketMethod.MethodName.IsNullOrEmpty())
                    {
                        SocketUrl = $"{SocketRoute.InternalServer}/{ControllerName}/{Item.Name}";
                    }
                    else
                    {
                        SocketUrl = $"{SocketRoute.InternalServer}/{ControllerName}/{SocketMethod.MethodName}";
                    }
                    if (!SocketMethod.MethodVersion.IsNullOrEmpty())
                    {
                        SocketUrl = SocketUrl + "/" + SocketMethod.MethodVersion;
                    }
                    Route.Add(SocketUrl.ToLower());
                });
                XPlusEx.XTry(() =>
                {
                    var Key = SocketRoute.InternalServer.ToLower();
                    if (SocketJson.ContainsKey(Key))
                    {
                        SocketJson[Key].AddRange(Route);
                    }
                    else
                    {
                        SocketJson.Add(Key, Route);
                    }
                }, Ex => throw Ex);
            }
            CreateSocketApiJsonScript();
            return(SocketMiddleData.Middle(SendTypeEnum.Init, SocketResultDefault.SetValue(null, SocketJson.ToJson())));
        }
Beispiel #8
0
        /// <summary>
        /// 初始化内部通信
        /// </summary>
        /// <param name="Ip"></param>
        /// <param name="Port"></param>
        /// <param name="MiddleData"></param>
        protected virtual void InitInternalSocket(string Ip, int Port, SocketMiddleData MiddleData)
        {
            if (CallHandle == null)
            {
                CallHandle = new CallHandle();
            }
            AsyncTcpClient ClientAsnyc = SocketFactory.CreateClient <AsyncTcpClient, SocketPacket>(Ip, Port);

            if (!ClientPath.IsNullOrEmpty() && ClientPort.HasValue)
            {
                ClientAsnyc.LocalEndPoint = new IPEndPoint(IPAddress.Parse(ClientPath), ClientPort.Value);
            }
            ClientAsnyc.Connect(out bool Connect);
            Call.SocketClient         = ClientAsnyc;
            ClientAsnyc.PacketReceive = (Client, Data) =>
            {
                DependencyCondition Instance = DependencyCondition.Instance;
                if (Instance.ExecuteIsCall(Data) != SendTypeEnum.CallBack)
                {
                    var MiddleData = Instance.ExecuteMapper(Data);
                    if (Client.IsConnected)
                    {
                        Call.CallBackHandler(MiddleData, CallHandle);
                    }
                }
                else
                {
                    Instance.ExecuteCallData(Data);
                }
            };
            ClientAsnyc.ClientError = (Client, Error) =>
            {
                DependencyError.Instance.ExecuteRecordLog(Error);
            };
            if (MiddleData.SendType == SendTypeEnum.Init)
            {
                ClientAsnyc.Send(MiddleData.ToJson());
            }
        }
Beispiel #9
0
        /// <summary>
        /// 处理内部消息
        /// </summary>
        /// <param name="Param"></param>
        public static void ExecuteInternalInfo(PacketDecodeCompletedEventArgs Event, SocketMiddleData Param)
        {
            switch (Param.SendType)
            {
            case SendTypeEnum.Init:
                InitHandler.ExecuteSocketApiJson(Param.MiddleResult);
                break;

            case SendTypeEnum.InternalInfo:
                InternalHandler.ExecuteSocketIniternalInfo(Param, PacketCache);
                break;

            case SendTypeEnum.RequestInfo:
                RequestHandler.ExecuteSocketRequest(Event, Param);
                break;

            case SendTypeEnum.CallBack:
                CallHandler.ExecuteSocketCallBack(PacketCache.Values.ToList(), Param);
                break;

            default:
                break;
            }
        }
Beispiel #10
0
 /// <summary>
 /// 发送请求
 /// </summary>
 /// <param name="Event"></param>
 /// <param name="Param"></param>
 internal static void ExecuteSocketRequest(PacketDecodeCompletedEventArgs Event, SocketMiddleData Param)
 {
     Event.Server.Send(Param.ToJson(), Event.Session);
 }
Beispiel #11
0
        /// <summary>
        /// 执行回调数据
        /// </summary>
        internal static void ExecuteSocketCallBack(List <PacketDecodeCompletedEventArgs> Packets, SocketMiddleData Param)
        {
            var Event = Packets.Where(Item => (Item.Session.Socket.RemoteEndPoint as IPEndPoint).Port == Param.SendPort).FirstOrDefault();

            Event.Server.Send(Param.ToJson(), Event.Session);
        }
Beispiel #12
0
 /// <summary>
 /// 回调数据
 /// </summary>
 /// <param name="Param"></param>
 /// <param name="SendPort"></param>
 private static void CallBackInternalInfo(ISocketResult Param, int?SendPort)
 {
     SocketClient.Send(SocketMiddleData.Middle(SendTypeEnum.CallBack, Param, null, SendPort).ToJson());
 }
Beispiel #13
0
 /// <summary>
 /// 逆向解析方法
 /// </summary>
 /// <param name="Param"></param>
 /// <returns></returns>
 public override ISocketResult ExecuteCallFuncHandler(SocketMiddleData Param)
 {
     return(base.ExecuteCallFuncHandler(Param));
 }