Пример #1
0
        public void ConnectionExistingClientTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));
            host.AddAcceptor(9001, (con, conClient) => new DataTransferHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    Connection connection = client.TakeConnection(9001);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(2, connectedClient.HandlerCount);

                    client.ReleaseConnection(connection);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }
Пример #2
0
        public void ConnectionNewClientTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(1, connectedClient.HandlerCount);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }
Пример #3
0
    private void Start()
    {
        RegisterCommandHandlers();
#if UNITY_EDITOR
        if (m_SpoofedTextAsset != null)
        {
            m_InEditorSpoof = new InEditorSpoof(m_SpoofedTextAsset.text);
            return;
        }
#endif
        m_Client = new SimpleClient(m_ServerIpAddress, m_PortNumber, MessageListener);
        m_Client.Connect();
    }
Пример #4
0
    void Start()
    {
        sc = new SimpleClient();

        if (sc.Connect("127.0.0.1", 1337))
        {
            //sc.Run();
            Console.WriteLine("Client Started");
        }
        else
        {
            Console.WriteLine("Client failed to connect!");
        }
    }
Пример #5
0
        static void Main()
        {
            MyServer server = new MyServer();

            server.AddService(new MySearchService());
            server.Start();

            var            canalCliente = SimpleClient.Connect("localhost", 12345);
            MySearchClient cliente      = new MySearchClient(canalCliente);

            SearchRequest req = new SearchRequest();

            req.FileName = "*.bat";
            var resp = cliente.Search(req);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormTeste());
        }
Пример #6
0
        public void AfterClientDisconnectTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(1, connectedClient.HandlerCount);

                    using (HostEventListener eventWait = new HostEventListener(host))
                    {
                        client.DisconnectAsync().Wait();

                        eventWait.SetStrategyUnlock(new UnlockIfRemoveLastHandler());

                        eventWait.Wait();
                        Assert.AreEqual(0, connectedClient.HandlerCount);

                        eventWait.Reset();

                        eventWait.SetStrategyUnlock(new UnlockIfRemoveLastClient());

                        eventWait.Wait();
                        Assert.AreEqual(0, host.ConnectedClientCount);
                    }
                }
            }
            finally
            {
                host.Close();
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            ChatMessage message = new ChatMessage();

            message.From    = "我是路人甲";
            message.To      = "我是路人乙";
            message.Message = "这些年你过得可好?";

            SimpleClient client = new SimpleClient("127.0.0.1", 8081);

            client.Connect();
            client.OnSocketReceive += client_OnSocketReceive;
            client.Send(LitJson.JsonMapper.ToJson(message));

            while (true)
            {
                client.Receive();
                client.Send(Console.ReadLine());
            }
        }
Пример #8
0
    void OnGUI()
    {
        if (!showButton)
        {
            return;
        }

        if (GUI.Button(new Rect(100, 100, 100, 50), "Start sever"))
        {
            Application.targetFrameRate = 20;
            SimpleServer.Start("myAppId");
            showButton = false;
        }

        if (GUI.Button(new Rect(100, 200, 100, 50), "Start client"))
        {
            Application.targetFrameRate = 60;
            SimpleClient.Connect("myAppId");
            showButton = false;
        }
    }
Пример #9
0
        private async Task <PersonalPacket> GetNewConnectionPacket(string username)
        {
            _listenTask = Task.Run(() => _client.Connect());

            IsRunning = await _listenTask;

            var notifyServer = new UserConnectionPacket
            {
                Username  = username,
                IsJoining = true,
                UserGuid  = _client.ClientId.ToString()
            };

            var personalPacket = new PersonalPacket
            {
                GuidId  = _client.ClientId.ToString(),
                Package = notifyServer
            };

            return(personalPacket);
        }
Пример #10
0
        public void CommunicationObjectReceiveAndSendDataTest()
        {
            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));
            host.AddAcceptor(9001, (con, conClient) => new DataTransferHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();

                    short    commandCodeOld = 5;
                    short    errorCodeOld   = 4;
                    FileData fileDataOld    = new FileData("fileName.txt", 1024);

                    var receiveData = client.SendAndReceiveData(commandCodeOld, errorCodeOld, fileDataOld);

                    short    commandCodeNew = receiveData.Item1;
                    short    errorCodeNew   = receiveData.Item2;
                    FileData fileDataNew    = receiveData.Item3;

                    Assert.AreEqual(commandCodeOld, commandCodeNew);
                    Assert.AreEqual(errorCodeOld, errorCodeNew);
                    Assert.AreEqual(fileDataOld, fileDataNew);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }
