public void Write( SocketBase s) { int size = 4 + 1 + m_addr.Length + 1; // event + len(addr) + addr + flag if (m_flag == ValueInteger) size += 4; else if (m_flag == ValueChannel) size += s_sizeOfIntPtr; int pos = 0; ByteArraySegment buffer = new byte[size]; buffer.PutInteger(Endianness.Little, (int)m_monitorEvent, pos); pos += 4; buffer[pos++] = (byte)m_addr.Length; // was not here originally buffer.PutString(m_addr, pos); pos += m_addr.Length; buffer[pos++] = ((byte)m_flag); if (m_flag == ValueInteger) { buffer.PutInteger(Endianness.Little, (int)m_arg, pos); } else if (m_flag == ValueChannel) { GCHandle handle = GCHandle.Alloc(m_arg, GCHandleType.Weak); if (s_sizeOfIntPtr == 4) buffer.PutInteger(Endianness.Little, GCHandle.ToIntPtr(handle).ToInt32(), pos); else buffer.PutLong(Endianness.Little, GCHandle.ToIntPtr(handle).ToInt64(), pos); } var msg = new Msg(); msg.InitGC((byte[])buffer, buffer.Size); s.TrySend(ref msg, SendReceiveConstants.InfiniteTimeout, false); }
/// <summary> /// Un-register the given address/socket, by removing it from the contained list of endpoints. /// </summary> /// <param name="address">the (string) address denoting the endpoint to unregister</param> /// <param name="socket">the socket associated with that endpoint</param> /// <returns>true if the endpoint having this address and socket is found, false otherwise</returns> public bool UnregisterEndpoint( string address, SocketBase socket) { lock (m_endpointsSync) { Endpoint endpoint; if (!m_endpoints.TryGetValue(address, out endpoint)) return false; if (socket != endpoint.Socket) return false; m_endpoints.Remove(address); return true; } }
/// <summary> /// Destroy the given socket - which means to remove it from the list of active sockets, /// and add it to the list of unused sockets to be terminated. /// </summary> /// <param name="socket">the socket to destroy</param> /// <remarks> /// If this was the last socket, then stop the reaper. /// </remarks> public void DestroySocket( SocketBase socket) { // Free the associated thread slot. lock (m_slotSync) { int threadId = socket.ThreadId; m_emptySlots.Push(threadId); m_slots[threadId].Close(); m_slots[threadId] = null; // Remove the socket from the list of sockets. m_sockets.Remove(socket); // If zmq_term() was already called and there are no more socket // we can ask reaper thread to terminate. if (m_terminating && m_sockets.Count == 0) m_reaper.Stop(); } //LOG.debug("Released Slot [" + socket_ + "] "); }
protected void DestroySocket( SocketBase socket) { m_ctx.DestroySocket(socket); }
/// <summary> /// Create a new SubscriberSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal SubscriberSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Add the given socket to the list to be reaped (terminated). /// </summary> /// <param name="socket">the socket to add to the list for termination</param> protected override void ProcessReap(SocketBase socket) { // Add the socket to the poller. socket.StartReaping(m_poller); ++m_sockets; }
/// <summary> /// Register the given events to monitor on the given endpoint. /// </summary> /// <param name="addr">a string denoting the endpoint to monitor. If this is null - monitoring is stopped.</param> /// <param name="events">the SocketEvent to monitor for</param> /// <exception cref="NetMQException">Maximum number of sockets reached.</exception> /// <exception cref="ProtocolNotSupportedException">The protocol of <paramref name="addr"/> is not supported.</exception> /// <exception cref="TerminatingException">The socket has been stopped.</exception> public void Monitor([CanBeNull] string addr, SocketEvents events) { CheckContextTerminated(); // Support de-registering monitoring endpoints as well if (addr == null) { StopMonitor(); return; } string address; string protocol; DecodeAddress(addr, out address, out protocol); CheckProtocol(protocol); // Event notification only supported over inproc:// if (protocol != Address.InProcProtocol) throw new ProtocolNotSupportedException($"In SocketBase.Monitor({addr},), protocol must be inproc"); // Register events to monitor m_monitorEvents = events; m_monitorSocket = Ctx.CreateSocket(ZmqSocketType.Pair); // Never block context termination on pending event messages const int linger = 0; try { m_monitorSocket.SetSocketOption(ZmqSocketOption.Linger, linger); } catch (NetMQException) { StopMonitor(); throw; } // Spawn the monitor socket endpoint try { m_monitorSocket.Bind(addr); } catch (NetMQException) { StopMonitor(); throw; } }
/// <summary> /// Create a new RequestSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal RequestSocket(SocketBase socketHandle) : base(socketHandle) { }
protected void UnregisterEndpoints( SocketBase socket) { m_ctx.UnregisterEndpoints(socket); }
public static MonitorEvent Read( SocketBase s) { var msg = new Msg(); msg.InitEmpty(); s.TryRecv(ref msg, SendReceiveConstants.InfiniteTimeout); int pos = msg.Offset; ByteArraySegment data = msg.Data; var @event = (SocketEvents)data.GetInteger(Endianness.Little, pos); pos += 4; var len = (int)data[pos++]; string addr = data.GetString(len, pos); pos += len; var flag = (int)data[pos++]; object arg = null; if (flag == ValueInteger) { arg = data.GetInteger(Endianness.Little, pos); } else if (flag == ValueChannel) { IntPtr value = s_sizeOfIntPtr == 4 ? new IntPtr(data.GetInteger(Endianness.Little, pos)) : new IntPtr(data.GetLong(Endianness.Little, pos)); GCHandle handle = GCHandle.FromIntPtr(value); AsyncSocket socket = null; if (handle.IsAllocated) { socket = handle.Target as AsyncSocket; } handle.Free(); arg = socket; } return new MonitorEvent(@event, addr, arg); }
protected bool UnregisterEndpoint( string addr, SocketBase socket) { return m_ctx.UnregisterEndpoint(addr, socket); }
protected virtual void ProcessReap( SocketBase socket) { // Overriden by Reaper throw new NotSupportedException(); }
protected void SendReap( SocketBase socket) { SendCommand(new Command(m_ctx.GetReaper(), CommandType.Reap, socket)); }
/// <summary> /// Remove from the list of endpoints, all endpoints that reference the given socket. /// </summary> /// <param name="socket">the socket to remove all references to</param> public void UnregisterEndpoints( SocketBase socket) { lock (m_endpointsSync) { IList<string> removeList = m_endpoints.Where(e => e.Value.Socket == socket).Select(e => e.Key).ToList(); foreach (var item in removeList) m_endpoints.Remove(item); } }
/// <summary> /// Create a new RouterSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal RouterSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a new Endpoint with the given socket. /// </summary> /// <param name="socket">the socket for this new Endpoint</param> /// <param name="options">the Options to assign to this new Endpoint</param> public Endpoint( SocketBase socket, Options options) { Socket = socket; Options = options; }
/// <summary> /// Create a new DealerSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal DealerSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a new ResponseSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal ResponseSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a new StreamSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal StreamSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// If there is a monitor-socket, close it and set monitor-events to 0. /// </summary> private void StopMonitor() { if (m_monitorSocket != null) { m_monitorSocket.Close(); m_monitorSocket = null; m_monitorEvents = 0; } }
/// <summary> /// Create a new SessionBase object from the given IOThread, socket, and Address. /// </summary> /// <param name="ioThread">the IOThread for this session to run on</param> /// <param name="connect">this flag dictates whether to connect</param> /// <param name="socket">the socket to contain</param> /// <param name="options">Options that dictate the settings of this session</param> /// <param name="addr">an Address that dictates the protocol and IP-address to use when connecting</param> public SessionBase([NotNull] IOThread ioThread, bool connect, [NotNull] SocketBase socket, [NotNull] Options options, [NotNull] Address addr) : base(ioThread, options) { m_ioObject = new IOObject(ioThread); m_connect = connect; m_socket = socket; m_ioThread = ioThread; m_addr = addr; if (options.RawSocket) { m_identitySent = true; m_identityReceived = true; } m_terminatingPipes = new HashSet<Pipe>(); }
/// <summary> /// Create a new PairSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal PairSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a new PushSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal PushSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a new PublisherSocket based upon the given SocketBase. /// </summary> /// <param name="socketHandle">the SocketBase to create the new socket from</param> internal PublisherSocket(SocketBase socketHandle) : base(socketHandle) { }
/// <summary> /// Create a return a new session. /// The specific subclass of SessionBase that is created is dictated by the SocketType specified by the options argument. /// </summary> /// <param name="ioThread">the <c>IOThread</c> for this session to run in</param> /// <param name="connect">whether to immediately connect</param> /// <param name="socket">the socket to connect</param> /// <param name="options">an <c>Options</c> that provides the SocketType that dictates which type of session to create</param> /// <param name="addr">an <c>Address</c> object that specifies the protocol and address to connect to</param> /// <returns>the newly-created instance of whichever subclass of SessionBase is specified by the options</returns> /// <exception cref="InvalidException">The socket must be of the correct type.</exception> public static SessionBase Create( IOThread ioThread, bool connect, SocketBase socket, Options options, Address addr) { switch (options.SocketType) { case ZmqSocketType.Req: return new Req.ReqSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Dealer: return new Dealer.DealerSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Rep: return new Rep.RepSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Router: return new Router.RouterSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Pub: return new Pub.PubSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Xpub: return new XPub.XPubSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Sub: return new Sub.SubSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Xsub: return new XSub.XSubSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Push: return new Push.PushSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Pull: return new Pull.PullSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Pair: return new Pair.PairSession(ioThread, connect, socket, options, addr); case ZmqSocketType.Stream: return new Stream.StreamSession(ioThread, connect, socket, options, addr); default: throw new InvalidException("SessionBase.Create called with invalid SocketType of " + options.SocketType); } }