コード例 #1
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            public Connection Connect(IPEndPoint connectEndPoint, ConnectionData connectionData)
            {
                CheckConnect();
                IConnectionSocket socket = Socket();

                var tokenSource = new CancellationTokenSource();

                RunCancellation(socket, tokenSource.Token);

                Connection connection = null;

                try
                {
                    connection = ConnectInternal(socket, connectEndPoint, connectionData);
                }
                catch (Exception ex)
                {
                    if (isCancellation)
                    {
                        _Logger.Info($"Socket Closed. : {ex.Message}");
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    tokenSource.Cancel();
                    socket.Dispose();
                }

                return(connection);
            }
コード例 #2
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            private IConnectionSocket Socket()
            {
                if (_LaunchSocket != null)
                {
                    return(_LaunchSocket.Invoke());
                }

                IConnectionSocket socket = null;

                try
                {
                    if (_LocalEndPoint == null)
                    {
                        socket = new InternalConnectionSocket();
                    }
                    else
                    {
                        socket = new InternalConnectionSocket(_LocalEndPoint);
                    }
                }
                catch (Exception ex)
                {
                    Throw(_LocalEndPoint, $"Fail Create ConnectionSocket. {ex.Message}", ex);
                }

                return(socket);
            }
コード例 #3
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            private void Send(IConnectionSocket socket, IPEndPoint remoteEndPoint, ShareClientData shareClientData)
            {
                try
                {
                    socket.Send(remoteEndPoint, shareClientData.ToByte());
                }
                catch (Exception ex)
                {
                    Throw(remoteEndPoint, $"Fail Send. {ex.Message}", ex);
                }

                _Logger.Send(remoteEndPoint, shareClientData.ToByte());
            }
コード例 #4
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            private ConnectionSocketRecieveData Recive(IConnectionSocket socket)
            {
                ConnectionSocketRecieveData recieveData = null;

                try
                {
                    recieveData = socket.Recieve();
                    _Logger.Receive(recieveData.ReciveEndPoint, recieveData.RecieveBytes);
                }
                catch (Exception ex)
                {
                    Throw(_LocalEndPoint, $"Fail Recive. {ex.Message}", ex);
                }

                return(recieveData);
            }
コード例 #5
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
 private void RunCancellation(IConnectionSocket socket, CancellationToken token)
 {
     Task.Run(() =>
     {
         while (!token.IsCancellationRequested)
         {
             if (_IsCancellation.Invoke())
             {
                 isCancellation = true;
                 socket.Dispose();
                 _Logger.Info("Request Cancel.");
                 break;
             }
             Thread.Sleep(1);
             Thread.Sleep(0);
             Thread.Yield();
         }
     });
 }
コード例 #6
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            private Connection AcceptInternal(IConnectionSocket socket)
            {
                var recieveData           = Recive(socket);
                var recieveConnectionData = ShareClientData.FromBytes(recieveData.RecieveBytes);

                if (recieveConnectionData == null)
                {
                    _Logger.Info($"ShareClientData Convert Fail.");
                    return(null);
                }
                else if (recieveConnectionData.Header.DataType != SendDataType.System)
                {
                    _Logger.Info($"ShareClientData Type {recieveConnectionData?.Header.DataType}.");
                    return(null);
                }

                var connectionData = ConnectionData.FromByte(recieveConnectionData.DataPart);

                if (connectionData == null)
                {
                    _Logger.Info($"ConnectionData Convert Fail.");
                    return(null);
                }

                var remoteEndPoint     = recieveData.ReciveEndPoint;
                var connectionResponse = _AcceptRequest.Invoke(remoteEndPoint, connectionData);

                _Logger.Info($"AcceptRequest is {connectionResponse.IsConnect}.");

                var responseData = new ShareClientData(ShareClientHeader.CreateSystem((uint)connectionResponse.Size), connectionResponse.ToByte());

                Send(socket, remoteEndPoint, responseData);

                Connection connection = null;

                if (connectionResponse.IsConnect)
                {
                    _Logger.Info($"Accept Succes.");
                    connection = new Connection(connectionResponse.ConnectionData.CleintSpec, socket.LocalEndPoint, remoteEndPoint);
                }
                return(connection);
            }
コード例 #7
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            private Connection ConnectInternal(IConnectionSocket socket, IPEndPoint connectEndPoint, ConnectionData connectionData)
            {
                _Logger.Info($"Start Connect. -> {connectEndPoint}");

                var sendData = new ShareClientData(ShareClientHeader.CreateSystem((uint)connectionData.Size), connectionData.ToByte());

                Send(socket, connectEndPoint, sendData);

                var recieveData  = Recive(socket);
                var responseData = ShareClientData.FromBytes(recieveData.RecieveBytes);

                if (responseData == null)
                {
                    _Logger.Info($"ShareClientData Convert Fail.");
                    return(null);
                }
                else if (responseData.Header.DataType != SendDataType.System)
                {
                    _Logger.Info($"ShareClientData Type is {responseData.Header.DataType}.");
                    return(null);
                }

                var connectionResponse = ConnectionResponse.FromByte(responseData.DataPart);

                if (connectionResponse == null)
                {
                    _Logger.Info($"Response DataPart is Nothing.");
                    return(null);
                }

                _ConnectResponse.Invoke(connectionResponse);
                if (!connectionResponse.IsConnect)
                {
                    _Logger.Info($"Reject Connect. -> {connectEndPoint}");
                    return(null);
                }

                _Logger.Info($"Succes Connect. -> {connectEndPoint}");

                return(new Connection(connectionResponse.ConnectionData.CleintSpec, socket.LocalEndPoint, connectEndPoint));
            }
コード例 #8
0
ファイル: Connection.cs プロジェクト: Yuki4-dev/ShareClient
            public Connection Accept(IPEndPoint localEndPont)
            {
                _LocalEndPoint = localEndPont;

                CheckRecieve();
                IConnectionSocket socket = Socket();

                var tokenSource = new CancellationTokenSource();

                RunCancellation(socket, tokenSource.Token);

                Connection connection = null;

                try
                {
                    connection = AcceptInternal(socket);
                }
                catch (Exception ex)
                {
                    if (isCancellation)
                    {
                        _Logger.Info($"Socket Closed. : {ex.Message}");
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    tokenSource.Cancel();
                    socket.Dispose();
                }

                return(connection);
            }