// csharp.map.receiver example // // Send an amqp/map message to amqp:tcp:localhost:5672 amq.direct/map_example // The map message // static int Main(string[] args) { string url = "amqp:tcp:localhost:5672"; string address = "message_queue; {create: always}"; string connectionOptions = ""; if (args.Length > 0) { url = args[0]; } if (args.Length > 1) { address = args[1]; } if (args.Length > 2) { connectionOptions = args[2]; } // // Create and open an AMQP connection to the broker URL // Connection connection = new Connection(url); connection.Open(); // // Create a session and a receiver fir the direct exchange using the // routing key "map_example". // Session session = connection.CreateSession(); Receiver receiver = session.CreateReceiver(address); // // Fetch the message from the broker // Message message = receiver.Fetch(DurationConstants.MINUTE); // // Extract the structured content from the message. // Dictionary <string, object> content = new Dictionary <string, object>(); message.GetContent(content); Console.WriteLine("{0}", message.AsString(content)); // // Acknowledge the receipt of all received messages. // session.Acknowledge(); // // Close the receiver and the connection. // receiver.Close(); connection.Close(); return(0); }
// // Sample invocation: csharp.example.drain.exe --broker localhost:5672 --timeout 30 my-queue // static int Main(string[] args) { Options options = new Options(args); Connection connection = null; try { connection = new Connection(options.Url, options.ConnectionOptions); connection.Open(); Session session = connection.CreateSession(); Receiver receiver = session.CreateReceiver(options.Address); Duration timeout = options.Forever ? DurationConstants.FORVER : DurationConstants.SECOND * options.Timeout; Message message = new Message(); while (receiver.Fetch(ref message, timeout)) { Dictionary <string, object> properties = new Dictionary <string, object>(); properties = message.Properties; Console.Write("Message(properties={0}, content='", message.MapAsString(properties)); if ("amqp/map" == message.ContentType) { Dictionary <string, object> content = new Dictionary <string, object>(); message.GetContent(content); Console.Write(message.MapAsString(content)); } else if ("amqp/list" == message.ContentType) { Collection <object> content = new Collection <object>(); message.GetContent(content); Console.Write(message.ListAsString(content)); } else { Console.Write(message.GetContent()); } Console.WriteLine("')"); session.Acknowledge(); } receiver.Close(); session.Close(); connection.Close(); return(0); } catch (Exception e) { Console.WriteLine("Exception {0}.", e); if (null != connection) { connection.Close(); } } return(1); }
/// <summary> /// Close the socket currently listening, and destroy the UDP sender device. /// </summary> public void Close() { if (Sender != null) { Sender.Close(); } if (Receiver != null) { Receiver.Close(); } socketsOpen = false; }
static void Main(string[] args) { PlayerManager.Init(args); UdpClient client = new UdpClient(Const.ServerPort); Receiver receiver = new Receiver(client); Console.WriteLine(String.Join("\n", new string[] { "Server ready to accept connections.", "Type 'quit' to shut the server down, 'next' to load next level." })); TimeSpan refreshPeriod = new TimeSpan(0, 0, 0, 0, Const.GameStepDuration); List <Task> tasks = new List <Task>(); DateTime lastRefresh = DateTime.Now; string cmd = ""; while (cmd != "quit") { cmd = Prompt.ReadLine(); if (PlayerManager.players.Count == 0) { Thread.Sleep(1000); } if (DateTime.Now - lastRefresh >= refreshPeriod) { lastRefresh = DateTime.Now; tasks.RemoveAll((task) => task.Status != TaskStatus.Running); tasks.Add(Task.Run(PlayerManager.Refresh)); } if (cmd == "next") { PlayerManager.NextLevel(); foreach (var keyVal in PlayerManager.players) { SendGameState(GameState.RequestType.LevelUpdate, keyVal.Value.ID, keyVal.Key, client); } } byte[] receivedBytes = receiver.GetBytes(out IPEndPoint endPoint); if (receivedBytes != null) { tasks.Add(Task.Run(() => ProcessData(receivedBytes, endPoint, client))); } } Task.WaitAll(tasks.ToArray()); client.Close(); receiver.Close(); }
private void button17_Click(object sender, EventArgs e)//打开串口Receiver { if (!Receiver.IsOpen) { Receiver.PortName = comboBox4.Text; //设置串口名 Receiver.BaudRate = Convert.ToInt32(comboBox3.Text, 10); Receiver.Encoding = System.Text.Encoding.GetEncoding("GB2312"); //此行非常重要 可解决接收中文乱码问题 try { if (comboBox4.Text == comboBox1.Text) { Receiver = Transmitter; same_serial_flag = true; button10.Text = "关闭串口"; comboBox1.Enabled = false; //关闭使能 comboBox2.Enabled = false; //关闭使能 } Receiver.Open(); //打开串口 button17.Text = "关闭串口"; comboBox4.Enabled = false; //关闭使能 comboBox3.Enabled = false; //关闭使能 Receiver.DataReceived += new SerialDataReceivedEventHandler(Receiver_DataReceived); //串口接收处理函数 } catch { MessageBox.Show("Receiver串口打开失败!"); } } else { try { Receiver.Close(); //关闭串口 button17.Text = "打开串口"; comboBox4.Enabled = true; //打开使能 comboBox3.Enabled = true; //打开使能 if (same_serial_flag == true) { button10.Text = "打开串口"; comboBox1.Enabled = true; //打开使能 comboBox2.Enabled = true; //打开使能 } } catch { MessageBox.Show("Receiver串口关闭失败!"); } } }
public void CloseConnection() { Console.WriteLine("STATUS: Closing listener socket"); Listener.Close(); if (Sender != null) { try { Console.WriteLine("STATUS: Shutdown sender socket"); Sender.Shutdown(SocketShutdown.Both); } catch (SocketException) { Console.WriteLine("STATUS: Closing sender socket"); Sender.Close(); } catch (ObjectDisposedException) { Console.WriteLine("STATUS: Sender socket already closed"); Sender.Close(); } } if (Receiver != null) { try { Console.WriteLine("STATUS: Shutdown receiver socket"); Receiver.Shutdown(SocketShutdown.Both); } catch (SocketException) { Console.WriteLine("STATUS: Closing receiver socket"); Receiver.Close(); } catch (ObjectDisposedException) { Console.WriteLine("STATUS: Receiver socket already closed"); Receiver.Close(); } } }
/// <summary> /// Closes this TCP client connection and unbinds it's socket. /// </summary> public void Close() { if (IsBound) { Unbind(); } if (Receiver != null) { Receiver.Close(); Receiver = null; } if (Stream != null) { Stream = null; } System.GC.SuppressFinalize(this); }
/// <summary> /// Close the socket currently listening, and destroy the UDP sender device. /// </summary> public void Close() { Sender.Close(); Receiver.Close(); socketsOpen = false; }
static int Main(string[] args) { string[] loggerOptions = { "", "--log-enable", "info+", "--log-to-stdout", "on", "--log-time", "on", "--log-level", "on" }; Logger logger = new Logger(); logger.Configure(loggerOptions); string brokerAddr = "amqp:ssl:ecag-fixml-simu1.deutsche-boerse.com:10170"; string connectionOptions = "{ protocol: amqp1.0 }"; string replyAddress = "response/response.CBKFR_TESTCALMMACC1; { node: { type: topic }, assert: never, create: never }"; string requestAddress = "request.CBKFR_TESTCALMMACC1; { node: { type: topic }, assert: never, create: never }"; string responseAddress = "response.CBKFR_TESTCALMMACC1; {create: never, assert: never, node: { type: queue } }"; Connection connection = null; Session session; try { /* * Step: Preparing the connection and session */ connection = new Connection(brokerAddr, connectionOptions); connection.SetOption("sasl_mechanisms", "EXTERNAL"); connection.Open(); session = connection.CreateSession(); Console.WriteLine("-I- Connection opened, session created"); /* * Step: Creating message consumer */ Receiver receiver = session.CreateReceiver(responseAddress); receiver.Capacity = 100; Sender sender = session.CreateSender(requestAddress); sender.Capacity = 100; Console.WriteLine("-I- Receiver and sender created "); /* * Step: Send request message */ Message requestMsg = new Message("<FIXML>...</FIXML>"); requestMsg.ReplyTo = new Address(replyAddress); sender.Send(requestMsg); Console.WriteLine("-I- Request sent: {0}", requestMsg.GetContent()); /* * Step: Receive response message */ Message msg = receiver.Fetch(DurationConstants.SECOND * 100); Console.WriteLine("-I- Response message received with content: {0}", msg.GetContent()); /* * Step: Stop the callback server and close receiver */ receiver.Close(); sender.Close(); session.Close(); } catch (QpidException ex) { Console.WriteLine("-E- QpidException caught: {0}", ex.Message); return(1); } finally { if (connection != null && connection.IsOpen) { Console.WriteLine("-I- Closing the connection."); connection.Close(); } } return(0); }
static int Main(string[] args) { string[] loggerOptions = { "", "--log-enable", "info+", "--log-to-stdout", "on", "--log-time", "on", "--log-level", "on" }; Logger logger = new Logger(); logger.Configure(loggerOptions); string brokerAddr = "amqp:ssl:ecag-fixml-simu1.deutsche-boerse.com:10170"; string connectionOptions = "{ protocol: amqp1.0}"; string broadcastAddress = "broadcast.ABCFR_ABCFRALMMACC1.TradeConfirmation; { node: { type: queue }, create: never, mode: consume, assert: never }"; Connection connection = null; Session session; try { /* * Step 1: Preparing the connection and session */ connection = new Connection(brokerAddr, connectionOptions); connection.SetOption("sasl_mechanisms", "EXTERNAL"); connection.Open(); session = connection.CreateSession(); Console.WriteLine("-I- Connection opened, session created"); /* * Step 2: Create callback server and implicitly start it */ // The callback server is running and executing callbacks on a separate thread. SessionReceiver.CallbackServer cbServer = new SessionReceiver.CallbackServer(session, new Listener()); /* * Step 3: Creating message consumer */ Receiver receiver = session.CreateReceiver(broadcastAddress); Console.WriteLine("-I- Receiver created "); receiver.Capacity = 100; System.Threading.Thread.Sleep(100 * 1000); // in mS /* * Step 4: Stop the callback server and close receiver */ receiver.Close(); cbServer.Close(); session.Close(); } catch (QpidException ex) { Console.WriteLine("-E- QpidException caught: {0}", ex.Message); return(1); } finally { if (connection != null && connection.IsOpen) { Console.WriteLine("-I- Closing the connection."); connection.Close(); } } return(0); }
static int Main(string[] args) { string[] loggerOptions = { "", "--log-enable", "info+", "--log-to-stdout", "on", "--log-time", "on", "--log-level", "on" }; Logger logger = new Logger(); logger.Configure(loggerOptions); string brokerAddr = "amqp:ssl:ecag-fixml-simu1.deutsche-boerse.com:10170"; string connectionOptions = "{ }"; string replyAddress = "response/response.ABCFR_ABCFRALMMACC1.response_queue_1; { node: { type: topic }, assert: never, create: never }"; string requestAddress = "request.ABCFR_ABCFRALMMACC1; { node: { type: topic }, assert: never, create: never }"; string responseAddress = "response.ABCFR_ABCFRALMMACC1.response_queue_1; {create: receiver, assert: never, node: { type: queue, x-declare: { auto-delete: true, exclusive: true, arguments: {'qpid.policy_type': ring, 'qpid.max_count': 1000, 'qpid.max_size': 1000000}}, x-bindings: [{exchange: 'response', queue: 'response.ABCFR_ABCFRALMMACC1.response_queue_1', key: 'response.ABCFR_ABCFRALMMACC1.response_queue_1'}]}}"; Connection connection = null; Session session; try { /* * Step: Preparing the connection and session */ connection = new Connection(brokerAddr, connectionOptions); connection.SetOption("heartbeat", "30"); connection.SetOption("sasl_mechanisms", "EXTERNAL"); connection.Open(); session = connection.CreateSession(); Console.WriteLine("-I- Connection opened, session created"); /* * Step: Creating message consumer */ Receiver receiver = session.CreateReceiver(responseAddress); receiver.Capacity = 100; Sender sender = session.CreateSender(requestAddress); sender.Capacity = 100; Console.WriteLine("-I- Receiver and sender created "); /* * Step: Send request message */ Message requestMsg = new Message("<FIXML>...</FIXML>"); requestMsg.ReplyTo = new Address(replyAddress); sender.Send(requestMsg); Console.WriteLine("-I- Request sent: {0}", requestMsg.GetContent()); /* * Step: Receive response message */ Message msg = receiver.Fetch(DurationConstants.SECOND * 100); Console.WriteLine("-I- Response message received with content: {0}", msg.GetContent()); /* * Step: Stop the callback server and close receiver */ receiver.Close(); sender.Close(); session.Close(); } catch (QpidException ex) { Console.WriteLine("-E- QpidException caught: {0}", ex.Message); return(1); } finally { if (connection != null && connection.IsOpen) { Console.WriteLine("-I- Closing the connection."); connection.Close(); } } return(0); }
/// <summary> /// A function to illustrate how to open a Session callback and /// receive messages. /// </summary> /// <param name="args">Main program arguments</param> public int TestProgram(string[] args) { string url = "amqp:tcp:localhost:5672"; string addr = "amq.direct/map_example"; int nSec = 30; string connectionOptions = ""; if (1 == args.Length) { if (args[0].Equals("-h") || args[0].Equals("-H") || args[0].Equals("/?")) { usage(url, addr, nSec); return(1); } } if (args.Length > 0) { url = args[0]; } if (args.Length > 1) { addr = args[1]; } if (args.Length > 2) { nSec = System.Convert.ToInt32(args[2]); } if (args.Length > 3) { connectionOptions = args[3]; } // // Create and open an AMQP connection to the broker URL // Connection connection = new Connection(url, connectionOptions); connection.Open(); // // Create a session. // Session session = connection.CreateSession(); // // Receive through callback // // Create callback server and implicitly start it // SessionReceiver.CallbackServer cbServer = new SessionReceiver.CallbackServer(session, this); // // The callback server is running and executing callbacks on a // separate thread. // // // Create a receiver for the direct exchange using the // routing key "map_example". // Receiver receiver = session.CreateReceiver(addr); // // Establish a capacity // receiver.Capacity = 100; // // Wait so many seconds for messages to arrive. // System.Threading.Thread.Sleep(nSec * 1000); // in mS // // Stop the callback server. // cbServer.Close(); // // Close the receiver and the connection. // try { receiver.Close(); connection.Close(); } catch (Exception exception) { // receiver or connection may throw if they closed in error. // A typical application will take more action here. Console.WriteLine("{0} Closing exception caught.", exception.ToString()); } return(0); }