/// <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
            });
        }
Beispiel #2
0
        /// <summary>
        /// Connects to NGRID server.
        /// </summary>
        public void Connect()
        {
            ChangeState(CommunicationStates.Connecting);

            try
            {
                var ip = IPAddress.Parse(_ipAddress);

                _socket = GeneralHelper.ConnectToServerWithTimeout(new IPEndPoint(ip, _port), 5000); //5 seconds
                if (!_socket.Connected)
                {
                    throw new NGRIDException("TCP connection can not be established.");
                }

                _socket.NoDelay = true;
                //Create stream object to read/write from/to socket.
                _networkStream = new NetworkStream(_socket);

                //Create and start a new thread to communicate with NGRID server
                _thread = new Thread(DoCommunicateAsThread);
                _thread.Start();

                ChangeState(CommunicationStates.Connected);
            }
            catch (Exception)
            {
                ChangeState(CommunicationStates.Closed);
                throw;
            }
        }