private void HandleAccept(NetEndPointMessage message)
        {
            this.LocalAcceptor = Network.CreateAcceptor(IPEndPointHelper.GetIPEndPoint(message), ProtocalType.Tcp, network =>
            {
                InAcceptNetworks[network.Id] = network;
            }, network =>
            {
                InAcceptNetworks.Remove(network.Id);
                this.InnerNetMapManager.Remove(network);

                if (this.IsCenterServer)
                {
                    return;
                }
            });

            if (this.IsCenterServer)
            {
                Log.Info($"中心服务启动成功.", LoggerBllType.System);
            }
            else if (this.IsProxyServer)
            {
                Log.Info($"代理服务启动成功.", LoggerBllType.System);
            }
            else
            {
                Log.Info($"监听内网端口:{message.Port}成功.", LoggerBllType.System);
            }
        }
Пример #2
0
        /// <summary>
        /// 连接到网关服务,连接成功后会断开与代理服务连接。
        /// </summary>
        private async void ConnectingGate()
        {
            var message = await this.Network.CallMessageAsync <int, NetEndPointMessage>((int)this.ProtocalType, (ushort)SysNetCommand.GetGateEndPoint);

            if (message == null)
            {
                Log.Error("服务端未启动", LoggerBllType.System);
                return;
            }

            if (this.Config.ProtocalType == ProtocalType.Kcp || this.ProtocalType == ProtocalType.Tcp)
            {
                var endPoint     = IPEndPointHelper.GetIPEndPoint(message);
                var proxyNetwork = this.Network.Channel;
                this.Network = Network.CreateConnector(endPoint, this.ProtocalType, network =>
                {
                    this.OnConnected?.Invoke(network, ConnectType.Gate);

                    //连接成功以后断开代理服务。
                    proxyNetwork.Disconnect();
                }, network =>
                {
                    this.OnDisconnected?.Invoke(network, ConnectType.Gate);
                });
            }
            else if (this.Config.ProtocalType == ProtocalType.Wcp)
            {
                var ws           = this.Config.HttpType == "https" ? "wss" : "ws";
                var prefixed     = $"{ws}://{message.Host}:{message.Port}/";
                var proxyNetwork = this.Network.Channel;
                this.Network = Network.CreateWebSocketConnector(prefixed, network =>
                {
                    this.OnConnected?.Invoke(network, ConnectType.Gate);

                    //连接成功以后断开代理服务。
                    proxyNetwork.Disconnect();
                },
                                                                network =>
                {
                    this.OnDisconnected?.Invoke(network, ConnectType.Gate);
                });
            }
        }
        private void Connecting(NetEndPointMessage message)
        {
            if (this.InConnectedNetworks.ContainsKey(message))
            {
                return;
            }

            //如果存在就不再创建新的Network
            if (this.DisconnectedNetworks.ContainsKey(message))
            {
                return;
            }

            if (this.IsCenterServer)
            {
                return;
            }

            //不连接进程内的监听端口
            if (message == this.Config.GetLocalMessage())
            {
                return;
            }

            var connector = Network.CreateConnector(IPEndPointHelper.GetIPEndPoint(message), ProtocalType.Tcp, network =>
            {
                OnClientConnect(network, message);
            }, network =>
            {
                OnClientDisconnect(network, message);
            });

            if (message == this.Config.GetCenterMessage())
            {
                this.CenterConnector = connector;
            }
            else
            {
                this.InConnectedNetworks[message]   = connector;
                this.NotExistProxyNetworks[message] = connector;
            }
        }
Пример #4
0
 private void StartConnecting()
 {
     if (this.Config.ProtocalType == ProtocalType.Kcp || this.ProtocalType == ProtocalType.Tcp)
     {
         var proxyEndPoint = IPEndPointHelper.GetIPEndPoint(this.Config);
         this.Network = Network.CreateConnector(proxyEndPoint, this.ProtocalType, network =>
         {
             if (this.Config.ProxyEnable)
             {
                 ConnectingGate();
                 return;
             }
             this.OnConnected?.Invoke(network, ConnectType.Proxy);
         }, network =>
         {
             this.OnDisconnected?.Invoke(network, ConnectType.Proxy);
         });
         return;
     }
     else if (this.Config.ProtocalType == ProtocalType.Wcp)
     {
         this.Network = Network.CreateWebSocketConnector(this.WebSocketPrefixed, network =>
         {
             if (this.Config.ProxyEnable)
             {
                 ConnectingGate();
                 return;
             }
             this.OnConnected?.Invoke(network, ConnectType.Proxy);
         },
                                                         network =>
         {
             this.OnDisconnected?.Invoke(network, ConnectType.Proxy);
         });
         return;
     }
     throw new ComponentException($"协议类型{this.Config.ProtocalType}不支持.");
 }
        private void HandleOutAccept()
        {
            var config = this.Config.OuterAcceptConfig;

            if (config.OuterKcpAcceptConfig.Enable)
            {
                var endPoint = IPEndPointHelper.GetIPEndPoint(config.OuterKcpAcceptConfig.GetMessage());
                var acceptor = Game.Scene.AddComponent <NetAcceptorComponent, IPEndPoint, ProtocalType>(endPoint, ProtocalType.Kcp);
                this.OuterAccepts.Add(acceptor);
            }
            if (config.OuterTcpAcceptConfig.Enable)
            {
                var endPoint = IPEndPointHelper.GetIPEndPoint(config.OuterTcpAcceptConfig.GetMessage());
                var acceptor = Game.Scene.AddComponent <NetAcceptorComponent, IPEndPoint, ProtocalType>(endPoint, ProtocalType.Tcp);
                this.OuterAccepts.Add(acceptor);
            }
            if (config.OuterWebSocketConfig.Enable)
            {
                var prefixed = config.OuterWebSocketConfig.HttpPrefixed;
                var acceptor = Game.Scene.AddComponent <NetAcceptorComponent, string, ProtocalType>(prefixed, ProtocalType.Wcp);
                this.OuterAccepts.Add(acceptor);
            }
        }