Пример #11
0
    private void ClientSettingsGUI()
    {
        GUILayout.BeginArea(new Rect(0, buttonHeight + 20, 300, 400));
        client.ServerMode = EditorGUILayout.Toggle("Act as Server", client.ServerMode);

        #region Dropdown connections
        // Generate the connection dropdown options/content
        List <GUIContent> options         = new List <GUIContent>();
        string[]          connectionNames = AmqpConfigurationEditor.GetConnectionNames();

        //if (Event.current.type == EventType.Layout)
        //{
        for (var i = 0; i < connectionNames.Length; i++)
        {
            var cName = connectionNames[i];
            if (string.IsNullOrEmpty(client.Connection) || client.Connection == cName)
            {
                selectedConfigurationIndex = i;
            }
            options.Add(new GUIContent(cName));
        }

        if (options.Count == 0)
        {
            Init();
        }
        //}

        // Connections drop down
        string tooltip = "Select the AMQP connection to use. Connections can be configured in the AMQP/Configuration menu.";
        selectedConfigurationIndex = EditorGUILayout.Popup(new GUIContent("Connection", tooltip), selectedConfigurationIndex, options.ToArray());

        // Set the connection name based on dropdown value
        try
        {
            GUIContent con = options[selectedConfigurationIndex];
            client.Connection = options[selectedConfigurationIndex].text;
        }
        catch (ArgumentException)
        {
            Repaint();
            return;
        }
        #endregion // Dropdown connections
        string connectionString = options[selectedConfigurationIndex].text;

        #region REST-Versuche
        AmqpConnection connection = SimpleClient.GetConnection(connectionString);
        //if (connection != null)
        //{
        //    //string consumers = "http://*****:*****@" + connection.Host + ":" + connection.AmqpPort + "/api/consumers";
        //    //Debug.Log("http://*****:*****@" + connection.Host + ":" + connection.AmqpPort + "/api/consumers");

        //    if (managementAPI == null && client.IsConnected)
        //    {
        //        managementAPI = new RabbitmqManagementAPI(connection);
        //    }

        //    if (overview == null)
        //    {
        //        if (client.IsConnected)
        //        {
        //            overview = managementAPI.Overview();
        //            //overview.Start();
        //        }

        //    }
        //    else
        //    {
        //        Debug.Log("OverviewResult: " + overview.GetResult());
        //        //Debug.Log("overViewRequest != null");
        //        if (client.IsConnected)
        //        {
        //            if (overview.RequestFinished)
        //            {
        //                if (!overview.RequestErrorOccurred)
        //                {
        //                    Debug.Log("not finished: " + overview.ResponseCode.ToString());
        //                }
        //                else
        //                {
        //                    Debug.Log("OverviewResult: " + overview.GetResult());
        //                }
        //            }
        //            else
        //            {
        //                //Debug.Log("not finished: " + overview.ResponseCode.ToString());
        //            }
        //        }
        //    }
        //}


        //if (connection != null)
        //{
        //    //http://10.211.55.2:15672/api/queues
        //    string ourPostData = "";
        //    Dictionary<string, string> headers = new Dictionary<string, string>();
        //    headers.Add("Content-Type", "application/json");

        //    byte[] pData = new byte[0];
        //    string consumers = "http://" + connection.Host + ":" + connection.AmqpPort + "/api/queues";
        //    WWW api = new WWW(consumers, pData, headers);

        //    while (!api.isDone)
        //    {
        //        if (api.error != null)
        //        {
        //            Debug.Log("Error");
        //            break;
        //        }
        //    }

        //    string jsonStr = Encoding.UTF8.GetString(api.bytes);
        //    Debug.Log(consumers + " : " + api.text);
        //}



        //WWW api = new WWW(consumers);

        //while (!api.isDone)
        //{
        //    // wait
        //}
        //Debug.Log("result: " + api.text);

        //UnityWebRequest
        //UnityWebRequest request = UnityWebRequest.Get(consumers);
        //while (!request.isDone || request.isError)
        //{
        //    // wait
        //    if (request.isError)
        //    {
        //        Debug.Log("Error");
        //    }
        //}
        //Debug.Log("result UnityWebRequest: " + request.downloadHandler.text);
        #endregion // REST-Versuche


        if (GUILayout.Button("Awake"))
        {
            client.Awake();
            Repaint();
            return;
        }

        EditorGUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("EnableUpdate"))
            {
                client.EnableUpdate();
                return;
            }

            if (GUILayout.Button("DisableUpdate"))
            {
                client.DisableUpdate();
                return;
            }
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Toggle("IsConnecting?", client.isConnecting);
        EditorGUILayout.Toggle("IsConnected?", client.IsConnected);
        EditorGUILayout.Toggle("hasConnected?", client.hasConnected);
        EditorGUILayout.Toggle("isDisconnecting?", client.isDisconnecting);
        EditorGUILayout.Toggle("hasDisconnected?", client.hasDisconnected);
        EditorGUILayout.Toggle("isReconnecting?", client.isReconnecting);
        EditorGUILayout.Toggle("wasBlocked?", client.wasBlocked);
        EditorGUILayout.Toggle("hasAborted?", client.hasAborted);
        EditorGUILayout.Toggle("canSubscribe?", client.canSubscribe);

        EditorGUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("Connect"))
            {
                client.Connect();
                return;
            }

            if (GUILayout.Button("Disconnect"))
            {
                client.Disconnect();
                return;
            }
        }
        EditorGUILayout.EndHorizontal();

        #region Available Queues
        EditorGUILayout.BeginVertical("box");
        foreach (var queue in client.GetQueues())
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Name:", queue.Name);


            if (GUILayout.Button("Subscribe", GUILayout.Width(30)))
            {
                client.SubscribeToQueue(queue.Name);
            }

            if (GUILayout.Button("X", GUILayout.Width(30)))
            {
                client.DeleteQueue(queue.Name);
            }
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndVertical();
        #endregion Available Queues

        #region Create Queue
        EditorGUILayout.BeginHorizontal("box");
        this.createQueueName = EditorGUILayout.TextField("Queue Name", this.createQueueName);
        if (GUILayout.Button("Create"))
        {
            client.DeclareQueue(this.createQueueName);
        }
        EditorGUILayout.EndHorizontal();
        #endregion // Create Queue

        #region Subscribe Queue
        AmqpQueue[] queues = client.GetQueues();
        string[]    names  = new string[queues.Length];
        for (int i = 0; i < queues.Length; i++)
        {
            names[i] = queues[i].Name;
        }

        selectedQueue = EditorGUILayout.Popup("Queue", selectedQueue, names);
        if (queues.Length != 0)
        {
            if (GUILayout.Button("Subscribe to " + names[this.selectedQueue] + " Queue"))
            {
                client.SubscribeToQueue(names[this.selectedQueue]);
            }
        }
        #endregion // Subscribe Queue

        #region JobQueue
        string[] jobQueueNames = new string[client.GetQueues().Length];
        for (int i = 0; i < client.GetQueues().Length; i++)
        {
            jobQueueNames[i] = client.GetQueues()[i].Name;
        }

        this.jobQueueIndex = EditorGUILayout.Popup("JobQueue", this.jobQueueIndex, jobQueueNames);
        for (int i = 0; i < jobQueueNames.Length; i++)
        {
            if (jobQueueNames[i] == "jobs")
            {
                this.jobQueueIndex = i;
                break;
            }
        }
        jobQueueName = client.GetQueues().Length == 0 ? "not set" : jobQueueNames[this.jobQueueIndex];
        //EditorGUILayout.LabelField("JobQueue", jobQueueName);
        #endregion // JobQueue

        #region ReplyQueue
        string[] replyToQueueNames = new string[client.GetQueues().Length];
        for (int i = 0; i < client.GetQueues().Length; i++)
        {
            replyToQueueNames[i] = client.GetQueues()[i].Name;
        }

        this.replyQueueIndex = EditorGUILayout.Popup("ReplyToQueue", this.replyQueueIndex, replyToQueueNames);
        for (int i = 0; i < replyToQueueNames.Length; i++)
        {
            if (replyToQueueNames[i] == "reply")
            {
                this.replyQueueIndex = i;
                break;
            }
        }
        replyQueueName = client.GetQueues().Length == 0 ? "not set" : replyToQueueNames[this.replyQueueIndex];
        //EditorGUILayout.LabelField("ReplyToQueue", replyQueueName);
        #endregion // JobQueue

        #region StatusUpdateQueue
        string[] statusUpdateQueueNames = new string[client.GetQueues().Length];
        for (int i = 0; i < client.GetQueues().Length; i++)
        {
            statusUpdateQueueNames[i] = client.GetQueues()[i].Name;
        }

        this.statusUpdateQueueIndex = EditorGUILayout.Popup("ReplyToQueue", this.statusUpdateQueueIndex, statusUpdateQueueNames);
        for (int i = 0; i < statusUpdateQueueNames.Length; i++)
        {
            if (statusUpdateQueueNames[i] == "statusUpdates")
            {
                this.statusUpdateQueueIndex = i;
                break;
            }
        }
        statusUpdateQueueName = client.GetQueues().Length == 0 ? "not set" : statusUpdateQueueNames[this.statusUpdateQueueIndex];
        #endregion // StatusUpdateQueue

        GUILayout.EndArea();
    }
Пример #12
0
 private void Start()
 {
     client = new SimpleClient("127.0.0.1", 5670, HandleMessage);
     client.Connect();
 }