Exemple #1
0
        public void Start(IPEndPoint localEndPoint, CancellationTokenSource _stoppingCts, T parent, ServerSetting _serverSetting = null, int HeartBeatTimeOut = 2000)
        {
            Parent        = parent;
            stoppingCts   = _stoppingCts;
            serverSetting = _serverSetting;

            if (serverSetting == null)
            {
                serverSetting = new ServerSetting {
                    UseSSL = false
                };
            }
            SocketySetting.HEART_BEAT_LOST_TIME = HeartBeatTimeOut;

            // メイン接続のTCP/IPを作成
            MainListener = new TcpListener(localEndPoint.Address, localEndPoint.Port);
            MainListener.Start();


            Task.Run(async() =>
            {
                //クライアント接続スレッド
                while (!stoppingCts.IsCancellationRequested)
                {
                    try
                    {
                        Logger.LogInformation("Waiting for a connection...");
                        TcpClient handler = await MainListener.AcceptTcpClientAsync();

                        //接続方法をクライアントに伝える
                        SendConnectSetting(handler.GetStream(), serverSetting);

                        Stream CommunicateStream;
                        SocketyCryptService cryptService = null;
                        if (serverSetting.UseSSL == true)
                        {
                            Logger.LogInformation("UseSSL");
                            //SSL設定
                            SslStream sslStream = new SslStream(handler.GetStream());
                            sslStream.AuthenticateAsServer(serverSetting.Certificate, false, System.Security.Authentication.SslProtocols.Tls12, true);
                            CommunicateStream = sslStream as Stream;

                            string SumKey = AES_IV + AES_Key;
                            var packet    = new SocketyPacket()
                            {
                                SocketyPacketType = SocketyPacket.SOCKETY_PAKCET_TYPE.KeyExchange, PackData = Encoding.UTF8.GetBytes(AES_IV + AES_Key)
                            };
                            var packetData = MessagePackSerializer.Serialize(packet);
                            //鍵を送る
                            var packetDataSized = BitConverter.GetBytes(packetData.Length);
                            CommunicateStream.Write(packetDataSized, 0, packetDataSized.Length);
                            CommunicateStream.Write(packetData, 0, packetData.Length);

                            cryptService = new SocketyCryptService(AES_IV, AES_Key);
                        }
                        else
                        {
                            Logger.LogInformation("NonSSL");
                            //平文
                            CommunicateStream = handler.GetStream();
                        }

                        //クライアント情報を受信
                        var clientInfo = ClientInfoReceive(CommunicateStream);
                        if (ClientInfoManagement(clientInfo) == true)
                        {
                            Logger.LogInformation($"ClientInfo ClientID:{clientInfo.ClientID} Name:{clientInfo.Name}");
                        }
                        else
                        {
                            Logger.LogInformation($"ReConnect ClientInfo ClientID:{clientInfo.ClientID} Name:{clientInfo.Name}");
                        }


                        // クライアントが接続したので、受付スレッドを開始する
                        var clientHub = new ClientHub <T>(_handler: handler,
                                                          _stream: CommunicateStream,
                                                          _clientInfo: clientInfo,
                                                          userClass: Parent,
                                                          logger: Logger,
                                                          _filters: SocketyFilters,
                                                          CryptService: cryptService);


                        clientHub.ConnectionReset = ConnectionReset;
                        clientHub.Run();

                        SocketClient <T> .GetInstance().AddClientHub(clientHub);
                    }
                    catch (Exception ex)
                    {
                        if (stoppingCts.IsCancellationRequested == true)
                        {
                            return;
                        }
                        Console.WriteLine(ex.ToString());
                    }
                }
            });
        }
Exemple #2
0
        public void Start(IPEndPoint localEndPoint, CancellationTokenSource _stoppingCts, T parent, ServerSetting _serverSetting = null)
        {
            Parent        = parent;
            stoppingCts   = _stoppingCts;
            serverSetting = _serverSetting;

            if (serverSetting == null)
            {
                serverSetting = new ServerSetting {
                    UseSSL = false
                };
            }
            // メイン接続のTCP/IPを作成
            MainListener = new TcpListener(localEndPoint.Address, localEndPoint.Port);
            MainListener.Start();


            Task.Run(async() =>
            {
                //クライアント接続スレッド
                while (!stoppingCts.IsCancellationRequested)
                {
                    try
                    {
                        Logger.LogInformation("Waiting for a connection...");
                        TcpClient handler = await MainListener.AcceptTcpClientAsync();

                        //接続方法をクライアントに伝える
                        SendConnectSetting(handler.GetStream(), serverSetting);

                        Stream CommunicateStream;
                        if (serverSetting.UseSSL == true)
                        {
                            Logger.LogInformation("UseSSL");
                            //SSL設定
                            SslStream sslStream = new SslStream(handler.GetStream());
                            sslStream.AuthenticateAsServer(serverSetting.Certificate, false, System.Security.Authentication.SslProtocols.Tls12, true);
                            CommunicateStream = sslStream as Stream;
                        }
                        else
                        {
                            Logger.LogInformation("NonSSL");
                            //平文
                            CommunicateStream = handler.GetStream();
                        }

                        //クライアント情報を受信
                        var clientInfo = ClientInfoReceive(CommunicateStream);
                        if (ClientInfoManagement(clientInfo) == true)
                        {
                            Logger.LogInformation($"ClientInfo ClientID:{clientInfo.ClientID} Name:{clientInfo.Name}");
                        }
                        else
                        {
                            Logger.LogInformation($"ReConnect ClientInfo ClientID:{clientInfo.ClientID} Name:{clientInfo.Name}");
                        }

                        //Udp接続
                        var CanUDPConnectPort = UserCommunicateService <T> .Get().Where(x => x.IsConnect == false).First();

                        //Udpポート番号を送信
                        var portData = MessagePackSerializer.Serialize(CanUDPConnectPort.UdpPortNumber);
                        CommunicateStream.Write(portData, 0, portData.Length);

                        //Udp HolePunching
                        var ret = UdpConnect(CanUDPConnectPort.UdpPortNumber);
                        CanUDPConnectPort.PunchingSocket = ret.s;
                        CanUDPConnectPort.PunchingPoint  = ret.p;
                        CanUDPConnectPort.IsConnect      = true;

                        // クライアントが接続したので、受付スレッドを開始する
                        var clientHub = new ClientHub <T>(_handler: handler,
                                                          _stream: CommunicateStream,
                                                          _clientInfo: clientInfo,
                                                          udpPort: CanUDPConnectPort,
                                                          userClass: Parent,
                                                          logger: Logger,
                                                          _filters: SocketyFilters);


                        clientHub.ConnectionReset = ConnectionReset;
                        clientHub.Run();

                        SocketClient <T> .GetInstance().AddClientHub(clientHub);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            });
        }