public ChannelReader( NetworkUtils.TransportChannel connection, IncomingMessageDispatchCallback incomingMessageDispatchCallback, string target, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.connection = connection; this.incomingMessageDispatchCallback = incomingMessageDispatchCallback; this.target = target; this.logCallback = logCallback; this.logLevel = logLevel; incomingFrames = new Dictionary<int, IncomingMessageFrames>(); if (connection != null) { if (connection.connectedChannel != null) { // stream-based connection headerBuffer = new MemoryStream(Frame.FRAME_HEADER_SIZE); setReadingFrameHeaderState(); } else { // datagram-based connection wholeFrameBuffer = new byte[options.udpFrameSize]; state = InputState.READING_WHOLE_FRAMES; } } deliverAsRawBinary = options.deliverAsRawBinary; }
public DispatchManager( object sender, // Agent reference for callbacks Options options, WaterFlowManager incomingFlowManager, IOWorker ioWorker, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.sender = sender; this.incomingFlowManager = incomingFlowManager; this.ioWorker = ioWorker; this.logCallback = logCallback; this.logLevel = logLevel; messageQueue = new LinkedList<IncomingMessage>(); objectMap = new Dictionary<string, IncomingMessageHandler>(); anyObjectCallback = null; int numOfThreads = options.dispatcherThreads; dispatchers = new List<Thread>(); for (int i = 0; i != numOfThreads; ++i) { Thread th = new Thread((new Dispatcher(this)).run); th.Name = "YAMI4 message dispatcher"; th.IsBackground = true; th.Start(); dispatchers.Add(th); } }
// Parameter incomingMessageDispatchCallback not used here internal UdpListener( Socket channel, string resolvedTarget, IncomingMessageDispatchCallback incomingMessageDispatchCallback, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) : base(channel, resolvedTarget, logCallback, logLevel) { this.channel = channel; this.options = options; }
internal static void configureTcpChannel(Socket channel, Options options) { channel.Blocking = false; channel.NoDelay = true; channel.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.KeepAlive, options.tcpKeepAlive ? 1 : 0 ); }
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 static TransportChannel connectTcp(string target, Options options) { IpComponents tcpComponents = parseTcp(target); Socket connection = CreateTCPSocket(); connection.Blocking = false; try { connection.Connect( tcpComponents.hostName, tcpComponents.port); } catch(SocketException ex) { // ignore if operaion in progress if(ex.SocketErrorCode != SocketError.WouldBlock && ex.SocketErrorCode != SocketError.AlreadyInProgress && ex.SocketErrorCode != SocketError.IsConnected) throw ex; } int timeout = options.tcpConnectTimeout; if(timeout == 0) // Sockets.Select needs -1 for infinite wait timeout = -1; if(!connection.Connected) { connection.Blocking = true; List<Socket> writeable = new List<Socket>(); writeable.Add(connection); List<Socket> error = new List<Socket>(); error.Add(connection); Socket.Select(null, writeable, error, timeout); if(writeable.Count == 0) throw new SocketException(WSAETIMEDOUT); connection.Blocking = false; } configureTcpChannel(connection, options); return new TransportChannel(connection); }
// 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 IOWorker( IDictionary<string, Channel> channels, IDictionary<string, Listener> listeners, WaterFlowManager incomingFlowManager, Options options, IncomingMessageDispatchCallback incomingMessageDispatchCallback, ConnectionEventCallback connectionEventCallback, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { this.channels = channels; this.listeners = listeners; this.incomingFlowManager = incomingFlowManager; this.options = options; this.incomingMessageDispatchCallback = incomingMessageDispatchCallback; this.connectionEventCallback = connectionEventCallback; this.logCallback = logCallback; this.logLevel = logLevel; listenersForSelection = new Dictionary<Socket, Listener>(); channelsForSelection = new Dictionary<Socket, Channel>(); stopRequest = false; }
//<parameter channel is not currently used internal static void configureUdpChannel(Socket channel, Options options) { channel.Blocking = false; }
private static UdpListener prepareUdpServer(string target, IncomingMessageDispatchCallback incomingMessageDispatchCallback, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { IpComponents udpComponents = parseUdp(target); string hostName = udpComponents.hostName; int port = udpComponents.port; Socket s = CreateUDPSocket(); IPEndPoint address; string boundHostName; if(hostName == "*") { // bind to the wildcard local address // and resolve to the local hostname address = new IPEndPoint(IPAddress.Any, port); boundHostName = Dns.GetHostName(); } else { //TODO: translation for empty result (if possible) address = new IPEndPoint( Dns.GetHostAddresses(hostName)[0], port); boundHostName = hostName; } s.Blocking = false; s.Bind(address); // get the actual address of this socket int boundPort = ((IPEndPoint)s.LocalEndPoint).Port; string resolvedTarget = formatUdpTarget(boundHostName, boundPort); return new UdpListener(s, resolvedTarget, incomingMessageDispatchCallback, options, logCallback, logLevel); }
internal static Listener prepareServer(string target, IncomingMessageDispatchCallback incomingMessageDispatchCallback, Options options, LogCallback logCallback, LogEventArgs.LogLevel logLevel) { if(protocolIsTcp(target)) { return prepareTcpServer(target, incomingMessageDispatchCallback, options, logCallback, logLevel); } else if(protocolIsUdp(target)) { return prepareUdpServer(target, incomingMessageDispatchCallback, options, logCallback, logLevel); } else { throw new BadProtocolException(target); } }
internal static int getPreferredFrameSize( Options options, string target) { if (protocolIsTcp(target)) { return options.tcpFrameSize; } if (protocolIsUdp(target)) { return options.udpFrameSize; } throw new UnexpectedValueException("unknown protocol"); }
internal static TransportChannel createUdp( string target, Options options) { IpComponents udpComponents = parseUdp(target); Socket ch = CreateUDPSocket(); configureUdpChannel(ch, options); // remember the target address, so that it can be used // for each sent message IPEndPoint address = new IPEndPoint( Dns.GetHostAddresses(udpComponents.hostName)[0], udpComponents.port); return new TransportChannel(ch, address); }