/// <summary> /// Used for reset and clear total data and ready for restart /// </summary> #region ResetAndClearClientData private void ResetAndClearClientData() { // Reset flag for class unable to connect _unableToConnectFlag = false; // Reset flag for check connection _connectDone.Reset(); // Initialize main account utilities _mainAccountUtilities = new G9AccountUtilities <TAccount, G9ClientAccountHandler, G9ClientSessionHandler> { Account = new TAccount() }; // Initialize account and session var session = new TSession(); session.InitializeAndHandlerAccountAndSessionAutomaticFirstTime(_mainAccountUtilities.SessionHandler = new G9ClientSessionHandler { // Set send command sync Session_SendCommandByName = SendCommandByName, // Set send command async Session_SendCommandByNameAsync = SendCommandByNameAsync, // Set session encoding Session_GetSessionEncoding = () => Configuration.EncodingAndDecoding, // Set account Core_SetAccount = () => _mainAccountUtilities.Account }, 0, IPAddress.Any); _mainAccountUtilities.Account.InitializeAndHandlerAccountAndSessionAutomaticFirstTime( _mainAccountUtilities.AccountHandler = new G9ClientAccountHandler(), session); // Initialize packet management _packetManagement = new G9PacketManagement(Configuration.CommandSize, Configuration.BodySize, Configuration.EncodingAndDecoding, _logging); // Set packet size _packetSize = _packetManagement.MaximumPacketSize; // Initialize state object _stateObject = new G9SuperNetCoreStateObjectClient(_packetSize, _mainAccountUtilities.Account.Session.SessionId); // Reset send receive bytes counter TotalSendBytes = TotalReceiveBytes = TotalSendPacket = TotalReceivePacket = 0; // Remove dead space GC.Collect(); }
/// <summary> /// <para>Constructor</para> /// <para>Initialize requirement And Set config</para> /// </summary> /// <param name="clientConfig">Specify client configuration</param> /// <param name="customLogging">Specified custom logging system</param> /// <param name="privateKeyForSslConnection"> /// <para> /// Notice: This is not a certificate password, it is a private, shared key between the client and the server for /// secure connection (SSL) /// </para> /// <para>Specified custom private key</para> /// </param> /// <param name="clientUniqueIdentity"> /// <para>Specify a unique identity string from client</para> /// <para>Used for ssl connection</para> /// </param> /// <param name="commandAssemblies"> /// <para>Specified command assemblies (find command in specified assembly)</para> /// <para>If set null, mean all assembly access</para> /// </param> /// <param name="customAccount">If need set account with custom initialized account, Set it.</param> /// <param name="customSession">If need set session with custom initialized session, Set it.</param> #region G9SuperNetCoreClientBase protected AG9SuperNetCoreClientBase(G9ClientConfig clientConfig, IG9Logging customLogging = null, string privateKeyForSslConnection = null, string clientUniqueIdentity = null, Assembly[] commandAssemblies = null, TAccount customAccount = null, TSession customSession = null) { // Set command assemblies // ReSharper disable once ConvertToNullCoalescingCompoundAssignment commandAssemblies = commandAssemblies ?? AppDomain.CurrentDomain.GetAssemblies(); // Set logging system _logging = customLogging ?? new G9LoggingClient(); // Set configuration Configuration = clientConfig; // Initialize main account utilities _mainAccountUtilities = new G9AccountUtilities <TAccount, G9ClientAccountHandler, G9ClientSessionHandler> { Account = customAccount ?? new TAccount() }; // Initialize account and session var session = customSession ?? new TSession(); session.InitializeAndHandlerAccountAndSessionAutomaticFirstTime(_mainAccountUtilities.SessionHandler = new G9ClientSessionHandler { // Set send command sync Session_SendCommandByName = SendCommandByName, // Set send command async Session_SendCommandByNameAsync = SendCommandByNameAsync, // Set session encoding Session_GetSessionEncoding = () => Configuration.EncodingAndDecoding, // Set account Core_SetAccount = () => _mainAccountUtilities.Account }, 0, IPAddress.Any); _mainAccountUtilities.Account.InitializeAndHandlerAccountAndSessionAutomaticFirstTime( _mainAccountUtilities.AccountHandler = new G9ClientAccountHandler(), session); // Initialize packet management _packetManagement = new G9PacketManagement(Configuration.CommandSize, Configuration.BodySize, Configuration.EncodingAndDecoding, _logging); // Set packet size _packetSize = _packetManagement.MaximumPacketSize; // Initialize state object _stateObject = new G9SuperNetCoreStateObjectClient(_packetSize, _mainAccountUtilities.Account.Session.SessionId); // Set log if (_logging.CheckLoggingIsActive(LogsType.EVENT)) { _logging.LogEvent(LogMessage.CreateAndInitializeClient, G9LogIdentity.CREATE_CLIENT, LogMessage.SuccessfulOperation); } // Initialize command handler _commandHandler = new G9CommandHandler <TAccount>(commandAssemblies, _logging, Configuration.CommandSize, OnUnhandledCommandHandler); // Set command call back CommandHandlerCallback = _commandHandler; // ######################## Add default command ######################## // G9 Echo Command _commandHandler.AddCustomCommand <string>(nameof(G9ReservedCommandName.G9EchoCommand), G9EchoCommandReceiveHandler, null); // G9 Test Send Receive _commandHandler.AddCustomCommand <string>(nameof(G9ReservedCommandName.G9TestSendReceive), G9TestSendReceiveCommandReceiveHandler, null); // G9 Ping Command _commandHandler.AddCustomCommand <string>(nameof(G9ReservedCommandName.G9PingCommand), G9PingCommandReceiveHandler, null); // G9 Authorization Command _commandHandler.AddCustomCommand <byte[]>(nameof(G9ReservedCommandName.G9Authorization), AuthorizationReceiveHandler, null); // Set reconnect try count - use when client disconnected _reconnectTryCount = Configuration.ReconnectTryCount; // Set private key if (string.IsNullOrEmpty(privateKeyForSslConnection)) { return; } // Set private key _privateKey = privateKeyForSslConnection; // Set client unique identity _clientIdentity = string.IsNullOrEmpty(clientUniqueIdentity) ? _clientIdentity = Guid.NewGuid().ToString("N") : _clientIdentity = clientUniqueIdentity.Length < 16 ? clientUniqueIdentity + Guid.NewGuid().ToString("N") : clientUniqueIdentity; }