internal virtual void injectFullFrame(byte[] frameBuffer) { // the process of injecting a complete frame // is implemented in terms of existing steps that are also used // for reading data from the physical channel // 1. the frame header is parsed // 2. the frame payload is consumed // injecting full frames is allowed only when the channel // is prepared to process whole frames System.Diagnostics.Debug.Assert( state == InputState.READING_WHOLE_FRAMES); byte[] rawBuffer = frameBuffer; byte[] header = new byte[Frame.FRAME_HEADER_SIZE]; Array.Copy(rawBuffer, header, header.Length); int payloadSize = parseFrameHeader(header); createCurrentIncomingFrame(payloadSize); currentIncomingFrame.dataBuffer.Write( rawBuffer, Frame.FRAME_HEADER_SIZE, payloadSize); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.HIGH, "Frame received:" + " from: " + target + " tid: " + currentIncomingFrameTransportId + " fid: " + currentIncomingFrameNumber); } // store this frame together with other frames // for the same message storeOrDispatchCurrentFrame(); }
internal Channel(string target, Options options, IncomingMessageDispatchCallback incomingMessageDispatchCallback, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.target = target; this.options = options; this.logCallback = logCallback; this.logLevel = logLevel; connect(incomingMessageDispatchCallback); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.LOW, "Connected to " + target); } }
internal Channel(string target, Options options, IncomingMessageDispatchCallback incomingMessageDispatchCallback, IOWorker ioWorker, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.target = target; this.options = options; this.logCallback = logCallback; this.logLevel = logLevel; connect(incomingMessageDispatchCallback, ioWorker); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.LOW, "Connected to " + target); } }
// used by listener when accepting new connections internal Channel(Socket acceptedChannel, string sourceTarget, IncomingMessageDispatchCallback incomingMessageDispatchCallback, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.target = sourceTarget; this.options = options; this.connection = new NetworkUtils.TransportChannel(acceptedChannel); this.logCallback = logCallback; this.logLevel = logLevel; createReaderWriter(incomingMessageDispatchCallback); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.LOW, "Accepted connection from " + target); } }
// used by listener when accepting new connections internal Channel(Socket acceptedChannel, string sourceTarget, IncomingMessageDispatchCallback incomingMessageDispatchCallback, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.target = sourceTarget; this.options = options; this.connection = new NetworkUtils.TransportChannel(acceptedChannel); this.logCallback = logCallback; this.logLevel = logLevel; createReaderWriter(incomingMessageDispatchCallback); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.LOW, "Accepted connection from " + target); } }
public virtual void close() { if (connection != null) { try { connection.close(); } catch (Exception) { // ignore } connection = null; channelWriter.notifyCancellation(); if (logCallback != null) { logCallback.Log(LogEventArgs.LogLevel.LOW, "Closed connection to " + target); } } }
internal virtual void doSomeOutput() { OutgoingFrame frame; lock (outgoingFrames) { if (outgoingFrames.Count == 0) { return; } frame = outgoingFrames[0]; if (frame.closeFlag) { outgoingFrames.RemoveAt(0); // this artificial exception forces the worker to // physically close this connection and remove it // from the set of channels throw new YAMIIOException("dummy"); } } if (connection.connectedChannel != null) { // TCP connection int sent = connection.connectedChannel.Send(frame.BuffersToSent); frame.AddSentBytes(sent); } else if (connection.ssl != null) { // TCP SSL connection int sent = connection.writingQueue.PutMany(frame.BuffersToSent); frame.AddSentBytes(sent); } else { // UDP connection connection.datagramChannel.SendTo( frame.SingleBuffer, connection.targetAddress); } if (frame.buffersConsumed()) { // the first frame (head in the queue) was complmesetely // sent -> notify its progress callback and remove // the head from the queue lock (outgoingFrames) { outgoingFrames.RemoveAt(0); } MessageProgressCallback callback = frame.messageProgressCallback; if (callback != null) { callback.progress(frame.byteCount, frame.totalByteCount); } if (logCallback != null) { if (logLevel == LogEventArgs.LogLevel.HIGH) { logCallback.Log(LogEventArgs.LogLevel.HIGH, "Frame sent:" + " target: " + target + " tid: " + frame.transportId + " fid: " + frame.frameNumber + " size: " + frame.byteCount + "/" + frame.totalByteCount); } } } }