예제 #1
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);
        }