예제 #1
0
        protected override CommunicationStatuses InnerConnect(IDataSource ds)
        {
            CommunicationStatuses ret = CommunicationStatuses.Busy;

            DataSource = ds;
            _isOpen    = true;
            ret        = CommunicationStatuses.Done;
            "{0:X2} {1} {2}@{3} {4}"._DLOG(SessionId, ApiType, ds.SourceName, ds.Args, ret);
            return(ret);
        }
예제 #2
0
 protected override CommunicationStatuses InnerConnect(IDataSource dataSource)
 {
     InnerDisconnect();
     lock (_lockObject)
     {
         CommunicationStatuses ret = CommunicationStatuses.Busy;
         DataSource = dataSource;
         if (dataSource != null &&
             dataSource is SocketDataSource &&
             dataSource.Validate()
             )
         {
             _dataSource    = (SocketDataSource)dataSource;
             _tcpConnection = new TcpConnection();
             int attempts = 1;
             while (!_tcpConnection.Connect(_dataSource.SourceName, _dataSource.Port) &&
                    attempts > 0
                    )
             {
                 attempts--;
                 Thread.Sleep(100);
                 "{0:X2} {1} {2} {3}"._DLOG(SessionId, ApiType, _dataSource, _tcpConnection.Connected);
             }
             if (_tcpConnection.Connected)
             {
                 "{0:X2} {1} {2} {3}"._DLOG(SessionId, ApiType, _dataSource, _tcpConnection.Connected);
                 ret       = CommunicationStatuses.Done;
                 _isStoped = false;
                 if (Connected != null)
                 {
                     Connected(this);
                 }
                 _workerThread              = new Thread(WorkerThreadMethod);
                 _workerThread.Name         = "Tcp Client";
                 _workerThread.IsBackground = true;
                 _workerThread.Start();
             }
         }
         return(ret);
     }
 }
예제 #3
0
        // Create connection.
        public CommunicationStatuses Connect(IDataSource dataSource)
        {
            CommunicationStatuses ret = CommunicationStatuses.Failed;

            if (dataSource == null)
            {
                throw new ArgumentNullException("dataSource");
            }

            if (!dataSource.Validate())
            {
                throw new ArgumentException("Not valid dataSource");
            }

            if (DataSource == null || !DataSource.Equals(dataSource))
            {
                DataSource = dataSource;
            }

            ret = InnerConnect(dataSource);
            return(ret);
        }
예제 #4
0
        protected override CommunicationStatuses InnerConnect(IDataSource ds)
        {
            InnerDisconnect();
            lock (_lockObject)
            {
                CommunicationStatuses ret = CommunicationStatuses.Busy;
                DataSource = ds;
                IPAddress ipAddress;

                if (ds != null &&
                    ds is SocketDataSource &&
                    IPAddress.TryParse(ds.SourceName, out ipAddress)
                    )
                {
                    _dataSource     = (SocketDataSource)ds;
                    _remoteEndPoint = new IPEndPoint(IPAddress.Parse(ds.SourceName), _dataSource.Port);
                    if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        if (Socket.OSSupportsIPv6)
                        {
                            int _localPort = GetAvailablePort(4000);
                            //IPEndPoint DsEnpoint =
                            //    new IPEndPoint(IPAddress.Parse("fd00:1111::10"), _localPort); //LAN IP address connected to the portal -- how to not hadcode this?

                            IPAddress  RemoteIP       = IPAddress.Parse(ds.SourceName);
                            IPEndPoint RemoteEndPoint = new IPEndPoint(RemoteIP, 4123);

                            Socket socket = null;
                            if (RemoteIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                            {
                                socket = new Socket(
                                    AddressFamily.InterNetworkV6,
                                    SocketType.Dgram,
                                    ProtocolType.Udp);
                            }
                            else
                            {
                                socket = new Socket(
                                    AddressFamily.InterNetwork,
                                    SocketType.Dgram,
                                    ProtocolType.Udp);
                            }
                            IPEndPoint localEndPoint = QueryRoutingInterface(socket, RemoteEndPoint);

                            IPEndPoint DsEnpoint =
                                new IPEndPoint(localEndPoint.Address, _localPort); //LAN IP address connected to the portal -- how to not hadcode this?

                            try
                            {
                                _port = new UdpClient(AddressFamily.InterNetworkV6);
                                _port.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true);
                                _port.ExclusiveAddressUse = true;
                                _port.Client.Bind(DsEnpoint);
                            }
                            catch (Exception e)
                            {
                                throw e;
                            }
                        }
                    }
                    else
                    {
                        _port = new UdpClient(AddressFamily.InterNetwork);
                    }
                    "{0:X2} {1} {2}@{3} {4}"._DLOG(SessionId, ApiType, _dataSource.SourceName, _dataSource.Port, _port.Client.Connected);
                    try
                    {
                        int attempts = 10;
                        while (!_port.Client.Connected && attempts > 0)
                        {
                            attempts--;
                            Thread.Sleep(100);
                            _port.Connect(_dataSource.SourceName, 4123); //harcoded again!!!!!!
                            "{0:X2} {1} {2}@{3} {4}"._DLOG(SessionId, ApiType, _dataSource.SourceName, _dataSource.Port, _port.Client.Connected);
                        }
                        if (_port.Client.Connected)
                        {
                            ret       = CommunicationStatuses.Done;
                            _isStoped = false;
                            if (Connected != null)
                            {
                                Connected(this);
                            }
                            _threadWorker              = new Thread(DoWork);
                            _threadWorker.Name         = "Udp Client";
                            _threadWorker.IsBackground = true;
                            _threadWorker.Start();
                        }
                    }
                    catch (IOException ex)
                    {
                        ex.Message._EXLOG();
                    }
                }
                return(ret);
            }
        }