本文整理汇总了C#中NetMQ.Sockets.DealerSocket.SendFrame方法的典型用法代码示例。如果您正苦于以下问题:C# DealerSocket.SendFrame方法的具体用法?C# DealerSocket.SendFrame怎么用?C# DealerSocket.SendFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetMQ.Sockets.DealerSocket
的用法示例。
在下文中一共展示了DealerSocket.SendFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindRandom
public void BindRandom()
{
using (var randomDealer = new DealerSocket())
using (var connectingDealer = new DealerSocket())
{
int port = randomDealer.BindRandomPort("tcp://*");
connectingDealer.Connect("tcp://127.0.0.1:" + port);
randomDealer.SendFrame("test");
Assert.AreEqual("test", connectingDealer.ReceiveFrameString());
}
}
示例2: ReceiveMessage
public void ReceiveMessage()
{
using (var server = new DealerSocket("@tcp://127.0.0.1:5555"))
using (var client = new DealerSocket(">tcp://127.0.0.1:5555"))
using (var manualResetEvent = new ManualResetEvent(false))
using (new NetMQProactor(client, (socket, message) =>
{
manualResetEvent.Set();
Assert.AreSame(client, socket);
}))
{
server.SendFrame("Hello");
Assert.IsTrue(manualResetEvent.WaitOne(100));
}
}
示例3: TwoMessagesFromRouterToDealer
public void TwoMessagesFromRouterToDealer()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
using (var poller = new NetMQPoller { client })
{
var port = server.BindRandomPort("tcp://*");
client.Connect("tcp://127.0.0.1:" + port);
var cnt = 0;
client.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine(str);
}
cnt++;
if (cnt == 2)
{
poller.Stop();
}
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
client.Options.Identity = clientId;
const string request = "GET /\r\n";
const string response = "HTTP/1.0 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Hello, World!";
client.SendFrame(request);
byte[] serverId = server.ReceiveFrameBytes();
Assert.AreEqual(request, server.ReceiveFrameString());
// two messages in a row, not frames
server.SendMoreFrame(serverId).SendFrame(response);
server.SendMoreFrame(serverId).SendFrame(response);
poller.Run();
}
}
示例4: BindToLocal
public void BindToLocal()
{
var validAliasesForLocalHost = new[] { "127.0.0.1", "localhost", Dns.GetHostName() };
foreach (var alias in validAliasesForLocalHost)
{
using (var localDealer = new DealerSocket())
using (var connectingDealer = new DealerSocket())
{
var port = localDealer.BindRandomPort("tcp://*");
connectingDealer.Connect(string.Format("tcp://{0}:{1}", alias, port));
localDealer.SendFrame("test");
Assert.AreEqual("test", connectingDealer.ReceiveFrameString());
Console.WriteLine(alias + " connected ");
}
}
}
示例5: NoBlock
public void NoBlock()
{
const int count = 1000;
NetMQConfig.Linger = TimeSpan.FromSeconds(0.5);
using (var client = new DealerSocket(">tcp://localhost:5557"))
{
// Sending a lot of messages
client.Options.SendHighWatermark = count;
for (int i = 0; i < count; i++)
{
client.SendFrame("Hello");
}
}
Stopwatch stopwatch = Stopwatch.StartNew();
NetMQConfig.Cleanup(false);
stopwatch.Stop();
Assert.Less(stopwatch.ElapsedMilliseconds, 500);
}
示例6: Mandatory
public void Mandatory()
{
using (var router = new RouterSocket())
{
router.Options.RouterMandatory = true;
router.Bind("tcp://127.0.0.1:5555");
using (var dealer = new DealerSocket())
{
dealer.Options.Identity = Encoding.ASCII.GetBytes("1");
dealer.Connect("tcp://127.0.0.1:5555");
dealer.SendFrame("Hello");
Assert.AreEqual("1", router.ReceiveFrameString());
Assert.AreEqual("Hello", router.ReceiveFrameString());
}
Thread.Sleep(100);
Assert.Throws<HostUnreachableException>(() => router.SendMoreFrame("1").SendFrame("Hello"));
}
}
示例7: AddSocketDuringWork
public void AddSocketDuringWork()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var poller = new NetMQPoller { router1 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
bool router1Arrived = false;
bool router2Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1.SkipFrame();
router1.SkipFrame();
router1Arrived = true;
poller.Add(router2);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2.SkipFrame();
router2.SkipFrame();
router2Arrived = true;
signal2.Set();
};
poller.RunAsync();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal2.WaitOne(300));
poller.Stop();
Assert.IsTrue(router1Arrived);
Assert.IsTrue(router2Arrived);
}
}
示例8: Ipv6ToIpv4
public void Ipv6ToIpv4()
{
using (var localDealer = new DealerSocket())
using (NetMQSocket connectingDealer = new DealerSocket())
{
localDealer.Options.IPv4Only = false;
var port = localDealer.BindRandomPort("tcp://*");
connectingDealer.Connect($"tcp://{IPAddress.Loopback}:{port}");
connectingDealer.SendFrame("test");
Assert.AreEqual("test", localDealer.ReceiveFrameString());
}
}
示例9: Unbind
public void Unbind(string protocol)
{
using (var server = new DealerSocket())
{
string address1, address2;
// just making sure can bind on both addresses
using (var client1 = new DealerSocket())
using (var client2 = new DealerSocket())
{
if (protocol == "tcp")
{
var port1 = server.BindRandomPort("tcp://localhost");
var port2 = server.BindRandomPort("tcp://localhost");
address1 = "tcp://localhost:" + port1;
address2 = "tcp://localhost:" + port2;
client1.Connect(address1);
client2.Connect(address2);
}
else
{
Debug.Assert(protocol == "inproc");
address1 = "inproc://localhost1";
address2 = "inproc://localhost2";
server.Bind(address1);
server.Bind(address2);
client1.Connect(address1);
client2.Connect(address2);
}
Thread.Sleep(100);
// we should be connected to both server
client1.SendFrame("1");
client2.SendFrame("2");
// the server receive from both
server.SkipFrame();
server.SkipFrame();
}
// unbind second address
server.Unbind(address2);
Thread.Sleep(100);
using (var client1 = new DealerSocket())
using (var client2 = new DealerSocket())
{
client1.Options.DelayAttachOnConnect = true;
client1.Connect(address1);
client2.Options.DelayAttachOnConnect = true;
if (protocol == "tcp")
{
client2.Connect(address2);
client1.SendFrame("1");
server.SkipFrame();
Assert.IsFalse(client2.TrySendFrame(TimeSpan.FromSeconds(2), "2"));
}
else
{
Assert.Throws<EndpointNotFoundException>(() => { client2.Connect(address2); });
}
}
}
}
示例10: HasInTest
public void HasInTest()
{
using (var server = new RouterSocket())
using (var client = new DealerSocket())
{
var port = server.BindRandomPort("tcp://*");
// no one sent a message so it should be false
Assert.IsFalse(server.HasIn);
client.Connect("tcp://localhost:" + port);
// wait for the client to connect
Thread.Sleep(100);
// now we have one client connected but didn't send a message yet
Assert.IsFalse(server.HasIn);
client.SendFrame("1");
// wait for the message to arrive
Thread.Sleep(100);
// the has in should indicate a message is ready
Assert.IsTrue(server.HasIn);
server.SkipFrame(); // identity
string message = server.ReceiveFrameString();
Assert.AreEqual(message, "1");
// we read the message, it should false again
Assert.IsFalse(server.HasIn);
}
}
示例11: AddTwoSocketAfterRemoving
public void AddTwoSocketAfterRemoving()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var router3 = new RouterSocket())
using (var router4 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var dealer3 = new DealerSocket())
using (var dealer4 = new DealerSocket())
using (var poller = new NetMQPoller { router1, router2 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
int port3 = router3.BindRandomPort("tcp://127.0.0.1");
int port4 = router4.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
dealer3.Connect("tcp://127.0.0.1:" + port3);
dealer4.Connect("tcp://127.0.0.1:" + port4);
int router1Arrived = 0;
int router2Arrived = 0;
bool router3Arrived = false;
bool router4Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
var signal3 = new ManualResetEvent(false);
var signal4 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1Arrived++;
router1.SkipFrame(); // identity
router1.SkipFrame(); // message
poller.Remove(router1);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2Arrived++;
router2.SkipFrame(); // identity
router2.SkipFrame(); // message
if (router2Arrived == 1)
{
poller.Add(router3);
poller.Add(router4);
signal2.Set();
}
};
router3.ReceiveReady += (s, e) =>
{
router3.SkipFrame(); // identity
router3.SkipFrame(); // message
router3Arrived = true;
signal3.Set();
};
router4.ReceiveReady += (s, e) =>
{
router4.SkipFrame(); // identity
router4.SkipFrame(); // message
router4Arrived = true;
signal4.Set();
};
poller.RunAsync();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal2.WaitOne(300));
dealer3.SendFrame("3");
dealer4.SendFrame("4");
dealer2.SendFrame("2");
dealer1.SendFrame("1");
Assert.IsTrue(signal3.WaitOne(300));
Assert.IsTrue(signal4.WaitOne(300));
poller.Stop();
router1.SkipFrame();
bool more;
Assert.AreEqual("1", router1.ReceiveFrameString(out more));
Assert.IsFalse(more);
Assert.AreEqual(1, router1Arrived);
Assert.AreEqual(2, router2Arrived);
Assert.IsTrue(router3Arrived);
Assert.IsTrue(router4Arrived);
}
}
示例12: AddSocketDuringWork
public void AddSocketDuringWork()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var poller = new Poller(router1) { PollTimeout = TestPollTimeoutMillis })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
bool router1Arrived = false;
bool router2Arrived = false;
var signal1 = new ManualResetEvent(false);
var signal2 = new ManualResetEvent(false);
router1.ReceiveReady += (s, e) =>
{
router1.SkipFrame();
router1.SkipFrame();
router1Arrived = true;
poller.AddSocket(router2);
signal1.Set();
};
router2.ReceiveReady += (s, e) =>
{
router2.SkipFrame();
router2.SkipFrame();
router2Arrived = true;
signal2.Set();
};
poller.PollTillCancelledNonBlocking();
dealer1.SendFrame("1");
Assert.IsTrue(signal1.WaitOne(300));
dealer2.SendFrame("2");
Assert.IsTrue(signal1.WaitOne(300));
poller.CancelAndJoin();
Assert.IsTrue(router1Arrived);
Assert.IsTrue(router2Arrived);
}
}
示例13: Messages_From_Router_To_Dealer_With_Subscription
public void Messages_From_Router_To_Dealer_With_Subscription()
{
Console.Write("Test sending message from publisher(router) to subscribers (dealer).\n");
int maxMessage = 5;
CountdownEvent cd = new CountdownEvent(maxMessage);
string endpoint;
using (var publisher = new RouterSocket())
using (var subscriber = new DealerSocket())
using (var poller = new NetMQPoller { subscriber })
{
publisher.Bind("tcp://127.0.0.1:0");
endpoint = publisher.Options.LastEndpoint;
subscriber.Connect(endpoint);
subscriber.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine("Subscribe: " + str);
}
cd.Signal();
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientIdTheIsLongerThen32BytesForSureAbsolutelySure");
subscriber.Options.Identity = clientId;
const string request = "Ping";
// Work around "feature" of router/dealer: the publisher does not know the subscriber exists, until it
// sends at least one message which makes it necessary to open the connection. I believe this is a
// low-level feature of the TCP/IP transport.
subscriber.SendFrame(request); // Ping.
byte[] serverId = publisher.ReceiveFrameBytes();
//Assert.AreEqual(request, publisher.ReceiveFrameString());
for (int i = 0; i < maxMessage; i++)
{
string msg = string.Format("[message: {0}]", i);
Console.Write("Publish: {0}\n", msg);
publisher.SendMoreFrame(serverId).SendFrame(msg);
//publisher.SendMoreFrame("").SendFrame(msg);
}
poller.RunAsync();
if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
{
Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
}
}
}
示例14: Messages_From_Dealer_To_Router
public void Messages_From_Dealer_To_Router()
{
int maxMessage = 5;
CountdownEvent cd = new CountdownEvent(maxMessage);
Console.Write("Test sending message from subscribers (dealer) to publisher(router).\n");
using (var publisher = new RouterSocket())
using (var subscriber = new DealerSocket())
using (var poller = new NetMQPoller { subscriber })
{
var port = publisher.BindRandomPort("tcp://*");
subscriber.Connect("tcp://127.0.0.1:" + port);
subscriber.ReceiveReady += (sender, e) =>
{
var strs = e.Socket.ReceiveMultipartStrings();
foreach (var str in strs)
{
Console.WriteLine(str);
}
cd.Signal();
};
byte[] clientId = Encoding.Unicode.GetBytes("ClientId");
subscriber.Options.Identity = clientId;
const string request = "GET /\r\n";
const string response = "HTTP/1.0 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Hello, World!";
subscriber.SendFrame(request);
byte[] serverId = publisher.ReceiveFrameBytes();
Assert.AreEqual(request, publisher.ReceiveFrameString());
for (int i = 0; i < maxMessage; i++)
{
publisher.SendMoreFrame(serverId).SendFrame(response);
}
poller.RunAsync();
if (cd.Wait(TimeSpan.FromSeconds(10)) == false) // Blocks until _countdown.Signal has been called.
{
Assert.Fail("Timed out, this test should complete in less than 10 seconds.");
}
}
}
示例15: RemoveSocket
public void RemoveSocket()
{
using (var router1 = new RouterSocket())
using (var router2 = new RouterSocket())
using (var router3 = new RouterSocket())
using (var dealer1 = new DealerSocket())
using (var dealer2 = new DealerSocket())
using (var dealer3 = new DealerSocket())
using (var poller = new NetMQPoller { router1, router2, router3 })
{
int port1 = router1.BindRandomPort("tcp://127.0.0.1");
int port2 = router2.BindRandomPort("tcp://127.0.0.1");
int port3 = router3.BindRandomPort("tcp://127.0.0.1");
dealer1.Connect("tcp://127.0.0.1:" + port1);
dealer2.Connect("tcp://127.0.0.1:" + port2);
dealer3.Connect("tcp://127.0.0.1:" + port3);
bool first = true;
router1.ReceiveReady += (s, e) =>
{
if (!first)
Assert.Fail("This should not happen because we cancelled the socket");
first = false;
// identity
e.Socket.SkipFrame();
bool more;
Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more));
Assert.False(more);
// cancelling the socket
poller.Remove(e.Socket); // remove self
};
router2.ReceiveReady += (s, e) =>
{
// identity
byte[] identity = e.Socket.ReceiveFrameBytes();
// message
e.Socket.SkipFrame();
e.Socket.SendMoreFrame(identity);
e.Socket.SendFrame("2");
};
router3.ReceiveReady += (s, e) =>
{
// identity
byte[] identity = e.Socket.ReceiveFrameBytes();
// message
e.Socket.SkipFrame();
e.Socket.SendMoreFrame(identity).SendFrame("3");
};
Task pollerTask = Task.Factory.StartNew(poller.Run);
// Send three messages. Only the first will be processed, as then handler removes
// the socket from the poller.
dealer1.SendFrame("Hello");
dealer1.SendFrame("Hello2");
dealer1.SendFrame("Hello3");
// making sure the socket defined before the one cancelled still works
dealer2.SendFrame("1");
Assert.AreEqual("2", dealer2.ReceiveFrameString());
// making sure the socket defined after the one cancelled still works
dealer3.SendFrame("1");
Assert.AreEqual("3", dealer3.ReceiveFrameString());
poller.Stop();
Assert.IsTrue(pollerTask.IsCompleted);
}
}