Exemplo 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
            });
        }
 /// <summary>
 /// When TCPConnectionListener handles a connection, it is taken by this method to create and
 /// register it's neccessary events and to add it to _communicators collection.
 /// This method is also starts the communicator.
 /// </summary>
 /// <param name="sender">Sending object</param>
 /// <param name="e">Event informations</param>
 private void ConnectionListener_ClientConnected(object sender, TCPClientConnectedEventArgs e)
 {
     try
     {
         OnCommunicatorConnected(new TCPCommunicator(e.ClientSocket, CommunicationLayer.CreateCommunicatorId()));
     }
     catch (Exception ex)
     {
         Logger.Error(ex.Message, ex);
     }
 }