protected internal override IOperationResult ReadResponse(PooledSocket socket) { return(new TextOperationResult { Success = String.Compare(TextSocketHelper.ReadResponse(socket), "DELETED", StringComparison.Ordinal) == 0 }); }
protected internal override ValueTask <IOperationResult> ReadResponseAsync(PooledSocket socket) { return(new ValueTask <IOperationResult>(new TextOperationResult { Success = String.Compare(TextSocketHelper.ReadResponse(socket), "STORED", StringComparison.Ordinal) == 0 })); }
protected internal override async Task <IOperationResult> ReadResponseAsync(PooledSocket socket, CancellationToken cancellationToken = default(CancellationToken)) { return(new TextOperationResult { Success = String.Compare(await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false), "DELETED", StringComparison.Ordinal) == 0 }); }
/// <summary> /// Reads the response of the server. /// </summary> /// <returns>The data sent by the memcached server.</returns> /// <exception cref="T:System.InvalidOperationException">The server did not sent a response or an empty line was returned.</exception> /// <exception cref="T:Enyim.Caching.Memcached.MemcachedException">The server did not specified any reason just returned the string ERROR. - or - The server returned a SERVER_ERROR, in this case the Message of the exception is the message returned by the server.</exception> /// <exception cref="T:Enyim.Caching.Memcached.MemcachedClientException">The server did not recognize the request sent by the memcachedClient. The Message of the exception is the message returned by the server.</exception> public static string ReadResponse(PooledSocket socket) { string response = TextSocketHelper.ReadLine(socket); if (log.IsDebugEnabled) { log.Debug("Received response: " + response); } if (String.IsNullOrEmpty(response)) { throw new MemcachedClientException("Empty response received."); } if (String.Compare(response, GenericErrorResponse, StringComparison.Ordinal) == 0) { throw new NotSupportedException("Operation is not supported by the server or the request was malformed. If the latter please report the bug to the developers."); } if (response.Length >= ErrorResponseLength) { if (String.Compare(response, 0, ClientErrorResponse, 0, ErrorResponseLength, StringComparison.Ordinal) == 0) { throw new MemcachedClientException(response.Remove(0, ErrorResponseLength)); } else if (String.Compare(response, 0, ServerErrorResponse, 0, ErrorResponseLength, StringComparison.Ordinal) == 0) { throw new MemcachedException(response.Remove(0, ErrorResponseLength)); } } return(response); }
/// <summary> /// Reads the response of the server /// </summary> /// <returns>The data sent by the memcached server.</returns> /// <exception cref="InvalidOperationException">The server did not sent a response or an empty line was returned.</exception> /// <exception cref="MemcachedClientException">The server did not specified any reason just returned the string ERROR. - or - The server returned a SERVER_ERROR, in this case the Message of the exception is the message returned by the server.</exception> /// <exception cref="MemcachedClientException">The server did not recognize the request sent by the client. The Message of the exception is the message returned by the server.</exception> public static async Task <string> ReadResponseAsync(PooledSocket socket, CancellationToken cancellationToken = default(CancellationToken)) { string response = await TextSocketHelper.ReadLineAsync(socket, cancellationToken).ConfigureAwait(false); if (String.IsNullOrEmpty(response)) { throw new MemcachedClientException("Empty response received."); } if (String.Compare(response, TextSocketHelper.GenericErrorResponse, StringComparison.Ordinal) == 0) { throw new NotSupportedException("Operation is not supported by the server or the request was malformed. If the latter please report the bug to the developers."); } TextSocketHelper.Logger = TextSocketHelper.Logger ?? Caching.Logger.CreateLogger(typeof(TextSocketHelper)); if (TextSocketHelper.Logger.IsEnabled(LogLevel.Debug)) { TextSocketHelper.Logger.LogDebug("Received response: " + response); } if (response.Length >= TextSocketHelper.ErrorResponseLength) { if (String.Compare(response, 0, TextSocketHelper.ClientErrorResponse, 0, TextSocketHelper.ErrorResponseLength, StringComparison.Ordinal) == 0) { throw new MemcachedClientException(response.Remove(0, TextSocketHelper.ErrorResponseLength)); } else if (String.Compare(response, 0, TextSocketHelper.ServerErrorResponse, 0, TextSocketHelper.ErrorResponseLength, StringComparison.Ordinal) == 0) { throw new MemcachedClientException(response.Remove(0, TextSocketHelper.ErrorResponseLength)); } } return(response); }
protected internal override IList <ArraySegment <byte> > GetBuffer() { var command = String.IsNullOrEmpty(this.type) ? "stats" + TextSocketHelper.CommandTerminator : "stats " + this.type + TextSocketHelper.CommandTerminator; return(TextSocketHelper.GetCommandBuffer(command)); }
protected internal override IList <ArraySegment <byte> > GetBuffer() { // gets key1 key2 key3 ... keyN\r\n var command = "gets " + String.Join(" ", Keys.ToArray()) + TextSocketHelper.CommandTerminator; return(TextSocketHelper.GetCommandBuffer(command)); }
public static async Task FinishCurrentAsync(PooledSocket socket, CancellationToken cancellationToken = default) { string response = await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false); if (String.Compare(response, "END", StringComparison.Ordinal) != 0) { throw new MemcachedClientException("No END was received."); } }
public static void FinishCurrent(PooledSocket socket) { string response = TextSocketHelper.ReadResponse(socket); if (String.Compare(response, "END", StringComparison.Ordinal) != 0) { throw new MemcachedClientException("No END was received."); } }
public static async Task <GetResponse> ReadItemAsync(PooledSocket socket, CancellationToken cancellationToken = default) { var description = await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false); if (String.Compare(description, "END", StringComparison.Ordinal) == 0) { return(null); } else if (description.Length < 6 || String.Compare(description, 0, "VALUE ", 0, 6, StringComparison.Ordinal) != 0) { throw new MemcachedClientException("No VALUE response received.\r\n" + description); } // response is: // VALUE <key> <flags> <bytes> [<cas unique>] // 0 1 2 3 4 // // cas only exists in 1.2.4+ // ulong cas = 0; var parts = description.Split(' '); if (parts.Length == 5) { if (!UInt64.TryParse(parts[4], out cas)) { throw new MemcachedClientException("Invalid CAS VALUE received."); } } else if (parts.Length < 4) { throw new MemcachedClientException("Invalid VALUE response received: " + description); } var flags = UInt16.Parse(parts[2], CultureInfo.InvariantCulture); var length = Int32.Parse(parts[3], CultureInfo.InvariantCulture); var allData = new byte[length]; var eod = new byte[2]; await socket.ReceiveAsync(allData, 0, length, cancellationToken).ConfigureAwait(false); await socket.ReceiveAsync(eod, 0, 2, cancellationToken).ConfigureAwait(false); // data is terminated by \r\n var result = new GetResponse(parts[1], flags, cas, allData); GetHelper.Logger = GetHelper.Logger ?? Caching.Logger.CreateLogger(typeof(GetHelper)); if (GetHelper.Logger.IsEnabled(LogLevel.Debug)) { GetHelper.Logger.LogDebug("Received value. Data type: {0}, size: {1}.", result.Item.Flags, result.Item.Data.Count); } return(result); }
protected internal override IList <ArraySegment <byte> > GetBuffer() { var command = (this.mode == MutationMode.Increment ? "incr " : "decr ") + this.Key + " " + this.delta.ToString(CultureInfo.InvariantCulture) + TextSocketHelper.CommandTerminator; return(TextSocketHelper.GetCommandBuffer(command)); }
public static GetResponse ReadItem(PooledSocket socket) { string description = TextSocketHelper.ReadResponse(socket); if (String.Compare(description, "END", StringComparison.Ordinal) == 0) { return(null); } if (description.Length < 6 || String.Compare(description, 0, "VALUE ", 0, 6, StringComparison.Ordinal) != 0) { throw new MemcachedClientException("No VALUE response received.\r\n" + description); } ulong cas = 0; string[] parts = description.Split(' '); // response is: // VALUE <key> <flags> <bytes> [<cas unique>] // 0 1 2 3 4 // // cas only exists in 1.2.4+ // if (parts.Length == 5) { if (!UInt64.TryParse(parts[4], out cas)) { throw new MemcachedClientException("Invalid CAS VALUE received."); } } else if (parts.Length < 4) { throw new MemcachedClientException("Invalid VALUE response received: " + description); } ushort flags = UInt16.Parse(parts[2], CultureInfo.InvariantCulture); int length = Int32.Parse(parts[3], CultureInfo.InvariantCulture); byte[] allData = new byte[length]; byte[] eod = new byte[2]; socket.Read(allData, 0, length); socket.Read(eod, 0, 2); // data is terminated by \r\n GetResponse retval = new GetResponse(parts[1], flags, cas, allData); if (log.IsDebugEnabled) { log.DebugFormat("Received value. Data type: {0}, size: {1}.", retval.Item.Flags, retval.Item.Data.Count); } return(retval); }
protected internal override bool ReadResponse(PooledSocket socket) { string response = TextSocketHelper.ReadResponse(socket); //maybe we should throw an exception when the item is not found? if (String.Compare(response, "NOT_FOUND", StringComparison.Ordinal) == 0) { return(false); } return(UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result)); }
protected internal override IOperationResult ReadResponse(PooledSocket socket) { string response = TextSocketHelper.ReadResponse(socket); var result = new TextOperationResult(); //maybe we should throw an exception when the item is not found? if (String.Compare(response, "NOT_FOUND", StringComparison.Ordinal) == 0) { return(result.Fail("Failed to read response. Item not found")); } result.Success = UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result); return(result); }
protected internal override async Task <IOperationResult> ReadResponseAsync(PooledSocket socket, CancellationToken cancellationToken = default(CancellationToken)) { string response = await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false); var result = new TextOperationResult(); //maybe we should throw an exception when the item is not found? if (String.Compare(response, "NOT_FOUND", StringComparison.Ordinal) == 0) { return(result.Fail("Failed to read response. Item not found")); } result.Success = UInt64.TryParse(response, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out this.result); return(result); }
protected internal override System.Collections.Generic.IList <ArraySegment <byte> > GetBuffer() { // todo adjust the size to fit a request using a fnv hashed key var sb = new StringBuilder(128); var buffers = new List <ArraySegment <byte> >(3); switch (this.command) { case StoreCommand.Add: sb.Append("add "); break; case StoreCommand.Replace: sb.Append("replace "); break; case StoreCommand.Set: sb.Append("set "); break; case StoreCommand.Append: sb.Append("append "); break; case StoreCommand.Prepend: sb.Append("prepend "); break; case StoreCommand.CheckAndSet: sb.Append("cas "); break; default: throw new MemcachedClientException(command + " is not supported."); } sb.Append(this.Key); sb.Append(" "); sb.Append(this.value.Flags.ToString(CultureInfo.InvariantCulture)); sb.Append(" "); sb.Append(this.expires.ToString(CultureInfo.InvariantCulture)); sb.Append(" "); var data = this.value.Data; sb.Append(Convert.ToString(data.Count, CultureInfo.InvariantCulture)); if (command == StoreCommand.CheckAndSet) { sb.Append(" "); sb.Append(Convert.ToString(this.cas, CultureInfo.InvariantCulture)); } sb.Append(TextSocketHelper.CommandTerminator); TextSocketHelper.GetCommandBuffer(sb.ToString(), buffers); buffers.Add(data); buffers.Add(StoreOperationBase.DataTerminator); return(buffers); }
protected internal override bool ReadResponse(PooledSocket socket) { var serverData = new Dictionary <string, string>(); while (true) { string line = TextSocketHelper.ReadResponse(socket); // stat values are terminated by END if (String.Compare(line, "END", StringComparison.Ordinal) == 0) { break; } // expected response is STAT item_name item_value if (line.Length < 6 || String.Compare(line, 0, "STAT ", 0, 5, StringComparison.Ordinal) != 0) { if (log.IsWarnEnabled) { log.Warn("Unknow response: " + line); } continue; } // get the key&value string[] parts = line.Remove(0, 5).Split(' '); if (parts.Length != 2) { if (log.IsWarnEnabled) { log.Warn("Unknow response: " + line); } continue; } // store the stat item serverData[parts[0]] = parts[1]; } this.result = serverData; return(true); }
protected internal override async Task <IOperationResult> ReadResponseAsync(PooledSocket socket, CancellationToken cancellationToken = default(CancellationToken)) { var serverData = new Dictionary <string, string>(); while (true) { string line = await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false); // stat values are terminated by END if (String.Compare(line, "END", StringComparison.Ordinal) == 0) { break; } // expected response is STAT item_name item_value if (line.Length < 6 || String.Compare(line, 0, "STAT ", 0, 5, StringComparison.Ordinal) != 0) { if (this._logger.IsEnabled(LogLevel.Debug)) { this._logger.LogWarning("Unknow response: " + line); } continue; } // get the key&value string[] parts = line.Remove(0, 5).Split(' '); if (parts.Length != 2) { if (this._logger.IsEnabled(LogLevel.Debug)) { this._logger.LogWarning("Unknow response: " + line); } continue; } // store the stat item serverData[parts[0]] = parts[1]; } this._result = serverData; return(new TextOperationResult().Pass()); }
protected internal override System.Collections.Generic.IList <ArraySegment <byte> > GetBuffer() { var command = "delete " + this.Key + TextSocketHelper.CommandTerminator; return(TextSocketHelper.GetCommandBuffer(command)); }
protected internal override bool ReadResponse(PooledSocket socket) { TextSocketHelper.ReadResponse(socket); return(true); }
protected internal override ValueTask <IOperationResult> ReadResponseAsync(PooledSocket socket) { TextSocketHelper.ReadResponse(socket); return(new ValueTask <IOperationResult>(new TextOperationResult().Pass())); }
protected internal override async Task <IOperationResult> ReadResponseAsync(PooledSocket socket, CancellationToken cancellationToken = default) { await TextSocketHelper.ReadResponseAsync(socket, cancellationToken).ConfigureAwait(false); return(new TextOperationResult().Pass()); }
protected internal override IOperationResult ReadResponse(PooledSocket socket) { TextSocketHelper.ReadResponse(socket); return(new TextOperationResult().Pass()); }
protected internal override IList <System.ArraySegment <byte> > GetBuffer() => TextSocketHelper.GetCommandBuffer("flush_all" + TextSocketHelper.CommandTerminator);
protected internal override bool ReadResponse(PooledSocket socket) { return(String.Compare(TextSocketHelper.ReadResponse(socket), "STORED", StringComparison.Ordinal) == 0); }
protected internal override IList <ArraySegment <byte> > GetBuffer() { var command = "gets " + this.Key + TextSocketHelper.CommandTerminator; return(TextSocketHelper.GetCommandBuffer(command)); }