コード例 #1
0
ファイル: ServerCore.cs プロジェクト: y-nakagawa512/Sockety
        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());
                    }
                }
            });
        }