Esempio n. 1
0
    // Use this for initialization
    void Start()
    {
        //registe bytes msg
        MsgManager.Instance.RegisterMsg(110, OnByteMsg);
        //you can registe many msg here
        //....

        //registe protobuf msg
        MsgManager.Instance.RegisterMsg(typeof(TestProtobufStruct).FullName, OnProtobufMsg);
        //....

        //connect(prefer host names)
        ClientTcp socket        = new ClientTcp();
        bool      tempIsConnect = socket.Connect("www.google.com", 111);

        Debug.Log("是否连接成功: " + tempIsConnect);

        // send byte msg
        MsgByte tempMsg1 = new MsgByte(110); //110 is proto id

        tempMsg1.Write <int>(100);           //write msg's body
        tempMsg1.Write("hello");             //write msg's body
        tempMsg1.Flush();                    //send

        //send protobuf msg
        TestProtobufStruct testProtobufStruct = new TestProtobufStruct();

        testProtobufStruct.x = 100;
        testProtobufStruct.y = "hello";
        MsgProtobuf tempMsg2 = new MsgProtobuf();

        tempMsg2.Write(testProtobufStruct);
        tempMsg2.Flush();//send
    }
Esempio n. 2
0
 public void Connection()
 {
     try
     {
         ClientTcp.Connect(IpAdresse, Cst_PortModbus);
         var factory = new ModbusFactory();
         ModBusMaster = factory.CreateMaster(ClientTcp);
         //DeleteUnexistingGroupe();
         Redresseurs = Model.Redresseur.GetAllRedresseurFromAutotameId(IpAdresse);
         foreach (Model.Redresseur redresseur in Redresseurs)
         {
             redresseur.ModBusMaster = ModBusMaster;
         }
         Mode = MODES.Connected.ToString();
     }
     catch (ArgumentNullException e)
     {
         Mode = MODES.Disconnected.ToString();
         Console.WriteLine("ArgumentNullException: {0}", e);
     }
     catch (SocketException e)
     {
         Mode = MODES.Disconnected.ToString();
         Console.WriteLine("SocketException: {0}", e);
     }
 }
Esempio n. 3
0
        public async Task Connection()
        {
            Assert.IsNotNull(server);

            ClientTcp client = new ClientTcp();

            Assert.IsNotNull(client);

            var isConnected = await client.Connect("127.0.0.1", port);

            Assert.IsFalse(isConnected);
            Assert.IsFalse(client.IsConnected);

            _ = Task.Run(server.StartListen);

            isConnected = await client.Connect("127.0.0.1", port);

            Assert.IsTrue(isConnected);
            Assert.IsTrue(client.IsConnected);
        }
Esempio n. 4
0
 public SocketHelper()
 {
     visionClient = new ClientTcp(IPAddress.Parse(Config.Vision.IP), Config.Vision.Port, 1024, true);
     visionClient.AddRules(new ReceiveDataRule {
         Prefix = "CAM"
     });
     visionClient.OnServerStateChangeEvent += (s) =>
     {
         StateColor = s ? "#FF11BB00" : "#FFF4F4F5";
         StateText  = s ? "已连接" : "未连接";
         OnPropertyChanged(nameof(StateColor));
         OnPropertyChanged(nameof(StateText));
         OnLog?.Invoke(s ? $"[{Config.Vision.IP}:{Config.Vision.Port}] 已连接" : $"[{Config.Vision.IP}:{Config.Vision.Port}] 连接失败,尝试重新连接...");
     };
     visionClient.Connect();
 }
