本文整理汇总了C#中IChannelHandlerContext.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IChannelHandlerContext.WriteAsync方法的具体用法?C# IChannelHandlerContext.WriteAsync怎么用?C# IChannelHandlerContext.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IChannelHandlerContext
的用法示例。
在下文中一共展示了IChannelHandlerContext.WriteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteAsync
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
if (message is int)
{
var buf = Unpooled.Buffer(4).WriteInt((int) message);
return context.WriteAsync(buf);
}
return context.WriteAsync(message);
}
示例2: WriteAsync
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
if (message is int)
{
State = State.WriteMessages((int) message);
Logger.Debug("[Client-Write] Writing: {0}", message);
}
return context.WriteAsync(message);
}
示例3: ChannelRead
public override void ChannelRead(IChannelHandlerContext context, object message)
{
var buffer = message as IByteBuffer;
if (buffer != null)
{
Console.WriteLine("Received from client: " + buffer.ToString(Encoding.UTF8));
}
context.WriteAsync(message);
}
示例4: WriteAsync
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
var packet = message as PublishPacket;
if (packet != null)
{
IByteBuffer result = ApplyCompression(packet.Payload, CompressionMode.Compress);
packet.Payload = result;
}
return context.WriteAsync(message);
}
开发者ID:kdotchkoff,项目名称:azure-iot-protocol-gateway,代码行数:10,代码来源:MqttPacketPayloadCompressionHandler.cs
示例5: ChannelRead
public override void ChannelRead(IChannelHandlerContext context, object message)
{
var byteBuffer = message as IByteBuffer;
if (byteBuffer != null)
{
this.buffer.Initialize();
byteBuffer.Duplicate().ReadBytes(this.buffer, 0, byteBuffer.ReadableBytes);
string msg = Encoding.UTF8.GetString(this.buffer);
Console.WriteLine("Received from server: " + msg);
}
context.WriteAsync(message);
}
示例6: ContinueScenarioExecution
void ContinueScenarioExecution(IChannelHandlerContext context)
{
if (!this.testScenario.MoveNext())
{
context.CloseAsync()
.ContinueWith(
t => this.completion.TrySetException(t.Exception),
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
this.completion.TryComplete();
return;
}
foreach (object message in this.testScenario.Current.SendMessages)
{
context.WriteAsync(message)
.ContinueWith(
t => this.completion.TrySetException(t.Exception),
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
}
context.Flush();
}
示例7: WriteAsync
public virtual Task WriteAsync(IChannelHandlerContext context, object message) => context.WriteAsync(message);
示例8: ChannelRead
public override void ChannelRead(IChannelHandlerContext context, object message)
{
context.WriteAsync(message);
}
示例9: ExecuteStep
void ExecuteStep(IChannelHandlerContext context, TestScenarioStep currentStep)
{
if (!context.Channel.Open)
{
// todo: dispose scheduled work in case of channel closure instead?
return;
}
Task lastTask = null;
object lastMessage = null;
foreach (object message in currentStep.Messages)
{
lastMessage = message;
var writeTimeoutCts = new CancellationTokenSource();
Task task = context.WriteAsync(message);
object timeoutExcMessage = message;
context.Channel.EventLoop.Schedule(
() => this.completion.TrySetException(new TimeoutException(string.Format("Sending of message did not complete in time: {0}", timeoutExcMessage))),
this.sendTimeout,
writeTimeoutCts.Token);
task.ContinueWith(
t => writeTimeoutCts.Cancel(),
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
task.OnFault(t => this.completion.TrySetException(t.Exception));
lastTask = task;
}
if (currentStep.WaitForFeedback)
{
if (this.responseTimeout > TimeSpan.Zero)
{
this.responseTimeoutCts = new CancellationTokenSource();
if (lastTask == null)
{
this.ScheduleReadTimeoutCheck(context, null);
}
else
{
lastTask.ContinueWith(
t => this.ScheduleReadTimeoutCheck(context, lastMessage),
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
}
}
context.Flush();
if (!currentStep.WaitForFeedback)
{
context.Channel.EventLoop.Execute(
ctx => this.ContinueScenarioExecution((IChannelHandlerContext)ctx),
context);
}
}
示例10: ProcessPendingSubscriptionChanges
async void ProcessPendingSubscriptionChanges(IChannelHandlerContext context)
{
try
{
do
{
ISessionState newState = this.sessionState.Copy();
Queue<Packet> queue = this.SubscriptionChangeQueue;
var acks = new List<Packet>(queue.Count);
foreach (Packet packet in queue) // todo: if can queue be null here, don't force creation
{
switch (packet.PacketType)
{
case PacketType.SUBSCRIBE:
acks.Add(Util.AddSubscriptions(newState, (SubscribePacket)packet, this.maxSupportedQosToClient));
break;
case PacketType.UNSUBSCRIBE:
acks.Add(Util.RemoveSubscriptions(newState, (UnsubscribePacket)packet));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
queue.Clear();
if (!this.sessionState.IsTransient)
{
// save updated session state, make it current once successfully set
await this.sessionStateManager.SetAsync(this.deviceId, newState);
}
this.sessionState = newState;
// release ACKs
var tasks = new List<Task>(acks.Count);
foreach (Packet ack in acks)
{
tasks.Add(context.WriteAsync(ack));
}
context.Flush();
await Task.WhenAll(tasks);
PerformanceCounters.PacketsSentPerSecond.IncrementBy(acks.Count);
}
while (this.subscriptionChangeQueue.Count > 0);
this.ResetState(StateFlags.ChangingSubscriptions);
}
catch (Exception ex)
{
ShutdownOnError(context, "-> UN/SUBSCRIBE", ex);
}
}
示例11: WriteAsync
public override Task WriteAsync(IChannelHandlerContext ctx, object msg)
{
if (this.Logger.IsEnabled(this.InternalLevel))
{
this.Logger.Log(this.InternalLevel, this.Format(ctx, "WRITE", msg));
}
return ctx.WriteAsync(msg);
}
示例12: WriteAsync
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
var buf = message as IByteBuffer;
if (buf != null)
{
var tcs = new TaskCompletionSource<int>(buf);
this.sslStreamWriteQueue.Enqueue(tcs);
State oldState = this.state;
if ((oldState & (State.WriteInProgress | State.Authenticated)) == State.Authenticated) // authenticated but not writing already
{
this.state = oldState | State.WriteInProgress;
this.ScheduleWriteToSslStream(buf);
}
return tcs.Task;
}
else
{
// it's not IByteBuffer - passthrough
// todo: non-leaking termination policy?
return context.WriteAsync(message);
}
}
示例13: ChannelRead
public override void ChannelRead(IChannelHandlerContext context, object message)
{
var buffer = (IByteBuffer)message;
if (_logger.IsEnabled(LogLevel.Information))
_logger.Information($"接收到消息:{buffer.ToString(Encoding.UTF8)}。");
var content = buffer.ToArray();
TransportMessage<RemoteInvokeMessage> transportMessage;
try
{
transportMessage = _serializer.Deserialize<TransportMessage<RemoteInvokeMessage>>(content);
}
catch (Exception exception)
{
_logger.Error($"将接收到的消息反序列化成 TransportMessage<RemoteInvokeMessage> 时发送了错误,消息内容:{{buffer.ToString(Encoding.UTF8)}}。", exception);
return;
}
var invokeMessage = transportMessage.Content;
var entry = _serviceEntryLocate.Locate(invokeMessage);
if (entry == null)
{
if (_logger.IsEnabled(LogLevel.Error))
_logger.Error($"根据服务Id:{invokeMessage.ServiceId},找不到服务条目。");
return;
}
if (_logger.IsEnabled(LogLevel.Debug))
_logger.Debug("准备执行本地逻辑。");
object result;
try
{
result = entry.Func(invokeMessage.Parameters);
}
catch (Exception exception)
{
if (_logger.IsEnabled(LogLevel.Error))
_logger.Error("执行本地逻辑时候发生了错误。", exception);
result = null;
}
try
{
if (_logger.IsEnabled(LogLevel.Debug))
_logger.Debug("准备发送响应消息。");
var resultData = _serializer.Serialize(new TransportMessage
{
Content = result,
Id = transportMessage.Id
});
buffer = Unpooled.Buffer(resultData.Length);
buffer.WriteBytes(resultData);
context.WriteAsync(buffer);
if (_logger.IsEnabled(LogLevel.Debug))
_logger.Debug("响应消息发送成功。");
}
catch (Exception exception)
{
if (_logger.IsEnabled(LogLevel.Error))
_logger.Error("发送响应消息时候发生了异常。", exception);
}
}
示例14: ChannelActive
public override void ChannelActive(IChannelHandlerContext context)
{
// write a large enough blob of data that it has to be split into multiple writes
var channel = context.Channel;
_tasks.Enqueue(
context.WriteAsync(context.Allocator.Buffer().WriteZero(1048576))
.ContinueWith(tr => channel.CloseAsync(),
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion)
.Unwrap());
_tasks.Enqueue(context.WriteAsync(context.Allocator.Buffer().WriteZero(1048576)));
context.Flush();
_tasks.Enqueue(context.WriteAsync(context.Allocator.Buffer().WriteZero(1048576)));
context.Flush();
}
示例15: WriteAsync
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
this.throughput.Increment();
return context.WriteAsync(message);
}