internal override byte[] ToByte(long acknowledgement, bool inquiryEnabledOnConnection) { if (_commandBytes == null || inquiryEnabled != inquiryEnabledOnConnection) { inquiryEnabled = inquiryEnabledOnConnection; this.CreateCommand(); _command.commandID = _commandID; this.SerializeCommand(); } if (SupportsAacknowledgement && inquiryEnabled) { byte[] acknowledgementBuffer = HelperFxn.ToBytes(acknowledgement.ToString()); MemoryStream stream = new MemoryStream(_commandBytes, 0, _commandBytes.Length, true, true); stream.Seek(20, SeekOrigin.Begin); stream.Write(acknowledgementBuffer, 0, acknowledgementBuffer.Length); _commandBytes = stream.GetBuffer(); } return(_commandBytes); }
private bool ReadAcknowledgement() { if (_clientManager.SupportAcknowledgement) { if (!_commandStream.EnsureData(ConnectionManager.AckIdBufLen)) { return(false); } _commandStream.CommandLength = ConnectionManager.AckIdBufLen; _commandStream.Position = 0; byte[] ackBuffer = new byte[ConnectionManager.AckIdBufLen]; _commandStream.Read(ackBuffer, 0, ConnectionManager.AckIdBufLen); _commandInfo.AcknowledgementId = HelperFxn.ToInt64(ackBuffer, 0, ConnectionManager.AckIdBufLen); } _commandInfo.State = CommandState.ReadCommand; return(true); }
public virtual void SerializeCommand() { using (MemoryStream stream = new MemoryStream()) { ///Write discarding buffer that socketserver reads byte[] discardingBuffer = new byte[20]; stream.Write(discardingBuffer, 0, discardingBuffer.Length); byte[] size = new byte[Connection.CmdSizeHolderBytesCount]; stream.Write(size, 0, size.Length); ProtoBuf.Serializer.Serialize <Alachisoft.NCache.Common.Protobuf.Command>(stream, this._command); int messageLen = (int)stream.Length - (size.Length + discardingBuffer.Length); size = HelperFxn.ToBytes(messageLen.ToString()); stream.Position = discardingBuffer.Length; stream.Write(size, 0, size.Length); this._commandBytes = stream.ToArray(); stream.Close(); } }
protected override void SerializeCommand() { using (MemoryStream stream = new MemoryStream()) { byte[] discardingBuffer = new byte[20]; stream.Write(discardingBuffer, 0, discardingBuffer.Length); byte[] acknowledgementBuffer = (SupportsAacknowledgement && inquiryEnabled) ? new byte[20] : new byte[0]; stream.Write(acknowledgementBuffer, 0, acknowledgementBuffer.Length); byte[] size = new byte[Connection.CmdSizeHolderBytesCount]; stream.Write(size, 0, size.Length); ProtoBuf.Serializer.Serialize <Alachisoft.NCache.Common.Protobuf.Command>(stream, this._command); int messageLen = (int)stream.Length - (size.Length + discardingBuffer.Length + acknowledgementBuffer.Length); size = HelperFxn.ToBytes(messageLen.ToString()); stream.Position = discardingBuffer.Length + acknowledgementBuffer.Length; stream.Write(size, 0, size.Length); this._commandBytes = stream.ToArray(); stream.Close(); } }
/// <summary> /// Reads a single single response from the stream and processes it. /// </summary> /// <param name="stream"></param> private void ProcessResponse(Stream stream) { Response response; //Reading a response's header... byte[] cmdSzBytes = new byte[CmdSizeHolderBytesCount]; stream.Read(cmdSzBytes, 0, CmdSizeHolderBytesCount); int commandSize = HelperFxn.ToInt32(cmdSzBytes, 0, CmdSizeHolderBytesCount); byte[] cmdBytes = new byte[commandSize]; stream.Read(cmdBytes, 0, commandSize); using (Stream tempStream = new ClusteredMemoryStream(cmdBytes)) { response = ProtoBuf.Serializer.Deserialize <Response>(tempStream); } CommandResponse cmdRespose = null; if (response != null) { cmdRespose = new CommandResponse(false, new Address()); cmdRespose.CacheId = _cacheId; cmdRespose.Result = response; } if (_perfStatsColl.IsEnabled) { _perfStatsColl.IncrementClientResponsesPerSecStats(1); } if (cmdRespose != null) { _container.ProcessResponse(cmdRespose, _serverAddress); } }
public GetRunningServersCommand(string id) { base.name = "GetRunningServersCommand"; _currentVersion = Alachisoft.NCache.Common.ProductVersion.ProductInfo; _getRunningServersCommand = new Alachisoft.NCache.Common.Protobuf.GetRunningServersCommand(); _getRunningServersCommand.cacheId = id; _getRunningServersCommand.isDotnetClient = true; _getRunningServersCommand.requestId = base.RequestId; if (_getRunningServersCommand.productVersion == null) { _getRunningServersCommand.productVersion = new Common.Protobuf.ProductVersion(); } _getRunningServersCommand.productVersion.AddiotionalData = _currentVersion.AdditionalData; _getRunningServersCommand.productVersion.EditionID = _currentVersion.EditionID; _getRunningServersCommand.productVersion.MajorVersion1 = HelperFxn.ParseToByteArray(_currentVersion.MajorVersion1); _getRunningServersCommand.productVersion.MajorVersion2 = HelperFxn.ParseToByteArray(_currentVersion.MajorVersion2); _getRunningServersCommand.productVersion.MinorVersion1 = HelperFxn.ParseToByteArray(_currentVersion.MinorVersion1); _getRunningServersCommand.productVersion.MinorVersion2 = HelperFxn.ParseToByteArray(_currentVersion.MinorVersion2); _getRunningServersCommand.productVersion.ProductName = _currentVersion.ProductName; }
public void Process() { try { ClientManager clientManager = null; lock (ConnectionManager.ConnectionTable) clientManager = (ClientManager)ConnectionManager.ConnectionTable[this._clientId]; if (clientManager != null) { byte[] table = new byte[0]; if (this._isDotNetClient) { if (_newmap != null) { if (this._newmap.Buffer == null) { NewHashmap.Serialize(this._newmap, this._cacheId, true); } table = this._newmap.Buffer; } } else { if (_newmap != null) { String map = HashtableUtil.ToString(this._newmap.Map); String members = HashtableUtil.ToString(this._newmap.Members); String lastViewId = this._newmap.LastViewId.ToString(); StringBuilder toStr = new StringBuilder(); toStr.Append(map); toStr.Append("\t"); toStr.Append(members); toStr.Append("\t"); toStr.Append(lastViewId); toStr.Append("\t"); toStr.Append(this._newmap.UpdateMap); table = HelperFxn.ToBytes(toStr.ToString()); } } Alachisoft.NCache.Common.Protobuf.Response response = new Alachisoft.NCache.Common.Protobuf.Response(); Alachisoft.NCache.Common.Protobuf.HashmapChangedEventResponse hashmapChangedResponse = new Alachisoft.NCache.Common.Protobuf.HashmapChangedEventResponse(); hashmapChangedResponse.table = table; response.hashmapChanged = hashmapChangedResponse; response.responseType = Alachisoft.NCache.Common.Protobuf.Response.Type.HASHMAP_CHANGED_EVENT; IList serializedResponse = Alachisoft.NCache.Common.Util.ResponseHelper.SerializeResponse(response); ConnectionManager.AssureSend(clientManager, serializedResponse, Alachisoft.NCache.Common.Enum.Priority.High); } } catch (Exception exc) { if (SocketServer.Logger.IsErrorLogsEnabled) { SocketServer.Logger.NCacheLog.Error("HashmapChangedEvent.Process", exc.ToString()); } } }
private bool ReadCommand(out object command, out short cmdType, out bool waitForResponse) { command = default(object); cmdType = _commandInfo.CommandType; waitForResponse = false; //In case there is no partial command is in reading process if (_commandInfo.CommandBuffer == null) { bool readCommandType = _clientManager.ClientVersion >= 5000; int nextHeaderLength = ConnectionManager.MessageSizeHeader; if (readCommandType) { nextHeaderLength += sizeof(short); } _expecteDataLength = _commandInfo.CommandLength; if (!_commandStream.EnsureData(nextHeaderLength)) { return(false); } _commandStream.CommandLength = sizeof(short); _commandStream.Position = 0; if (readCommandType) { byte[] type = new byte[2]; _commandStream.Read(type, 0, type.Length); _commandInfo.CommandType = HelperFxn.ConvertToShort(type); } else { _commandInfo.CommandType = 0; } cmdType = _commandInfo.CommandType; _commandStream.CommandLength = ConnectionManager.MessageSizeHeader; _commandStream.Position = 0; _commandStream.Read(LengthBuffer, 0, ConnectionManager.MessageSizeHeader); int commandSize = ToInt32(LengthBuffer, 0, ConnectionManager.MessageSizeHeader); _expecteDataLength -= nextHeaderLength; _commandInfo.CommandBuffer = new byte[commandSize]; _commandInfo.BufferOffset = 0; } if (_commandStream.HasAnyData()) { _commandStream.Position = 0; int streamLength = (int)_commandStream.AvailableData; int dataToCopy = _expecteDataLength <= streamLength ? _expecteDataLength : streamLength; _commandStream.CommandLength = dataToCopy; _commandStream.Read(_commandInfo.CommandBuffer, _commandInfo.BufferOffset, dataToCopy); _expecteDataLength -= dataToCopy; _commandInfo.BufferOffset += dataToCopy; } //command is full read from main command stream so let's parse it if (_expecteDataLength == 0) { using (MemoryStream stream = new MemoryStream(_commandInfo.CommandBuffer)) { cmdType = _commandInfo.CommandType; //get ready for next command to parse; reset before command deserialize _commandInfo.CommandBuffer = null; _commandInfo.BufferOffset = 0; _commandInfo.CommandType = 0; if (_clientManager.ClientVersion >= 5000) { command = ProtoBuf.Serializer.NonGeneric.Deserialize(_commandTypes[cmdType], stream); } else { command = ProtoBuf.Serializer.Deserialize <Alachisoft.NCache.Common.Protobuf.Command>(stream); } } _commandInfo.State = CommandState.ReadLength; _commandInfo.CommandLength = 0; waitForResponse = true; return(true); } return(false); }