/// <summary> /// Handle request from command /// </summary> /// <param name="request">Received request</param> /// <param name="account">Access to account</param> /// <param name="waitForFinish">If true, wait task to finish progress</param> /// <returns>Response for client</returns> #region G9CallHandler public void G9CallHandler(G9SendAndReceivePacket request, TAccount account, bool waitForFinish = false) { Action callCommand = () => { CommandDataType <TAccount> command = null; try { // Check exist command #region Unhandled Command if (!_accessToCommandDataTypeCollection.ContainsKey(request.Command)) { _onUnhandledCommand?.Invoke(request, account); return; } #endregion // Get normal command command = _accessToCommandDataTypeCollection[request.Command]; // Set log if (_logging.CheckLoggingIsActive(LogsType.EVENT)) { _logging.LogEvent( $"{LogMessage.RunningCommand}: {request.Command}", G9LogIdentity.RUNNING_COMMAND, LogMessage.SuccessfulOperation); } // Execute command with information command.AccessToMethodReceiveCommand(request.Body, account, request.RequestId, command.ExecuteRegisterCallBack); } catch (Exception ex) { // Add to log exception if (_logging.CheckLoggingIsActive(LogsType.EXCEPTION)) { _logging.LogException(ex, LogMessage.ErrorInRunningCommand, G9LogIdentity.RUNNING_COMMAND, LogMessage.FailedOperation); } // If not null call OnError in this command command?.AccessToMethodOnErrorInCommand?.Invoke(ex, account); } }; if (waitForFinish) { callCommand.Invoke(); } else { Task.Run(callCommand); } }
public G9PacketSplitHandler PackingRequestByData(byte[] command, byte[] body, G9PacketDataType dataType, Guid?customRequestId) #endif { try { if (command.Length == CalculateCommandSize) { var requestId = customRequestId ?? Guid.NewGuid(); var requestIdBytes = requestId.ToByteArray(); G9PacketSplitHandler packet; if (body.Length <= CalculateBodySize) { // Initialize packet split handler packet = new G9PacketSplitHandler(requestId, 1); // Initialize memory stream and save packet data using (var memoryStream = new MemoryStream()) { memoryStream.WriteByte((byte)G9PacketType.OnePacket); memoryStream.WriteByte((byte)dataType); memoryStream.WriteByte(checked ((byte)body.Length)); #if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 memoryStream.Write(command); memoryStream.Write(body); memoryStream.Write(requestIdBytes); #else memoryStream.Write(command, 0, command.Length); memoryStream.Write(body, 0, body.Length); memoryStream.Write(requestIdBytes, 0, requestIdBytes.Length); #endif // Add packet packet.AddPacket(0, memoryStream.ToArray()); } } else { // Calculate packet size var counter = (byte)Math.Ceiling(body.Length / (decimal)CalculateBodySize); // Packet size plus one => because first packet specify packet count information // counter.ToString().Length * counter => Reserve first byte for packet number counter = (byte)(Math.Ceiling(((decimal)body.Length + counter.ToString().Length *counter) / CalculateBodySize) + 1); // Specify counter length // counter length = 1 => Like byte 0 To 256 var counterLength = 1; // Initialize packet split packet = new G9PacketSplitHandler(requestId, counter); // Initialize memory stream and save packet data using (var memoryStream = new MemoryStream()) { memoryStream.WriteByte((byte)G9PacketType.MultiPacket); memoryStream.WriteByte((byte)dataType); // First packet length is 2 | 0 => packet number | 1 => Total packet count memoryStream.WriteByte(2); #if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 memoryStream.Write(command); #else memoryStream.Write(command, 0, command.Length); #endif // write packet number memoryStream.WriteByte(0); // write total packet count memoryStream.WriteByte(counter); #if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 memoryStream.Write(requestIdBytes); #else memoryStream.Write(requestIdBytes, 0, requestIdBytes.Length); #endif // Set first packet => packet count information packet.AddPacket(0, memoryStream.ToArray()); } for (byte i = 1; i < counter; i++) { var newBodyStandardSize = CalculateBodySize - (i == 1 ? 0 : counterLength); var offset = newBodyStandardSize * i - newBodyStandardSize; var length = i == counter - 1 ? body.Length - offset : CalculateBodySize - counterLength; // Initialize memory stream and save packet data using (var memoryStream = new MemoryStream()) { memoryStream.WriteByte((byte)G9PacketType.MultiPacket); memoryStream.WriteByte((byte)dataType); memoryStream.WriteByte(checked ((byte)(length + 1))); #if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 memoryStream.Write(command); #else memoryStream.Write(command, 0, command.Length); #endif memoryStream.WriteByte(i); #if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 memoryStream.Write(body.Slice(offset, length)); memoryStream.Write(requestId.ToByteArray()); #else memoryStream.Write(body, offset, length); memoryStream.Write(requestIdBytes, 0, requestIdBytes.Length); #endif // Add total packet packet.AddPacket(i, memoryStream.ToArray()); } } } return(packet); } if (_logging.CheckLoggingIsActive(LogsType.EXCEPTION)) { _logging.LogException(new ArgumentException( $"{LogMessage.CommandLengthError}\n{LogMessage.StandardLength}: {CalculateCommandSize}\n{LogMessage.LengthEntered}: {command.Length}"), null, G9LogIdentity.GENERATE_PACKET, LogMessage.FailedOperation); } return(null); } catch (Exception ex) { if (_logging.CheckLoggingIsActive(LogsType.EXCEPTION)) { _logging.LogException(ex, LogMessage.FailGeneratePacket, G9LogIdentity.GENERATE_PACKET, LogMessage.FailedOperation); } return(null); } }