Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new TCPCommunicator to communicate with MDS server.
        /// </summary>
        private void ConnectToServer()
        {
            var ip = IPAddress.Parse(_ipAddress);
            if (ip == null)
            {
                throw new MDSException("IP address is not valid: " + _ipAddress);
            }

            var socket = GeneralHelper.ConnectToServerWithTimeout(new IPEndPoint(ip, _port), 10000); //10 seconds
            if (!socket.Connected)
            {
                throw new MDSException("TCP connection can not be established.");
            }

            //Create communicator object.
            _reconnectingCommunicator = new TCPCommunicator(socket, CommunicationLayer.CreateCommunicatorId());

            //Register MessageReceived event to receive response of Register message
            _reconnectingCommunicator.MessageReceived += Communicator_MessageReceived;

            //Start communicator and send a register message
            _reconnectingCommunicator.Start();
            _reconnectingCommunicator.SendMessage(new MDSRegisterMessage
                                               {
                                                   CommunicationWay = CommunicationWays.SendAndReceive,
                                                   CommunicatorType = CommunicatorTypes.MdsServer,
                                                   Name = Settings.ThisServerName,
                                                   Password = "" //Not implemented yet
                                               });
        }