Exemplo n.º 1
0
        /// <summary>
        ///     外部创建通信代理对象
        /// </summary>
        /// <param name="remoteAddress" />
        /// <returns></returns>
        public static ProcessProxy Create(string remoteAddress)
        {
            var edpTcp = new EndpointAddress(remoteAddress);

            // 创建Binding
            var myBinding = new NetTcpBinding
            {
                OpenTimeout            = TimeSpan.FromSeconds(10),
                ReceiveTimeout         = TimeSpan.FromSeconds(10),
                SendTimeout            = TimeSpan.FromSeconds(10),
                Security               = { Mode = SecurityMode.None },
                MaxBufferSize          = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue
            };
            //myBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
            //myBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
            var client = new ProcessProxy(myBinding, edpTcp);

            return(client);
        }
        public void StartConnect()
        {
            if (Connected)
            {
                return;
            }

            // 创建
            switch (HostType)
            {
            case HostConnectionType.Machine:
                ConnectionInstance = MachineProxy.Create(RemoteHostAddress);
                break;

            case HostConnectionType.Process:
                ConnectionInstance = ProcessProxy.Create(RemoteHostAddress);
                break;

            case HostConnectionType.Resource:
                ConnectionInstance = ResourceProxy.Create(RemoteHostAddress);
                break;

            case HostConnectionType.Partner:
                ConnectionInstance = PartnerProxy.Create(RemoteHostAddress);
                break;

            case HostConnectionType.Admin:
                ConnectionInstance = AdminProxy.Create(RemoteHostAddress);
                break;

            default:
                throw new NotSupportedException();
            }

            // 连接
            ConnectionInstance?.Connect();

            if (Connected)
            {
                //连接事件
                ServerEventArg arg = new ServerEventArg("Unknown", ServerEventType.Connected);

                LOG.Info($"启动{HostType.ToString()}对方端口{RemoteHostAddress}成功");

                OnConnectedHander?.BeginInvoke(arg, null, null);

                //启动心跳
                //StartHeartbeat();
            }
            else
            {
                //连接失败
                ServerEventArg arg = new ServerEventArg("Unknown", ServerEventType.Fault);

                LOG.Debug($"启动{HostType.ToString()}对方端口{RemoteHostAddress}失败");


                OnConnectFaultHander?.BeginInvoke(arg, null, null);

                ConnectionInstance = null;
            }
        }