Пример #1
0
        public void Ping()
        {
            while (IsRunning)
            {
                int Start = Environment.TickCount;

                if (Mgr != null)
                {
                    List <RpcClientInfo> Disconnected = new List <RpcClientInfo>();

                    foreach (RpcClientInfo Info in Mgr.GetClients())
                    {
                        if (!Info.Connected)
                        {
                            continue;
                        }

                        try
                        {
                            GetObject <ClientMgr>(Info).Ping();
                        }
                        catch (Exception e)
                        {
                            Log.Error("RpcServer", e.ToString());
                            Log.Notice("RpcServer", Info.Description() + " | Disconnected");

                            Disconnected.Add(Info);
                            Mgr.Remove(Info.RpcID);
                        }
                    }

                    if (Disconnected.Count > 0)
                    {
                        foreach (RpcClientInfo ToDisconnect in Disconnected)
                        {
                            foreach (Type type in RegisteredTypes[0])
                            {
                                GetLocalObject(type).OnClientDisconnected(ToDisconnect);
                            }

                            foreach (RpcClientInfo Info in Mgr.GetClients())
                            {
                                foreach (Type type in RegisteredTypes[1])
                                {
                                    RpcServer.GetObject(type, Info.Ip, Info.Port).OnClientDisconnected(ToDisconnect);
                                }
                            }
                        }
                    }
                }

                int Diff = Environment.TickCount - Start;
                if (Diff < PING_TIME)
                {
                    Thread.Sleep(PING_TIME - Diff);
                }
            }
        }
Пример #2
0
        public T GetClientObject <T>(int RpcID) where T : RpcObject
        {
            RpcClientInfo Info = GetServerObject <ServerMgr>().GetClient(RpcID);

            if (Info == null)
            {
                return(GetLocalObject <T>());
            }
            else
            {
                return(RpcServer.GetObject <T>(Info));
            }
        }
Пример #3
0
        public bool Connected(int RpcID)
        {
            RpcClientInfo Info = GetClient(RpcID);

            if (Info == null)
            {
                return(false);
            }

            Log.Success("ServerMgr", Info.Description() + " | Connected");

            foreach (Type type in Server.RegisteredTypes[0])
            {
                Server.GetLocalObject(type).OnClientConnected(Info);
            }

            foreach (RpcClientInfo ConnectedClient in GetClients())
            {
                if (Info.RpcID == ConnectedClient.RpcID)
                {
                    continue;
                }

                try
                {
                    foreach (Type type in Server.RegisteredTypes[1])
                    {
                        Log.Debug("ServerMgr", Info.Name + " Send to : " + ConnectedClient.Name + ",T=" + type);
                        RpcServer.GetObject(type, ConnectedClient.Ip, ConnectedClient.Port).OnClientConnected(Info);
                        RpcServer.GetObject(type, Info.Ip, Info.Port).OnClientConnected(ConnectedClient);
                    }
                }
                catch (Exception e)
                {
                    Log.Error("ServerMgr", e.ToString());
                    Log.Notice("ServerMgr", "Invalid : " + ConnectedClient.Description());
                }
            }


            Info.Connected = true;

            return(true);
        }
Пример #4
0
        public void CheckPing()
        {
            while (IsRunning)
            {
                int Start = Environment.TickCount;

                if (!Connecting)
                {
                    try
                    {
                        if (Mgr != null)
                        {
                            Mgr.Ping();
                        }
                        else
                        {
                            Connect();
                        }
                    }
                    catch
                    {
                        foreach (Type t in RegisteredTypes[1])
                        {
                            RpcServer.GetObject(t, Info.Ip, Info.Port).OnServerDisconnected();
                        }

                        Connect();
                    }
                }
                int Diff = Environment.TickCount - Start;
                if (Diff < PING_TIME)
                {
                    Thread.Sleep(PING_TIME - Diff);
                }
            }
        }
Пример #5
0
 public RpcObject GetLocalObject(Type type)
 {
     return(RpcServer.GetObject(type, LocalIp, LocalPort));
 }
Пример #6
0
 public T GetLocalObject <T>() where T : RpcObject
 {
     return(RpcServer.GetObject(typeof(T), LocalIp, LocalPort) as T);
 }
Пример #7
0
        public bool Connect(string Ip, int Port)
        {
            Log.Debug("RpcClient", "Connect : " + Ip + ":" + Port);

            RpcServerIp   = Ip;
            RpcServerPort = Port;
            Connecting    = true;

            try
            {
                if (Channel != null)
                {
                    return(false);
                }

                Log.Debug("RpcClient", "Connecting to : " + Ip);

                if (!Pinger.IsAlive)
                {
                    Pinger.Start();
                }

                Channel = new TcpClientChannel(ServerName, null);
                ChannelServices.RegisterChannel(Channel, false);

                Mgr  = Activator.GetObject(typeof(ServerMgr), "tcp://" + Ip + ":" + Port + "/" + typeof(ServerMgr).Name) as ServerMgr;
                Info = Mgr.Connect(ServerName, ServerIp);

                if (Info == null)
                {
                    return(false);
                }

                Log.Debug("RpcClient", "Listening on : " + ServerIp + ":" + Info.Port);

                ServerChannel = new TcpServerChannel("Server" + Info.RpcID, Info.Port);
                ChannelServices.RegisterChannel(ServerChannel, false);

                RegisteredTypes = RpcObject.RegisterHandlers(false, AllowedID);
                foreach (Type t in RegisteredTypes[1])
                {
                    RpcServer.GetObject(t, Info.Ip, Info.Port).MyInfo = Info;
                }

                Mgr.Connected(Info.RpcID);

                Log.Success("RpcClient", "Connected to : " + ServerIp + ":" + RpcServerPort + ", Listen on : " + Info.Ip + ":" + Info.Port);

                foreach (Type t in RegisteredTypes[1])
                {
                    RpcServer.GetObject(t, Info.Ip, Info.Port).OnServerConnected();
                }

                Connecting = false;
            }
            catch (Exception e)
            {
                Log.Error("RpcClient", e.ToString());
                Log.Notice("RpcClient", "Can not start RPC : " + Ip + ":" + Port);

                Connecting = false;
                Mgr        = null;
                Info       = null;

                return(false);
            }

            return(true);
        }