Esempio n. 5
0
        protected override void Run()
        {
            bool   foundService   = false;
            string requestMessage = "mpos_serv_req_wp:" + appName + ":" + appVersion;

            while (running && !foundService)
            {
                Debug.WriteLine("#> [DiscoveryService]: Started Discovery Service for endpoint: " + server.Type + " and app: " + appName + "/" + appVersion);
                foundService = true;

                ClientTcp socket = null;
                try
                {
                    socket = new ClientTcp(256, false);
                    if (server.Type == EndpointType.SECONDARY_SERVER && !MposFramework.Instance.DeviceController.IsOnline())
                    {
                        throw new ConnectException("The mobile is completly offline!");
                    }

                    socket.Connect(server.Ip, servicePort);
                    Sent(socket, requestMessage);
                    Receive(socket);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("## [DiscoveryService]: Error -> \n" + e.ToString());
                    foundService = false;
                }
                finally
                {
                    if (running && !foundService)
                    {
                        Debug.WriteLine(">> [DiscoveryService]: Retry Discovery Service for endpoint: " + server.Type + ", in " + EndpointController.REPEAT_DISCOVERY_TASK + " ms");
                        Thread.Sleep(EndpointController.REPEAT_DISCOVERY_TASK);
                    }
                    else
                    {
                        Debug.WriteLine("#> [DiscoveryService]: Finished Discovery Service for endpoint: " + server.Type + " on " + server.Ip);
                        MposFramework.Instance.EndpointController.StartDecisionMaker(server);
                    }

                    socket.Close();
                }
            }
        }
Esempio n. 6
0
        protected override void Run()
        {
            Debug.WriteLine("#> [DeployRpcService]: Started Deploy App " + appName + "/" + appVersion + " on Remote Server (" + server.Ip + ":" + server.DeployAppPort + ")");
            ClientTcp socket = null;

            //async programming, but run synch mode
            task = new DependenceFileLoadTask();
            task.RunSynchronous();

            try
            {
                socket = new ClientTcp(false);
                socket.Connect(server.Ip, server.DeployAppPort);

                string deployRequest = "mpos_deploy_app:" + appName + ":" + appVersion;
                //Debug.WriteLine("[DEBUG]: sent -> " + deployRequest);
                Sent(socket, deployRequest);
                if (!ReceiveFeedbackMessage(socket))
                {
                    throw new NetworkException("Problems in deploy request");
                }

                string mposDependence = "mpos_dependence_size:" + task.dependenceFiles.Count;
                //Debug.WriteLine("[DEBUG]: sent -> " + mposDependence);
                Sent(socket, mposDependence);
                if (!ReceiveFeedbackMessage(socket))
                {
                    throw new NetworkException("Problems in deploy request");
                }

                SentFiles(socket);
                server.RpcServicePort = ReceiveServicePort(socket);
            }
            catch (Exception e)
            {
                Debug.WriteLine("## [DeployRpcService]: Any problem in I/O processing!\n" + e.ToString());
            }
            finally
            {
                Debug.WriteLine("#> [DeployRpcService]: Finished Deploy App " + appName + "/" + appVersion + " on Remote Server port: " + server.RpcServicePort);
                socket.Close();
            }
        }
Esempio n. 7
0
        public object Call(bool needProfile, bool manualSerialization, Object objOriginal, String methodName, object[] methodParams)
        {
            ClientTcp socket = null;

            if (server == null)
            {
                throw new NetworkException("Need to setup any server for use the RPC Client");
            }

            try
            {
                socket = new ClientTcp();
                socket.Connect(server.Ip, server.RpcServicePort);

                Sent(socket, manualSerialization, needProfile, objOriginal, methodName, methodParams);
                ResponseRemotable response = Receive(socket, objOriginal, methodName);

                if (response.Code == Code.OK)
                {
                    return(response.MethodReturn);
                }
                else if (response.Code == Code.METHOD_THROW_ERROR)
                {
                    throw new RpcException("[Server]: Remote method thrown some errors\n" + response.Except);
                }
                else
                {
                    throw new RpcException("[Server]: RPC call terminated with some errors!\n" + response.Except);
                }
            }
            catch (SocketException e)
            {
                throw new ConnectException("Failed to connect to server: " + server.Ip + ":" + server.RpcServicePort, e);
            }
            finally
            {
                socket.Close();
            }
        }
Esempio n. 8
0
 public void Start()
 {
     clientTcp.Connect(ipAddress, port);
     clientTcp.Message  += ClientTcpOnMessage;
     clientTcp.Selector += Selector;
 }