Пример #1
0
 public ServerMessageHandler(MessageHandlerUri address)
 {
     Address = address;
     Type    = HandlerProtocol.Reply;
     _socket = new ReplySocket();
     _socket.Bind(address.OriginalString);
     Trace.WriteLine($"Server initialized: socket = {_socket}");
     Trace.WriteLine($"Server error: {NN.Errno()}");
 }
Пример #2
0
 private static void Node0(string url)
 {
     using (var s = new ReplySocket())
     {
         s.Bind(url);
         Console.WriteLine("NODE0: RECEIVED: \"" + Encoding.UTF8.GetString(s.Receive()) + "\"");
         const string sendText = "Goodbye.";
         Console.WriteLine("NODE0: SENDING: \"" + sendText + "\"");
         s.Send(Encoding.UTF8.GetBytes(sendText));
     }
 }
Пример #3
0
    /*
     * The Authentication server is a request-reply server which handles
     * authentication for the World server. Right now registration is simply:
     *
     * 1) Generate a random hash as an id
     * 3) Register this id with the authentication server by sending:
     *      "REG <id>"
     * 4) Server will reply "<id>-ACK" to acknowledge the request. If the
     * request is denied "<id>-DENIED" will be sent.
     * 5) If acknowledged can now connect to World server with the hash as
     * an id and subscribe to that id.
     */
    public void AuthThreadProc ()
    {
        using (var s = new ReplySocket())
        {
            s.Bind(url + ":8889");
            while (running) {
                byte[] bytes = s.Receive();
                if (bytes == null)
                    continue;

                String reply = "Bad Request.";
                String request = Encoding.ASCII.GetString(bytes);
                String[] e = request.Split(' ');

                if (e[0] == "REG") {
                    Console.WriteLine("Got registration request: " + request);
                    registered[e[1]] = 0;
                    reply = e[1] + "-ACK";
                }
                else if (e[0] == "ACCESS") {
                    /*
                     * Check that id has registered.
                     * TODO:
                     *  When the new client has connected we can construct new
                     *  events to send to the client through the World server.
                     *  These events would be the 'newConnection' events such
                     *  as initial placements of current NPCs.
                     */
                    if (registered.ContainsKey(e[1])) {
                        reply = e[1] + "-ACK";
                        /*
                         * Need a collection of Entities on the server side.
                         * Need generic entity on Client side which to
                         * instantiate when it receives this event.
                         */
                        eventChannel.Insert(e[1] + " new ");
                        //var props = ScriptHandler.Properties();
                        //foreach (var pair in props)
                        //    eventChannel.Insert(e[1] + " new " + pair.Value);
                    } else {
                        reply = e[1] + "-DENIED";
                    }
                }
                else if (e[0] == "END") {
                    Console.WriteLine("Ending Connection: " + request);
                    registered.Remove(e[1]);
                    eventChannel.Insert(e[1] + " end");
                    reply = e[1] + "-ACK";
                }

                s.Send(Encoding.UTF8.GetBytes(reply));
            }
        }
    }
        void Download()
        {
            ReplySocket sockClientListner;

            try
            {
                sockClientListner = new ReplySocket();

                sockClientListner.Bind("tcp://" + global.Instance.RecClientConIp + ":" + global.Instance.ClientPort_Rec);

                //SubscribeSocket subscriber = new SubscribeSocket();
                //subscriber.Connect("tcp://" +global.Instance.ClientConIp+ ":" +global.Instance.ClientPort_Rec);
                Console.WriteLine("NANOMQ UDP_Reciever Start DataAddress: " + "tcp://" + global.Instance.RecClientConIp + ":" + global.Instance.ClientPort_Rec);
                //subscriber.Subscribe(BitConverter.GetBytes(Convert.ToInt64(global.Instance.Client1)));
                //subscriber.Subscribe(BitConverter.GetBytes(Convert.ToInt64(global.Instance.Client2)));
                int cnt = 0;
                while (true)
                {
                    //Thread.Sleep(10000);
                    //var buffer = subscriber.Receive();
                    Console.WriteLine("cnt :\t" + cnt);

                    var buffer = sockClientListner.Receive();
                    cnt++;
                    Console.WriteLine("Recieved :" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));

                    string msg = Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray());

                    if ("" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()) == "upload")
                    {
                        //Console.WriteLine("2" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));
                        Program.Instance.send(BitConverter.ToInt64(buffer, 0), msg);
                    }

                    if ("" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()) == "recieved")
                    {
                        //Console.WriteLine("2" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));
                        Program.Instance.send(BitConverter.ToInt64(buffer, 0), msg);
                    }
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
Пример #5
0
            public static void Response(CancellationToken cancelToken)
            {
                using (var s = new ReplySocket())
                {
                    s.Bind(MessageQueueConstants.Address);

                    while (!cancelToken.IsCancellationRequested)
                    {
                        byte[] payloadBytes = s.Receive();
                        Debug.WriteLine("Response: Received request");

                        var payload = Encoding.UTF8.GetString(payloadBytes);
                        Assert.IsTrue(payload.Equals(MessageQueueConstants.RequestPayload));

                        Debug.WriteLine("Response: Sending reply");
                        s.Send(BitConverter.GetBytes(MessageQueueConstants.ReplyPayload));
                    }
                }
            }
Пример #6
0
        public static void Execute(string[] args)
        {
            const string socketAddress = "tcp://127.0.0.1:5088";

            if (args[1] == "client")
            {
                using (var req = new RequestSocket())
                {
                    req.Connect(socketAddress);
                    req.Send(Encoding.UTF8.GetBytes("hello from client"));
                    Console.WriteLine("Message from SERVER: " + Encoding.UTF8.GetString(req.Receive()));

                    Console.WriteLine("CLIENT finished");
                }
            }

            else if (args[1] == "server")
            {
                using (var rep = new ReplySocket())
                {
                    rep.Bind(socketAddress);

                    var listener = new NanomsgListener();
                    listener.AddSocket(rep);
                    listener.ReceivedMessage += socketId =>
                    {
                        Console.WriteLine("Message from CLIENT: " + Encoding.UTF8.GetString(rep.Receive()));
                        rep.Send(Encoding.UTF8.GetBytes("hello from server"));
                        Console.WriteLine("SERVER Finished");
                        Environment.Exit(0);
                    };

                    listener.Listen(null);
                }
            }
            else
            {
                Console.WriteLine("Unknown argument: " + args[1]);
            }
        }