void IConnectionAdapter.Send(Frame frame) { SendState state; if (_closing || _disposed) return; state = new SendState(); state.data = frame.ToBytes(); if (_sending) { _sendQueue.Enqueue(state); return; } processSendState(state); }
internal void Send(Frame frame) { if (_adapter == null) return; _adapter.Send(frame); }
void resolveHandler(Frame frame) { string path; try { path = Encoding.UTF8.GetString(frame.Payload); } catch (EncoderFallbackException) { // Ignore unresolved paths; return; } Channel channel; if (_channels.ContainsKey(path) == false) { return; } channel = _channels[path]; if (channel.State == ChannelState.Closing) { deallocChannel(channel); return; } if (frame.ResolveFlag != ResolveFlag.Success) { deallocChannel(channel, frame, "Unable to resolve path"); return; } _routes.Add(frame.Ptr, channel); channel.handleResolved(frame.Ptr); Frame open = Frame.Create(frame.Ptr, channel.Mode, channel.ctoken, channel.token); _adapter.Send(open); }
void signalHandler(Frame frame) { Channel channel; if (frame.SignalFlag == SignalFlag.Emit) { if (frame.Ptr == 0) { foreach (KeyValuePair<string, Channel> kvp in _channels) { channel = kvp.Value; channel.handleSignal(frame.Clone()); } } else { if (_routes.ContainsKey(frame.Ptr) == false) return; channel = _routes[frame.Ptr]; channel.handleSignal(frame); } } else { if (frame.Ptr == 0) { foreach (KeyValuePair<string, Channel> kvp in _channels) { channel = kvp.Value; deallocChannel(channel, frame.Clone(), null); } } else { if (_routes.ContainsKey(frame.Ptr) == false) return; channel = _routes[frame.Ptr]; if (channel.State != ChannelState.Closing) { // We havent closed our channel yet. We therefor need // to send an ENDSIG in response to this packet. Frame end = Frame.Create(frame.Ptr, SignalFlag.End, ContentType.Utf, null); _adapter.Send(end); } deallocChannel(channel, frame, null); } } }
void dataHandler(Frame frame) { if (frame.Payload == null || frame.Payload.Length == 0) { close("Protocol error: Zero data packet sent received"); return; } Channel channel; if (frame.Ptr == 0) { foreach (KeyValuePair<string, Channel> kvp in _channels) { channel = kvp.Value; if ((channel.Mode & ChannelMode.Read) == ChannelMode.Read) { channel.handleData(frame.Clone()); } } return; } if (_routes.ContainsKey(frame.Ptr) == false) return; channel = _routes[frame.Ptr]; if ((channel.Mode & ChannelMode.Read) == ChannelMode.Read) { channel.handleData(frame); } }
void openHandler(Frame frame) { Channel channel; if (_routes.ContainsKey(frame.Ptr) == false) { close("Protocol error: Server sent invalid open packet"); return; } channel = _routes[frame.Ptr]; if (channel.State != ChannelState.Resolved) { close("Protocol error: Server sent open to an open channel"); return; } if (frame.OpenFlag == OpenFlag.Success) { channel.handleOpen(frame); return; } deallocChannel(channel, frame, "Open was denied"); }
void frameHandler(Frame frame) { switch (frame.OpCode) { case OpCode.Open: openHandler(frame); break; case OpCode.Data: dataHandler(frame); break; case OpCode.Signal: signalHandler(frame); break; case OpCode.Resolve: resolveHandler(frame); break; } }
void deallocChannel(Channel channel, Frame frame, string reason) { if (channel.AbsolutePath == null) return; if (_channels.ContainsKey(channel.AbsolutePath)) _channels.Remove(channel.AbsolutePath); if (channel.Ptr != 0) _routes.Remove(channel.Ptr); _refcount--; if (_refcount == 0) { close(); } channel.handleClose(frame, reason); }
internal void handleClose(Frame frame, string reason) { if (_state == ChannelState.Closed) { return; } _state = ChannelState.Closed; _ptr = 0; if (_closedInvoker == null) return; ChannelCloseEventArgs e; if (frame == null) { e = new ChannelCloseEventArgs(false, false, reason); } else { bool wasDenied = frame.OpCode == OpCode.Open; bool wasClean = wasDenied ? false : frame.SignalFlag == SignalFlag.End; e = new ChannelCloseEventArgs(frame.ContentType, frame.Payload, wasDenied, wasClean, reason); } _closedInvoker(this, e); }
internal void handleClose(Frame frame) { handleClose(frame, "Unknown reason"); }
internal void handleSignal(Frame frame) { if (_signalInvoker == null) return; ChannelEventArgs e; e = new ChannelEventArgs(frame.ContentType, frame.Payload); _signalInvoker(this, e); }
internal void handleData(Frame frame) { if (_dataInvoker == null) return; ChannelDataEventArgs e; e = new ChannelDataEventArgs(frame.ContentType, frame.Payload, frame.PriorityFlag); _dataInvoker(this, e); }
internal void handleOpen(Frame frame) { if (_state == ChannelState.Closing) { // User has called close before it was open. Send // the pending ENDSIG. Frame end = Frame.Create(_ptr, SignalFlag.End, coutro, outro); outro = null; _connection.Send(end); return; } _state = ChannelState.Open; if (_openInvoker == null) return; ChannelEventArgs e; e = new ChannelEventArgs(frame.ContentType, frame.Payload); _openInvoker(this, e); }