protected MonitoredSocketUser(String monitorAddress, ZSocketType socketType, TimeSpan pollWaitTime) { m_context = new ZContext(); m_socket = new ZSocket(m_context, socketType) { Linger = ZMQLinger, ReceiveHighWatermark = RCVHWM, SendHighWatermark = SNDHWM }; m_socket.Monitor(monitorAddress); m_monitor = ZMonitor.Create(m_context, monitorAddress); m_poller = ZPollItem.CreateReceiver(); m_monitor.AllEvents += (sender, e) => ScreenLogger.Log( ((Int32)e.Event.Event is 4096) ? $"Event fired on socket {e.Event.Address}: Handshake Success" : $"Event fired on socket {e.Event.Address}: {e.Event.Event}", LogObject.EventManager); m_cancelCondition = true; m_receiveBuffer = new BufferBlock <ZMessage>(); m_sendAction = new ActionBlock <ZMessage>((msg) => { m_socket.Send(msg); msg.Dispose(); }); m_pollWaitTime = MinPollWaitTime; if (pollWaitTime > m_pollWaitTime) { m_pollWaitTime = pollWaitTime; } ListenLoop(); }
protected ZeroMQBase(string addr, ZSocketType socketType, BindingType bindingType, string identity = "") { // Making socket of socketType _socket = new ZSocket(socketType); // Setting identity. Needed if not the server/router, since its needed to identify the receiver. // Identity needs to be set before connecting. if (identity != "") // Default name is Master Unit { _socket.IdentityString = identity; } // Either binding or connecting to given address if (bindingType == BindingType.Bind) { _socket.Bind(addr); // "tcp://*:5555" for server. "tcp://18.185.114.115:5555" for client } else if (bindingType == BindingType.Connect) { _socket.Connect(addr); } // Starting threads. _sender = new Thread(SenderMethod); _receiver = new Thread(ReceiverMethod); _sender.Start(); _receiver.Start(); }
public HwClient(string address, ZSocketType socketType, BindingType bindingType, string identity) : base(address, socketType, bindingType, identity) { //SetUp(); //ZSocketType.DEALER //"Master Unit" //"tcp://18.185.114.115:5555" }
protected virtual bool Initialize(ZSocketType frontendType, ZSocketType backendType, out ZError error) { error = default(ZError); /* if (frontendType == ZSocketType.None && backendType == ZSocketType.None) * { * throw new InvalidOperationException(); * } /**/ if (frontendType != ZSocketType.None) { if (null == (FrontendSocket = ZSocket.Create(Context, frontendType, out error))) { return(false); } FrontendSetup = new ZSocketSetup(FrontendSocket); } if (backendType != ZSocketType.None) { if (null == (BackendSocket = ZSocket.Create(Context, backendType, out error))) { return(false); } BackendSetup = new ZSocketSetup(BackendSocket); } return(true); }
private ZSocket CreateSocket(ZSocketType Type) { var s = new ZSocket(context, Type); s.SetOption(ZSocketOption.CONFLATE, 1); Sockets.Add(s); return(s); }
public void Close(ZSocketType socketType) { using (var context = new ZContext()) { using (var socket = new ZSocket(context, socketType)) { socket.Close(); } } }
public virtual async Task StartAsync(string endpoint, ZSocketType type, Action <ZFrame> receiveAction) { Context = Context ?? new ZContext(); Socket = Socket ?? new SendSocketServerAdapter(new ZSocket(Context, type)); Socket.Bind(endpoint); while (!_IsDisposed) { await OpenReceiveAsync(receiveAction); } }
public ZSocket CreateServerSocket(ZSocketType Type) { var s = CreateSocket(Type); s.CurveServer = true; s.CurvePublicKey = ServerPublicKey; s.CurveSecretKey = ServerPrivateKey; return(s); }
protected MonitoredDoubleSocketUser(String tcpMonitorAddress, String udpMonitorAddress, ZSocketType socketType, TimeSpan pollWaitTime) { m_context = new ZContext(); m_tcpSocket = new ZSocket(m_context, socketType) { Linger = ZMQLinger, ReceiveHighWatermark = RCVHWM, SendHighWatermark = SNDHWM }; m_tcpSocket.Monitor(tcpMonitorAddress); m_tcpSocketMonitor = ZMonitor.Create(m_context, tcpMonitorAddress); m_poller = ZPollItem.CreateReceiver(); m_tcpSocketMonitor.AllEvents += (sender, e) => ScreenLogger.Log( ((Int32)e.Event.Event is 4096) ? $"Event fired on main TCP socket: Handshake Success" : $"Event fired on main TCP socket: {e.Event.Event}", LogObject.EventManager); /* * m_udpSocket = new ZSocket(m_context, socketType) * { * Linger = ZMQLinger, * ReceiveHighWatermark = RCVHWM, * SendHighWatermark = SNDHWM * }; * m_udpSocket.Monitor(udpMonitorAddress); * m_udpSocketMonitor = ZMonitor.Create(m_context, udpMonitorAddress); * m_poller = ZPollItem.CreateReceiver(); * m_udpSocketMonitor.AllEvents += (sender, e) => * ScreenLogger.Log( * ((Int32)e.Event.Event is 4096) ? * $"Event fired on main UDP socket: Handshake Success" : * $"Event fired on main UDP socket: {e.Event.Event}", * LogObject.EventManager); */ m_cancelConddition = true; m_receiveBuffer = new BufferBlock <(TransportProtocol, ZMessage)>(); m_tcpSocketSendAction = new ActionBlock <ZMessage>((msg) => { m_tcpSocket.Send(msg); msg.Dispose(); }); //m_udpSocketSendAction = new ActionBlock<ZMessage>((msg) => { m_udpSocket.Send(msg); msg.Dispose(); }); m_pollWaitTime = MinPollWaitTime; if (pollWaitTime > m_pollWaitTime) { m_pollWaitTime = pollWaitTime; } ListenLoop(); }
public ZSocket CreateClientSocket(ZSocketType Type) { var s = CreateSocket(Type); Z85.CurveKeypair(out byte[] publicKey, out byte[] privateKey); s.CurvePublicKey = publicKey; s.CurveSecretKey = privateKey; s.CurveServerKey = ServerPublicKey; return(s); }
/// <summary> /// Initializes a new instance of the <see cref="ZDevice"/> class. /// </summary> /// <param name="frontendSocket"> /// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>. /// </param> /// <param name="backendSocket"> /// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>. /// </param> /// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param> protected ZDevice(ZContext context, ZSocketType frontendType, ZSocketType backendType) : base() { Context = context; ZError error; if (!Initialize(frontendType, backendType, out error)) { throw new ZException(error); } }
/// <summary> /// Initializes a new instance of the <see cref="ZDevice"/> class. /// </summary> /// <param name="frontendSocket"> /// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>. /// </param> /// <param name="backendSocket"> /// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>. /// </param> /// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param> protected ZDevice(ZContext context, ZSocketType frontendType, ZSocketType backendType) { Context = context; ZError error; if (!Initialize(frontendType, backendType, out error)) { throw new ZException(error); } }
public void Create_WithContext(ZSocketType socketType) { using (var context = new ZContext()) { using (var socket = new ZSocket(context, socketType)) { Assert.AreSame(context, socket.Context); Assert.IsNotNull(socket.SocketPtr); Assert.AreEqual(socketType, socket.SocketType); } } }
/// <summary> /// Create a <see cref="ZSocket"/> instance. /// </summary> /// <returns><see cref="ZSocket"/></returns> public ZSocket(ZContext context, ZSocketType socketType) { _context = context; _socketType = socketType; ZError error; if (!Initialize(out error)) { throw new ZException(error); } }
/// <summary> /// Create a <see cref="ZSocket"/> instance. /// </summary> /// <returns><see cref="ZSocket"/></returns> public static ZSocket Create(ZContext context, ZSocketType socketType, out ZError error) { var socket = new ZSocket(); socket._context = context; socket._socketType = socketType; if (!socket.Initialize(out error)) { return(default(ZSocket)); } return(socket); }
protected virtual bool Initialize(string endpoint, ZSocketType type, Action <ZSocket> modifySocketCallback) { // Disable ZMQ for non-windows platforms. if (Application.platform != RuntimePlatform.WindowsEditor && Application.platform != RuntimePlatform.WindowsPlayer) { Debug.LogWarning($"ZMQ Connection is not supported on this platform. {nameof(ZeroMQHandler)} will be disabled."); return(false); } Poller = ZPollItem.CreateReceiver(); Socket = new ZSocket(type); modifySocketCallback(Socket); return(true); }
public static void CreateZmq(ZSocketType socketType, int port, bool bind, out ZContext context, out ZSocket socket) { context = new ZContext(); socket = new ZSocket(context, socketType); ZError error; if (bind) { socket.Bind($"tcp://127.0.0.1:{port}", out error); } else { socket.Connect($"tcp://127.0.0.1:{port}", out error); } Assert.AreEqual(ZError.None, error); }
public SynapseEndpoint(String endpoint, String identity = null, ZContext context = null, ZSocketType socketType = ZSocketType.DEALER) { Endpoint = endpoint; Context = context; SocketType = socketType; if (Context == null) { Context = new ZContext(); } Socket = new ZSocket(Context, SocketType); Identity = identity; if (Identity == null) { Identity = Guid.NewGuid().ToString(); } Socket.Identity = Encoding.UTF8.GetBytes(Identity); }
public virtual async Task StartAsync(string endpoint, ZSocketType type, Action <ZFrame> receiveAction) { if (string.IsNullOrWhiteSpace(endpoint)) { if (string.IsNullOrWhiteSpace(Endpoint)) { await RegisterAsync(); } endpoint = Endpoint; } await Task.Run(() => { Context = Context ?? new ZContext(); Socket = Socket ?? new SendSocketServerAdapter(new ZSocket(Context, ZSocketType.PUB)); Logger?.WriteLine($"Starting {Name} on {endpoint}.", new VectorTimeStamp().Update(SystemRegistration.Id, DateTime.Now)); Socket.Bind(endpoint); }); }
public static void Server(ZContext context, String identity, string BrokerAddressforServer, ZSocketType socketType, string AddrForSub) { using (var server = new ZSocket(context, socketType)) { server.Connect(BrokerAddressforServer); server.Identity = Encoding.UTF8.GetBytes(identity); string text = "Confirmed Echo"; ZPollItem poll = ZPollItem.CreateReceiver(); ZError error; ZMessage request2; try { if (socketType == ZSocketType.REP) { using (ZMessage request = server.ReceiveMessage()) { string uid = request[0].ReadString(); string receiverIdentity = request[1].ReadString(); Console.WriteLine("{0} <- {1} : [{2} {3}]", identity, receiverIdentity, uid, "REQ"); Send(server, Encoding.UTF8.GetBytes(identity), text, uid); } } else if (socketType == ZSocketType.PULL) { while (true) { if (server.PollIn(poll, out request2, out error, TimeSpan.FromMilliseconds(1000))) { string uid = request2[1].ReadString(); string receivedIdentity = request2[2].ReadString(); Console.WriteLine("{0} <- {1} : [{2}]", identity, receivedIdentity, uid); } else { ErrorChecker(error); } } } else if (socketType == ZSocketType.SUB) { if (identity == "Server") { server.Subscribe("B"); } if (identity == "Server2") { server.Subscribe("Topic"); } if (identity == "Server3") { server.SubscribeAll(); } while (true) { using (ZMessage request = server.ReceiveMessage()) { string topic = request[0].ReadString(); string uid = request[1].ReadString(); string receivedIdentity = request[2].ReadString(); Console.WriteLine("{0} <- {1} : [{2} {3}]", identity, receivedIdentity, uid, topic); } } } } catch (ZException) { context = new ZContext(); } } }
public virtual void Connect(string endpoint, ZSocketType type) { Context = Context ?? new ZContext(); Socket = Socket ?? new RequestSocketAdapter(new ZSocket(Context, type), ThrowOnFailureToSend, RetryOnFailure); Socket.Connect(endpoint); }
static void Main(string[] args) { while (true) { var context = new ZContext(); Thread broker1 = new Thread(() => BrokerCreator.Broker(context, broker1AddrRequest, broker1AddrResponse, broker1AddrRequestPush, broker1AddrResponsePull, broker1AddrRequestPub, broker1AddrResponseSub, AddrToBroker2, AddrToBroker3, broker1AddrFromBrokers, brokerIdentity1)); Thread broker2 = new Thread(() => BrokerCreator.Broker(context, broker2AddrRequest, broker2AddrResponse, broker2AddrRequestPush, broker2AddrResponsePull, broker2AddrRequestPub, broker2AddrResponseSub, AddrToBroker1, AddrToBroker3, broker2AddrFromBrokers, brokerIdentity2)); Thread broker3 = new Thread(() => BrokerCreator.Broker(context, broker3AddrRequest, broker3AddrResponse, broker3AddrRequestPush, broker3AddrResponsePull, broker3AddrRequestPub, broker3AddrResponseSub, AddrToBroker1, AddrToBroker2, broker3AddrFromBrokers, brokerIdentity3)); ZSocketType socketTypeClient = ZSocketType.None; ZSocketType socketTypeServer = ZSocketType.None; string AddressforClient = ""; string AddressforServer = ""; ZSocketType socketTypeClient2 = ZSocketType.None; ZSocketType socketTypeServer2 = ZSocketType.None; string AddressforClient2 = ""; string AddressforServer2 = ""; ZSocketType socketTypeClient3 = ZSocketType.None; ZSocketType socketTypeServer3 = ZSocketType.None; string AddressforClient3 = ""; string AddressforServer3 = ""; Thread Client2 = null; Thread Server2 = null; Thread Client3 = null; Thread Server3 = null; Thread Client = null; Thread Server = null; string ClientIdentity = "Client"; string ServerIdentity = "Server"; string ClientIdentity2 = "Client2"; string ServerIdentity2 = "Server2"; string ClientIdentity3 = "Client3"; string ServerIdentity3 = "Server3"; string AddrForSub = ""; Random random = new Random(); Console.WriteLine("\nPress number to choose type of clients sockets."); Console.WriteLine("1 - REQ-REP"); Console.WriteLine("2 - PUSH-PULL"); Console.WriteLine("3 - PUB-SUB"); Console.WriteLine("4 - All sockets different"); Console.WriteLine("5 - All sockets REQ-REP"); Console.WriteLine("6 - All sockets PUSH-PULL"); Console.WriteLine("7 - All sockets PUB-SUB\n"); ConsoleKey key2 = Console.ReadKey(true).Key; if (key2 == ConsoleKey.D1) { socketTypeClient = ZSocketType.REQ; socketTypeServer = ZSocketType.REP; AddressforClient = AddressforClientReq[random.Next(0, AddressforClientReq.Length)]; AddressforServer = AddressforServerRep[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, "")); } else if (key2 == ConsoleKey.D2) { socketTypeClient = ZSocketType.PUSH; socketTypeServer = ZSocketType.PULL; AddressforClient = AddressforClientPush[random.Next(0, AddressforClientPush.Length)]; AddressforServer = AddressforServerPull[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, "")); } else if (key2 == ConsoleKey.D3) { int randIndex = random.Next(0, AddressforServerSub.Length); socketTypeClient = ZSocketType.PUB; socketTypeServer = ZSocketType.SUB; AddressforClient = AddressforClientPub[random.Next(0, AddressforClientPub.Length)]; AddressforServer = AddressforServerSub[randIndex]; AddrForSub = AddressforClientReq[randIndex]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity3, AddressforServer, socketTypeServer, AddrForSub)); } else if (key2 == ConsoleKey.D4) { socketTypeClient = ZSocketType.REQ; socketTypeServer = ZSocketType.REP; AddressforClient = AddressforClientReq[0]; AddressforServer = AddressforServerRep[1]; socketTypeClient2 = ZSocketType.PUSH; socketTypeServer2 = ZSocketType.PULL; AddressforClient2 = AddressforClientPush[1]; AddressforServer2 = AddressforServerPull[2]; socketTypeClient3 = ZSocketType.PUB; socketTypeServer3 = ZSocketType.SUB; AddressforClient3 = AddressforClientPub[2]; AddressforServer3 = AddressforServerSub[0]; AddrForSub = AddressforClientReq[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, "")); Client2 = new Thread(() => ClientCreator.Client(context, ClientIdentity2, AddressforClient2, socketTypeClient2, AddressforServer2)); Server2 = new Thread(() => ServerCreator.Server(context, ServerIdentity2, AddressforServer2, socketTypeServer2, "")); Client3 = new Thread(() => ClientCreator.Client(context, ClientIdentity3, AddressforClient3, socketTypeClient3, "")); Server3 = new Thread(() => ServerCreator.Server(context, ServerIdentity3, AddressforServer3, socketTypeServer3, AddrForSub)); } else if (key2 == ConsoleKey.D5) { socketTypeClient = ZSocketType.REQ; socketTypeServer = ZSocketType.REP; AddressforClient = AddressforClientReq[0]; AddressforServer = AddressforServerRep[1]; socketTypeClient2 = ZSocketType.REQ; socketTypeServer2 = ZSocketType.REP; AddressforClient2 = AddressforClientReq[1]; AddressforServer2 = AddressforServerRep[2]; socketTypeClient3 = ZSocketType.REQ; socketTypeServer3 = ZSocketType.REP; AddressforClient3 = AddressforClientReq[2]; AddressforServer3 = AddressforServerRep[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, "")); Client2 = new Thread(() => ClientCreator.Client(context, ClientIdentity2, AddressforClient2, socketTypeClient2, AddressforServer2)); Server2 = new Thread(() => ServerCreator.Server(context, ServerIdentity2, AddressforServer2, socketTypeServer2, "")); Client3 = new Thread(() => ClientCreator.Client(context, ClientIdentity3, AddressforClient3, socketTypeClient3, AddressforServer3)); Server3 = new Thread(() => ServerCreator.Server(context, ServerIdentity3, AddressforServer3, socketTypeServer3, "")); } else if (key2 == ConsoleKey.D6) { socketTypeClient = ZSocketType.PUSH; socketTypeServer = ZSocketType.PULL; AddressforClient = AddressforClientPush[0]; AddressforServer = AddressforServerPull[1]; socketTypeClient2 = ZSocketType.PUSH; socketTypeServer2 = ZSocketType.PULL; AddressforClient2 = AddressforClientPush[1]; AddressforServer2 = AddressforServerPull[2]; socketTypeClient3 = ZSocketType.PUSH; socketTypeServer3 = ZSocketType.PULL; AddressforClient3 = AddressforClientPush[2]; AddressforServer3 = AddressforServerPull[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, "")); Client2 = new Thread(() => ClientCreator.Client(context, ClientIdentity2, AddressforClient2, socketTypeClient2, AddressforServer2)); Server2 = new Thread(() => ServerCreator.Server(context, ServerIdentity2, AddressforServer2, socketTypeServer2, "")); Client3 = new Thread(() => ClientCreator.Client(context, ClientIdentity3, AddressforClient3, socketTypeClient3, AddressforServer3)); Server3 = new Thread(() => ServerCreator.Server(context, ServerIdentity3, AddressforServer3, socketTypeServer3, "")); } else if (key2 == ConsoleKey.D7) { socketTypeClient = ZSocketType.PUB; socketTypeServer = ZSocketType.SUB; AddressforClient = AddressforClientPub[0]; AddressforServer = AddressforServerSub[1]; socketTypeClient2 = ZSocketType.PUB; socketTypeServer2 = ZSocketType.SUB; AddressforClient2 = AddressforClientPub[1]; AddressforServer2 = AddressforServerSub[2]; socketTypeClient3 = ZSocketType.PUB; socketTypeServer3 = ZSocketType.SUB; AddressforClient3 = AddressforClientPub[2]; AddressforServer3 = AddressforServerSub[0]; AddrForSub = AddressforClientReq[1]; string AddrForSub2 = AddressforClientReq[2]; string AddrForSub3 = AddressforClientReq[0]; Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, "")); Server = new Thread(() => ServerCreator.Server(context, ServerIdentity, AddressforServer, socketTypeServer, AddrForSub)); Client2 = new Thread(() => ClientCreator.Client(context, ClientIdentity2, AddressforClient2, socketTypeClient2, "")); Server2 = new Thread(() => ServerCreator.Server(context, ServerIdentity2, AddressforServer2, socketTypeServer2, AddrForSub2)); Client3 = new Thread(() => ClientCreator.Client(context, ClientIdentity3, AddressforClient3, socketTypeClient3, "")); Server3 = new Thread(() => ServerCreator.Server(context, ServerIdentity3, AddressforServer3, socketTypeServer3, AddrForSub3)); } Console.WriteLine("Press number to choose test. Press S to stop."); Console.WriteLine("1 - all components active"); if (key2 == ConsoleKey.D1 || key2 == ConsoleKey.D2 || key2 == ConsoleKey.D3) { Console.WriteLine("2 - passive sending client after sending and active receiving client after 5 sec\n"); } else { Console.WriteLine("\n"); } ConsoleKey key = Console.ReadKey(true).Key; broker1.Start(); broker2.Start(); broker3.Start(); if (key == ConsoleKey.D1) { Server.Start(); Client.Start(); if (AddressforClient2 != "") { Server2.Start(); Server3.Start(); Client2.Start(); Client3.Start(); } } else if (key == ConsoleKey.D2) { Client.Start(); Thread.Sleep(10 * 1000); Client.Abort(); Thread.Sleep(5 * 1000); Server.Start(); Thread.Sleep(5 * 1000); Client = new Thread(() => ClientCreator.Client(context, ClientIdentity, AddressforClient, socketTypeClient, AddressforServer)); Client.Start(); } while (Console.ReadKey(true).Key != ConsoleKey.S) { Thread.Sleep(1); } Client.Abort(); Server.Abort(); if (Client2 != null) { Client2.Abort(); Server2.Abort(); Client3.Abort(); Server3.Abort(); } broker1.Abort(); broker2.Abort(); broker3.Abort(); } }
/// <summary> /// Initializes a new instance of the <see cref="ZDevice"/> class. /// </summary> /// <param name="frontendSocket"> /// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>. /// </param> /// <param name="backendSocket"> /// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>. /// </param> /// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param> protected ZDevice(ZSocketType frontendType, ZSocketType backendType) : this(ZContext.Current, frontendType, backendType) { }
protected virtual bool Initialize(ZSocketType frontendType, ZSocketType backendType, out ZError error) { error = default(ZError); /* if (frontendType == ZSocketType.None && backendType == ZSocketType.None) { throw new InvalidOperationException(); } /**/ if (frontendType != ZSocketType.None) { if (null == (FrontendSocket = ZSocket.Create(Context, frontendType, out error))) { return false; } FrontendSetup = new ZSocketSetup(FrontendSocket); } if (backendType != ZSocketType.None) { if (null == (BackendSocket = ZSocket.Create(Context, backendType, out error))) { return false; } BackendSetup = new ZSocketSetup(BackendSocket); } return true; }
private static void DoWithUnconnectedSocket(Action <ZContext, ZSocket> action, ZSocketType socketType) { using (var context = new ZContext()) { using (var socket = new ZSocket(context, socketType)) { action(context, socket); } } }
public HwServer(string address, ZSocketType socketType, BindingType bindingType) : base(address, socketType, bindingType) { SetUp(); }
public static void Client(ZContext context, string identity, string ProxyAddressforClient, ZSocketType socketType, string receiverAddress) { var client = CreateZSocket(context, identity, ProxyAddressforClient, socketType); string text = "Echo"; ZError error; ZMessage request; ZPollItem poll = ZPollItem.CreateReceiver(); int counter = 0; try { if (socketType == ZSocketType.REQ) { Send(client, client.Identity, text, receiverAddress); while (true) { if (client.PollIn(poll, out request, out error, TimeSpan.FromMilliseconds(20 * 1000))) { string requestID = request[0].ReadString(); string receivedIdentity = request[1].ReadString(); Console.WriteLine("{0} <- {1} : [{2} {3}]", identity, receivedIdentity, requestID, "REP"); break; } else { if (error == ZError.EAGAIN) { client.Dispose(); client = CreateZSocket(context, identity, ProxyAddressforClient, socketType); Send(client, client.Identity, text, receiverAddress); continue; } if (error == ZError.ETERM) { return; // Interrupted } throw new ZException(error); } } } else if (socketType == ZSocketType.PUSH) { while (counter < 3) { bool sended = Send(client, client.Identity, text, receiverAddress); if (sended == false) { counter--; } Thread.Sleep(3 * 1000); counter++; } } else if (socketType == ZSocketType.PUB) { string topic = null; if (identity == "Client") { topic = "Topic"; } if (identity == "Client2") { topic = "A"; } if (identity == "Client3") { topic = "B"; } while (counter < 3) { Thread.Sleep(3 * 1000); bool sended = Send(client, client.Identity, text, topic); if (sended == false) { counter--; } counter++; } } } finally { if (client != null) { client.Dispose(); } } }
/// <summary> /// Initializes a new instance of the <see cref="ZDevice"/> class. /// You are using ZContext.Current! /// </summary> /// <param name="frontendSocket"> /// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>. /// </param> /// <param name="backendSocket"> /// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>. /// </param> /// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param> protected ZDevice(ZSocketType frontendType, ZSocketType backendType) : this(ZContext.Current, frontendType, backendType) { }
static ZSocket CreateZSocket(ZContext context, string identity, string address, ZSocketType socketType) { var client = new ZSocket(context, socketType); client.Identity = Encoding.UTF8.GetBytes(identity); //client.Linger = TimeSpan.FromMilliseconds(0); client.Connect(address); return(client); }
/// <summary> /// Create a <see cref="ZSocket"/> instance. /// You are using ZContext.Current! /// </summary> /// <returns><see cref="ZSocket"/></returns> public ZSocket(ZSocketType socketType) : this(ZContext.Current, socketType) { }
async Task IServer <ZFrame> .StartAsync(string endpoint, ZSocketType type, Action <ZFrame> receiveAction) { await StartAsync(endpoint, receiveAction); }
/// <summary> /// Create a <see cref="ZSocket"/> instance. /// </summary> /// <returns><see cref="ZSocket"/></returns> public static ZSocket Create(ZContext context, ZSocketType socketType) { return(new ZSocket(context, socketType)); }