コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Tier 3 here");
            var reciveSocket = new ResponseSocket();

            reciveSocket.Bind("tcp://0.0.0.0:" + listeningPort);

            var multipartMessage = reciveSocket.ReceiveMultipartMessage();

            var frames = new List <NetMQFrame>();

            while (false == multipartMessage.IsEmpty)
            {
                frames.Add(multipartMessage.Pop());
            }

            Console.WriteLine(frames[3]);

            while (frames.Count > 2)
            {
                frames.RemoveAt(2);
            }

            frames.Add(new NetMQFrame("Yes this is from server three"));
            var responseMessage = new NetMQMessage(frames);

            reciveSocket.SendMultipartMessage(responseMessage);

            Console.ReadKey();
        }
コード例 #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using var server = new ResponseSocket("tcp://*:8050");


            while (!stoppingToken.IsCancellationRequested)
            {
                string serverMessage = "";
                await Task.Run(() =>
                {
                    try
                    {
                        serverMessage = server.ReceiveFrameString();
                        Console.WriteLine($"Client Sent: {serverMessage} DateTime: {DateTime.Now}");
                        Console.WriteLine("Sending reply to the client ...");

                        const string msg1 = "Hi", msg2 = "How are you ?";

                        var response = new NetMQMessage();
                        response.Append(msg1);
                        response.Append(msg2);

                        server.SendMultipartMessage(response);
                        Console.WriteLine($"Response has been sent => 1: {msg1}, 2: {msg2}");

                        _hubContext.Clients.All.SendAsync("ReceiveNotification", serverMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error: {ex.Message} DateTime: {DateTime.Now}");
                    }
                });
            }
        }
コード例 #3
0
        private void CreateWorker(object worker)
        {
            var workerParam = worker as WorkerParam;

            _workers.Add(workerParam.Name);

            using (var workerClient = new ResponseSocket($"inproc://{workerParam.Name}"))
            {
                var message = workerClient.ReceiveMultipartMessage();
                var handler = _resolver.Resolve(workerParam.Type) as IHandler;

                var response = handler.Initialize(message[2].ConvertToString());

                var messageToRouter = new NetMQMessage();
                messageToRouter.Append(message[0]);
                messageToRouter.AppendEmptyFrame();
                messageToRouter.Append(JsonSerializer.Serialize(response));

                workerClient.SendMultipartMessage(messageToRouter);
            }
        }
コード例 #4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using var server = new ResponseSocket("tcp://*:8050");

            while (!stoppingToken.IsCancellationRequested)
            {
                string serverMessage = server.ReceiveFrameString();
                Console.WriteLine($"Client Sent: {serverMessage}");

                Console.WriteLine("Sending reply to the client ...");

                const string msg1 = "Hi", msg2 = "How are you ?";

                var response = new NetMQMessage();
                response.Append(msg1);
                response.Append(msg2);

                server.SendMultipartMessage(response);
                Console.WriteLine($"Response has been sent => 1: {msg1}, 2: {msg2}");

                await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
            }
        }
コード例 #5
0
        private void SendResponse(ResponseSocket server, RequestType requestType)
        {
            /*
             * Идея структуры ответа такова:
             * - первым кадром возвращается RequestType, полученный в запросе;
             * - вторым и последующими кадрами - полезная нагрузка;
             */

            switch (requestType)
            {
            case RequestType.GetSmallPayload:
                server.SendMultipartMessage(GetSmallPayloadMessage());
                break;

            case RequestType.GetLargePayload:
                server.SendMultipartMessage(GetLargePayloadMessage());
                break;

            case RequestType.PostLargePayload:
                server.SendMultipartMessage(PostLargePayloadMessage());
                break;

            case RequestType.GetLargePayloadMultipart:
                server.SendMultipartMessage(GetLargePayloadMessageMultipart());
                break;

            case RequestType.PostLargePayloadMultipart:
                server.SendMultipartMessage(PostLargePayloadMessageMultipart());
                break;

            case RequestType.Unspecified:
            default:
                server.SendMultipartMessage(GetUnspecifiedRequestMessage());
                break;
            }
        }
コード例 #6
0
        /// <summary>
        /// ZMQ通信、接收到命令及参数、根据命令进行相应处理
        /// </summary>
        public void Run()
        {
            int result = 1;

            while (true)
            {
                // 等待接收Message
                var message = serverSocket.ReceiveMultipartMessage();
                // 获取命令
                var Command = message[0].ConvertToString();

                string typeName   = "";
                string configName = "";
                string dataName   = "";
                byte[] pbData     = null;
                // switch case 进行判断
                switch (Command)
                {
                case "bind_config":
                    typeName = message[1].ConvertToString();
                    byte[] configData = message[2].Buffer;
                    configName = message[3].ConvertToString();
                    result     = bind_config(typeName, configData, configName);
                    break;

                case "bind_data":
                    typeName = message[1].ConvertToString();
                    pbData   = message[2].Buffer;
                    dataName = message[3].ConvertToString();
                    result   = bind_data(typeName, pbData, dataName);
                    break;

                case "start_service":
                    configName = message[1].ConvertToString();
                    result     = start_service(configName);
                    break;

                case "stop_service":
                    configName = message[1].ConvertToString();
                    result     = stop_service(configName);
                    break;

                case "set_data":
                    typeName = message[1].ConvertToString();
                    pbData   = message[2].Buffer;
                    dataName = message[3].ConvertToString();
                    result   = set_data(dataName, typeName, pbData);
                    break;

                case "get_data":
                    dataName = message[1].ConvertToString();
                    pb_data_sensor_list data_sensor_list = null;
                    result = get_data(dataName, ref data_sensor_list);
                    if (result == 1)
                    {
                        var msg = new NetMQMessage();
                        msg.Append("pb_data_sensor_list");
                        MemoryStream sm = new MemoryStream();
                        data_sensor_list.WriteTo(sm);
                        msg.Append(sm.ToArray());
                        serverSocket.SendMultipartMessage(msg);
                        log.Debug(string.Format("server response:{0} pb_data_sensor in data_sensor_list", data_sensor_list.PbDataSensors.Count));
                    }
                    else
                    {
                        serverSocket.SendFrame(result.ToString());
                        log.Debug(string.Format("server response:result = 0x{0:X2}", result));
                    }
                    break;

                case "dispose_data":
                    // 清空绑定的数据 缺少判断状态
                    configName = message[1].ConvertToString();
                    dataName   = message[2].ConvertToString();
                    result     = dispose_data(configName, dataName);
                    break;

                case "auto_get_data":
                    break;
                }
                Thread.Sleep(100);
                if (Command != "get_data")
                {
                    serverSocket.SendFrame(result.ToString());
                    log.Debug(string.Format("server response:result = 0x{0:X2}", result));
                }
            }
        }