本文整理汇总了C#中PooledSocket类的典型用法代码示例。如果您正苦于以下问题:C# PooledSocket类的具体用法?C# PooledSocket怎么用?C# PooledSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PooledSocket类属于命名空间,在下文中一共展示了PooledSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadResponse
protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var serverData = new Dictionary<string, string>();
var retval = false;
while (response.Read(socket) && response.KeyLength > 0)
{
retval = true;
var data = response.Data;
var key = BinaryConverter.DecodeKey(data.Array, data.Offset, response.KeyLength);
var value = BinaryConverter.DecodeKey(data.Array, data.Offset + response.KeyLength, data.Count - response.KeyLength);
serverData[key] = value;
}
this.result = serverData;
this.StatusCode = response.StatusCode;
var result = new BinaryOperationResult()
{
StatusCode = StatusCode
};
result.PassOrFail(retval, "Failed to read response");
return result;
}
示例2: ReadResponse
protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
var retval = new Dictionary<string, CacheItem>();
var cas = new Dictionary<string, ulong>();
try
{
GetResponse r;
while ((r = GetHelper.ReadItem(socket)) != null)
{
var key = r.Key;
retval[key] = r.Item;
cas[key] = r.CasValue;
}
}
catch (NotSupportedException)
{
throw;
}
catch (Exception e)
{
log.Error(e);
}
this.result = retval;
this.Cas = cas;
return new TextOperationResult().Pass();
}
示例3: FinishCurrent
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.");
}
示例4: Auth
/// <summary>
/// Implements memcached's SASL auth sequence. (See the protocol docs for more details.)
/// </summary>
/// <param name="socket"></param>
/// <returns></returns>
private bool Auth(PooledSocket socket)
{
SaslStep currentStep = new SaslStart(this.authenticationProvider);
socket.Write(currentStep.GetBuffer());
while (!currentStep.ReadResponse(socket))
{
// challenge-response authentication
if (currentStep.StatusCode == 0x21)
{
currentStep = new SaslContinue(this.authenticationProvider, currentStep.Data);
socket.Write(currentStep.GetBuffer());
}
else
{
if (log.IsWarnEnabled)
log.WarnFormat("Authentication failed, return code: 0x{0:x}", currentStep.StatusCode);
// invalid credentials or other error
return false;
}
}
return true;
}
示例5: ReadResponse
protected internal override bool ReadResponse(PooledSocket socket)
{
this.result = new Dictionary<string, CacheItem>();
this.Cas = new Dictionary<string, ulong>();
var response = new BinaryResponse();
while (response.Read(socket))
{
// found the noop, quit
if (response.CorrelationId == this.noopId)
return true;
string key;
// find the key to the response
if (!this.idToKey.TryGetValue(response.CorrelationId, out key))
{
// we're not supposed to get here tho
log.WarnFormat("Found response with CorrelationId {0}, but no key is matching it.", response.CorrelationId);
continue;
}
if (log.IsDebugEnabled) log.DebugFormat("Reading item {0}", key);
// deserialize the response
int flags = BinaryConverter.DecodeInt32(response.Extra, 0);
this.result[key] = new CacheItem((ushort)flags, response.Data);
this.Cas[key] = response.CAS;
}
// finished reading but we did not find the NOOP
return false;
}
示例6: Read
public unsafe bool Read(PooledSocket socket)
{
this.StatusCode = -1;
if (!socket.IsAlive)
return false;
var header = new byte[HeaderLength];
socket.Read(header, 0, header.Length);
int dataLength, extraLength;
DeserializeHeader(header, out dataLength, out extraLength);
if (dataLength > 0)
{
var data = new byte[dataLength];
socket.Read(data, 0, dataLength);
this.Extra = new ArraySegment<byte>(data, 0, extraLength);
this.Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);
}
return this.StatusCode == 0;
}
示例7: ReadResponse
/// <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 client. 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;
}
示例8: ReadResponse
protected internal override IOperationResult ReadResponse(PooledSocket socket)
{
return new TextOperationResult
{
Success = String.Compare(TextSocketHelper.ReadResponse(socket), "STORED", StringComparison.Ordinal) == 0
};
}
示例9: ReadFrom
public override void ReadFrom(PooledSocket socket)
{
var replies = new List<RedisValue>();
if (Transactional)
{
_multi.ReadFrom(socket); // OK
for (int i = 0; i < _commands.Count; i++)
{
// read "QUEUED"
var status = socket.ExpectSingleLineReply();
}
// The result is a multi-bulk, so
// consume the count of returned items
var count = socket.ExpectMultiBulkCount();
if (count != _commands.Count)
throw new RedisClientException(
String.Format("Invalid number of bulk responses. Expected {0}, Got {1}", _commands.Count, count));
}
foreach (var command in _commands)
{
command.ReadFrom(socket);
replies.Add(command.Value);
}
this.Value = new RedisValue(){Type = RedisValueType.MultiBulk, MultiBulkValues =replies.ToArray()};
}
示例10: ReadAsync
/// <summary>
/// Reads the response from the socket asynchronously.
/// </summary>
/// <param name="socket">The socket to read from.</param>
/// <param name="next">The delegate whihc will continue processing the response. This is only called if the read completes asynchronoulsy.</param>
/// <param name="ioPending">Set totrue if the read is still pending when ReadASync returns. In this case 'next' will be called when the read is finished.</param>
/// <returns>
/// If the socket is already dead, ReadAsync returns false, next is not called, ioPending = false
/// If the read completes synchronously (e.g. data is received from the buffer), it returns true/false depending on the StatusCode, and ioPending is set to true, 'next' will not be called.
/// It returns true if it has to read from the socket, so the operation will complate asynchronously at a later time. ioPending will be true, and 'next' will be called to handle the data
/// </returns>
public bool ReadAsync(PooledSocket socket, Action<bool> next, out bool ioPending)
{
this.StatusCode = -1;
this.currentSocket = socket;
this.next = next;
var asyncEvent = new AsyncIOArgs();
asyncEvent.Count = HeaderLength;
asyncEvent.Next = this.DoDecodeHeaderAsync;
this.shouldCallNext = true;
if (socket.ReceiveAsync(asyncEvent))
{
ioPending = true;
return true;
}
ioPending = false;
this.shouldCallNext = false;
return asyncEvent.Fail
? false
: this.DoDecodeHeader(asyncEvent, out ioPending);
}
示例11: ReadResponse
protected internal override bool ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = response.Read(socket);
this.Cas = response.CAS;
return retval & this.ProcessResponse(response);
}
示例12: ReadResponse
protected internal override bool ReadResponse(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = response.Read(socket);
this.StatusCode = StatusCode;
return retval;
}
示例13: WriteTo
public override void WriteTo(PooledSocket socket)
{
new MultiCommand().WriteTo(socket);
foreach (var command in _commands)
{
command.WriteTo(socket);
}
new ExecCommand().WriteTo(socket);
}
示例14: ReadResponse
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);
}
示例15: Read
public unsafe bool Read(PooledSocket socket)
{
if (!socket.IsAlive)
{
this.StatusCode = -1;
return false;
}
byte[] header = new byte[24];
socket.Read(header, 0, 24);
#if DEBUG_PROTOCOL
if (log.IsDebugEnabled)
{
log.Debug("Received binary response");
StringBuilder sb = new StringBuilder(128).AppendLine();
for (int i = 0; i < header.Length; i++)
{
byte value = header[i];
sb.Append(value < 16 ? "0x0" : "0x").Append(value.ToString("X"));
if (i % 4 == 3) sb.AppendLine(); else sb.Append(" ");
}
log.Debug(sb.ToString());
}
#endif
fixed (byte* buffer = header)
{
if (buffer[0] != MAGIC_VALUE)
throw new InvalidOperationException("Expected magic value " + MAGIC_VALUE + ", received: " + buffer[0]);
int remaining = BinaryConverter.DecodeInt32(buffer, HEADER_BODY);
int extraLength = buffer[HEADER_EXTRA];
byte[] data = new byte[remaining];
socket.Read(data, 0, remaining);
this.Extra = new ArraySegment<byte>(data, 0, extraLength);
this.Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);
this.DataType = buffer[HEADER_DATATYPE];
this.Opcode = buffer[HEADER_OPCODE];
this.StatusCode = BinaryConverter.DecodeInt16(buffer, HEADER_STATUS);
this.KeyLength = BinaryConverter.DecodeInt16(buffer, HEADER_KEY);
this.CorrelationId = BinaryConverter.DecodeInt32(buffer, HEADER_OPAQUE);
this.CAS = BinaryConverter.DecodeUInt64(buffer, HEADER_CAS);
}
return this.StatusCode == 0;
}