示例#1
0
        public IrDAEndPoint(byte[] irdaDeviceID, string serviceName)
        {
            if (irdaDeviceID == null)
            {
                throw new ArgumentNullException("irdaDeviceID");
            }

            this.id      = new IrDAAddress(irdaDeviceID);
            this.service = serviceName ?? throw new ArgumentNullException("serviceName");
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        int IComparable.CompareTo(object obj)
        {
            IrDAAddress irda = obj as IrDAAddress;

            if (irda != null)
            {
                return(ToInt32().CompareTo(irda.ToInt32()));
            }

            return(-1);
        }
示例#3
0
        /// <summary>
        /// Compares two <see cref="IrDAAddress"/> instances for equality.
        /// </summary>
        /// -
        /// <param name="obj">The <see cref="IrDAAddress"/>
        /// to compare with the current instance.
        /// </param>
        /// -
        /// <returns><c>true</c> if <paramref name="obj"/>
        /// is a <see cref="IrDAAddress"/> and equal to the current instance;
        /// otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            IrDAAddress ira = obj as IrDAAddress;

            if (ira != null)
            {
                return(this == ira);
            }

            return(base.Equals(obj));
        }
示例#4
0
		/// <summary>
		/// Initializes a new instance of the <see cref="IrDAEndPoint"/> class.
		/// </summary>
		/// <param name="irdaDeviceAddress">The device address.</param>
        /// <param name="serviceName">The Service Name to connect to/listen on eg "<c>OBEX</c>".
        /// In the very uncommon case where a connection is to be made to
        /// / a server is to listen on 
        /// a specific LSAP-SEL (port number), then use 
        /// the form "<c>LSAP-SELn</c>", where n is an integer.
        /// </param>
        public IrDAEndPoint(IrDAAddress irdaDeviceAddress, string serviceName)
		{
            if (irdaDeviceAddress == null) {
                throw new ArgumentNullException("irdaDeviceAddress");
            }
            if (serviceName == null) {
                throw new ArgumentNullException("serviceName");
            }
            //
            this.id = irdaDeviceAddress;
			this.service = serviceName;
        }
示例#5
0
		public IrDAEndPoint(byte[] irdaDeviceID, string serviceName)
		{
            if (irdaDeviceID == null) {
                throw new ArgumentNullException("irdaDeviceID");
            }
            if (serviceName == null) {
                throw new ArgumentNullException("serviceName");
            }
            //
			this.id = new IrDAAddress(irdaDeviceID);
			this.service = serviceName;
		}
示例#6
0
 public static bool TryParse(string s, out IrDAAddress result)
 {
     try
     {
         result = Parse(s);
         return(true);
     }
     catch
     {
         result = null;
         return(false);
     }
 }
示例#7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IrDAEndPoint"/> class.
 /// </summary>
 /// <param name="irdaDeviceAddress">The device address.</param>
 /// <param name="serviceName">The Service Name to connect to/listen on eg "<c>OBEX</c>".
 /// In the very uncommon case where a connection is to be made to
 /// / a server is to listen on
 /// a specific LSAP-SEL (port number), then use
 /// the form "<c>LSAP-SELn</c>", where n is an integer.
 /// </param>
 public IrDAEndPoint(IrDAAddress irdaDeviceAddress, string serviceName)
 {
     if (irdaDeviceAddress == null)
     {
         throw new ArgumentNullException("irdaDeviceAddress");
     }
     if (serviceName == null)
     {
         throw new ArgumentNullException("serviceName");
     }
     //
     this.id      = irdaDeviceAddress;
     this.service = serviceName;
 }
示例#8
0
        internal static ObexTransport GetObexTransportFromHost(string hostname)
        {
            if (string.IsNullOrEmpty(hostname))
            {
                return(ObexTransport.Unknown);
            }

            if (IrDAAddress.TryParse(hostname, out var address))
            {
                return(ObexTransport.IrDA);
            }
            else if (BluetoothAddress.TryParse(hostname, out BluetoothAddress ba))
            {
                return(ObexTransport.Bluetooth);
            }
            else if (IPAddress.TryParse(hostname, out IPAddress ipAddress))
            {
                return(ObexTransport.Tcp);
            }

            return(ObexTransport.Unknown);
        }
示例#9
0
        /// <summary>
        /// Converts the string representation of an IrDA address to a new <see cref="IrDAAddress"/> instance.
        /// </summary>
        /// <param name="irdaString">A string containing an address to convert.</param>
        /// <returns>New <see cref="IrDAAddress"/> instance.</returns>
        /// <remarks>Address must be specified in hex format optionally separated by the colon or period character e.g. 00000000, 00:00:00:00 or 00.00.00.00.</remarks>
        /// <exception cref="T:System.ArgumentNullException">irdaString is null.</exception>
        /// <exception cref="T:System.FormatException">irdaString is not a valid IrDA address.</exception>
        public static IrDAAddress Parse(string irdaString)
        {
            if (irdaString == null)
            {
                throw new ArgumentNullException("irdaString");
            }

            IrDAAddress ia;

            if (irdaString.IndexOf(":", StringComparison.Ordinal) > -1)
            {
                // assume address in colon separated hex format 00:00:00:00
                // check length
                if (irdaString.Length != 11)
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

                byte[] iabytes = new byte[AddressBytesLength];
                // split on colons
                string[] sbytes = irdaString.Split(':');
                for (int ibyte = 0; ibyte < iabytes.Length; ibyte++)
                {
                    // parse hex byte in reverse order
                    iabytes[ibyte] = byte.Parse(sbytes[3 - ibyte], NumberStyles.HexNumber);
                }

                ia = new IrDAAddress(iabytes);
            }
            else if (irdaString.IndexOf(".", StringComparison.Ordinal) > -1)
            {
                // assume address in uri hex format 00.00.00.00
                // check length
                if (irdaString.Length != 11)
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

                byte[] iabytes = new byte[AddressBytesLength];
                // split on colons
                string[] sbytes = irdaString.Split('.');
                for (int ibyte = 0; ibyte < iabytes.Length; ibyte++)
                {
                    // parse hex byte in reverse order
                    iabytes[ibyte] = byte.Parse(sbytes[3 - ibyte], NumberStyles.HexNumber, CultureInfo.InvariantCulture);
                }

                ia = new IrDAAddress(iabytes);
            }
            else
            {
                // assume specified as integer
                // check length
                if ((irdaString.Length < 4) | (irdaString.Length > 8))
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

                ia = new IrDAAddress(int.Parse(irdaString, NumberStyles.HexNumber, CultureInfo.InvariantCulture));
            }

            return(ia);
        }
示例#10
0
        private ObexStatusCode Connect()
        {
            if (!connected)
            {
                if (ns == null)
                {
                    try {
#if NETCF
                        // Will be set to false in some Bluetooth cases (e.g. Widcomm and Bluetopia)...
                        bool isWinCeSockets = true;
#endif
                        if (uri.Host.Length == 0)
                        {
                            System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream != null);
                            System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream.CanRead &&
                                                            m_alreadyConnectedObexStream.CanWrite);
                            ns = m_alreadyConnectedObexStream;
                        }
                        else
                        {
                            BluetoothAddress ba;
                            IrDAAddress      ia;
                            if (BluetoothAddress.TryParse(uri.Host, out ba))
                            {
                                // No good on Widcomm! s = new Socket(AddressFamily32.Bluetooth, SocketType.Stream, BluetoothProtocolType.RFComm);
                                BluetoothClient cli;
                                if (_btFactory == null)
                                {
                                    cli = new BluetoothClient();
                                }
                                else
                                {
                                    cli = _btFactory.CreateBluetoothClient();
                                }
                                Guid serviceGuid;

                                switch (uri.Scheme)
                                {
                                case SchemeNames.Ftp:
                                    serviceGuid = BluetoothService.ObexFileTransfer;
                                    break;

                                //potential for other obex based profiles to be added
                                case SchemeNames.Sync:
                                    serviceGuid = BluetoothService.IrMCSyncCommand;
                                    break;

                                case SchemeNames.Pbap:
                                    serviceGuid = BluetoothService.PhonebookAccessPse;
                                    break;

                                default:
                                    serviceGuid = BluetoothService.ObexObjectPush;
                                    break;
                                }


                                BluetoothEndPoint bep = new BluetoothEndPoint(ba, serviceGuid);
                                cli.Connect(bep);
                                ns = cli.GetStream();
#if NETCF
                                try {
                                    Socket tmp = cli.Client; // Attempt to get the Socket
                                    Debug.Assert(isWinCeSockets);
                                } catch (NotSupportedException) {
                                    isWinCeSockets = false;
                                }
#endif
                            }
                            else if (IrDAAddress.TryParse(uri.Host, out ia))
                            {
                                //irda
                                s = new Socket(AddressFamily.Irda, SocketType.Stream, ProtocolType.IP);

                                IrDAEndPoint iep = new IrDAEndPoint(ia, "OBEX");

                                s.Connect(iep);
                            }
                            else
                            {
                                //assume a tcp host
                                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                                IPAddress ipa;
                                try {
                                    ipa = IPAddress.Parse(uri.Host);
                                } catch {
                                    // Compile-time: warning CS0618: 'System.Net.Dns.Resolve(string)'
                                    //    is obsolete: 'Resolve is obsoleted for this type,
                                    //    please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202'
                                    // However GetHostEntry isn't supported on NETCFv1,
                                    // so just keep it and disable the warning on
                                    // the other platforms.
#if V1
/*
 #endif
 #pragma warning disable 618
 #if V1
 */
#endif
                                    ipa = System.Net.Dns.Resolve(uri.Host).AddressList[0];
#if V1
/*
 #endif
 #pragma warning restore 618
 #if V1
 */
#endif
                                }

                                IPEndPoint ipep = new IPEndPoint(ipa, 650);

                                s.Connect(ipep);
                            }

                            if (ns == null) // BluetoothClient used above
                            {
                                ns = new NetworkStream(s, true);
                            }

#if NETCF
                            if (isWinCeSockets)
                            {
                                // Winsock on WinCE does _not_ support timeouts!
                                ns = new InTheHand.Net.Bluetooth.Factory.TimeoutDecorStream(ns);
                            }
#endif
                            // Timeout
                            ns.ReadTimeout  = timeout;
                            ns.WriteTimeout = timeout;
                        }

                        return(Connect_Obex());
                    } finally {
                        if (s != null && !s.Connected)
                        {
                            s = null;
                        }
                    }
                }
            }
            Debug.Fail("Don't know that we every get here (Connect when connected).");
            return((ObexStatusCode)0);
        }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IrDAEndPoint"/> class.
 /// </summary>
 /// <param name="irdaDeviceAddress">The device address.</param>
 /// <param name="serviceName">The Service Name to connect to/listen on eg "<c>OBEX</c>".
 /// In the very uncommon case where a connection is to be made to
 /// / a server is to listen on
 /// a specific LSAP-SEL (port number), then use
 /// the form "<c>LSAP-SELn</c>", where n is an integer.
 /// </param>
 public IrDAEndPoint(IrDAAddress irdaDeviceAddress, string serviceName)
 {
     this.id      = irdaDeviceAddress ?? throw new ArgumentNullException("irdaDeviceAddress");
     this.service = serviceName ?? throw new ArgumentNullException("serviceName");
 }
示例#12
0
 /// <summary>
 /// Converts the string representation of an address to it's <see cref="IrDAAddress"/> equivalent.
 /// A return value indicates whether the operation succeeded.
 /// </summary>
 /// <param name="s">A string containing an address to convert.</param>
 /// <param name="result">When this method returns, contains the <see cref="IrDAAddress"/> equivalent to the address contained in s, if the conversion succeeded, or null (Nothing in Visual Basic) if the conversion failed.
 /// The conversion fails if the s parameter is null or is not of the correct format.</param>
 /// <returns>true if s is a valid IrDA address; otherwise, false.</returns>
 public static bool TryParse(string s, out IrDAAddress result)
 {
     try
     {
         result = Parse(s);
         return true;
     }
     catch
     {
         result = null;
         return false;
     }
 }
示例#13
0
		/// <summary>
		/// Converts the string representation of an IrDA address to a new <see cref="IrDAAddress"/> instance.
		/// </summary>
        /// <param name="irdaString">A string containing an address to convert.</param>
		/// <returns>New <see cref="IrDAAddress"/> instance.</returns>
		/// <remarks>Address must be specified in hex format optionally separated by the colon or period character e.g. 00000000, 00:00:00:00 or 00.00.00.00.</remarks>
        /// <exception cref="ArgumentNullException">irdaString is null.</exception>
        /// <exception cref="FormatException">irdaString is not a valid IrDA address.</exception>
        public static IrDAAddress Parse(string irdaString)
		{
            if (irdaString == null)
			{
				throw new ArgumentNullException("irdaString");
			}

			IrDAAddress ia;

            if (irdaString.IndexOf(":") > -1)
			{
				//assume address in colon separated hex format 00:00:00:00
                //check length
                if (irdaString.Length != 11)
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

				byte[] iabytes = new byte[AddressBytesLength];
				//split on colons
                string[] sbytes = irdaString.Split(':');
				for(int ibyte = 0; ibyte < iabytes.Length; ibyte++)
				{
					//parse hex byte in reverse order
					iabytes[ibyte] = byte.Parse(sbytes[3 - ibyte],System.Globalization.NumberStyles.HexNumber);
				}
                ia = new IrDAAddress(iabytes);
			}
            else if (irdaString.IndexOf(".") > -1)
			{
				//assume address in uri hex format 00.00.00.00
                //check length
                if (irdaString.Length != 11)
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

                byte[] iabytes = new byte[AddressBytesLength];
                //split on colons
                string[] sbytes = irdaString.Split('.');
				for(int ibyte = 0; ibyte < iabytes.Length; ibyte++)
				{
					//parse hex byte in reverse order
					iabytes[ibyte] = byte.Parse(sbytes[3 - ibyte],System.Globalization.NumberStyles.HexNumber);
				}
                ia = new IrDAAddress(iabytes);
            }
			else
			{
				//assume specified as integer
                //check length
                if ((irdaString.Length < 4) | (irdaString.Length > 8))
                {
                    throw new FormatException("irdaString is not a valid IrDA address.");
                }

                ia = new IrDAAddress(int.Parse(irdaString, System.Globalization.NumberStyles.HexNumber));
			}

			return ia;
        }
示例#14
0
        private ObexStatusCode Connect()
        {
            if (!connected)
            {
                if (ns == null)
                {
                    try {
                        if (uri.Host.Length == 0)
                        {
                            System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream != null);
                            System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream.CanRead &&
                                                            m_alreadyConnectedObexStream.CanWrite);
                            ns = m_alreadyConnectedObexStream;
                        }
                        else
                        {
                            if (IrDAAddress.TryParse(uri.Host, out var address))
                            {
                                IrDAClient client = new IrDAClient();

                                string serviceName;

                                switch (uri.Scheme)
                                {
                                //potential for other obex based profiles to be added

                                default:
                                    serviceName = IrDAService.ObjectExchange;
                                    break;
                                }

                                client.Connect(new IrDAEndPoint(address, serviceName));

                                if (!client.Connected)
                                {
                                    return(ObexStatusCode.NotFound);
                                }

                                ns = client.GetStream();
                            }
                            else if (BluetoothAddress.TryParse(uri.Host, out BluetoothAddress ba))
                            {
                                BluetoothClient cli = new BluetoothClient();
                                Guid            serviceGuid;

                                switch (uri.Scheme)
                                {
                                case SchemeNames.Ftp:
                                    serviceGuid = BluetoothService.ObexFileTransfer;
                                    break;

                                //potential for other obex based profiles to be added
                                case SchemeNames.Sync:
                                    serviceGuid = BluetoothService.IrMCSyncCommand;
                                    break;

                                case SchemeNames.Pbap:
                                    serviceGuid = BluetoothService.PhonebookAccessPse;
                                    break;

                                case SchemeNames.Map:
                                    serviceGuid = BluetoothService.MessageAccessProfile;
                                    break;

                                default:
                                    serviceGuid = BluetoothService.ObexObjectPush;
                                    break;
                                }

                                cli.Connect(ba, serviceGuid);

                                if (!cli.Connected)
                                {
                                    return(ObexStatusCode.NotFound);
                                }

                                ns = cli.GetStream();
                            }
                            else
                            {
                                //assume a tcp host
                                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                                IPAddress ipa       = null;
                                var       addresses = System.Net.Dns.GetHostAddresses(uri.Host);
                                if (addresses.Length > 0)
                                {
                                    ipa = addresses[0];
                                }
                                else
                                {
                                    throw new WebException("Host not recognised", WebExceptionStatus.NameResolutionFailure);
                                }

                                IPEndPoint ipep = new IPEndPoint(ipa, 650);

                                s.Connect(ipep);
                            }

                            if (ns == null) // BluetoothClient used above
                            {
                                ns = new System.Net.Sockets.NetworkStream(s, true);
                            }

                            // Timeout
                            //ns.ReadTimeout = timeout;
                            //ns.WriteTimeout = timeout;
                        }

                        return(Connect_Obex());
                    } finally {
                        if (s != null && !s.Connected)
                        {
                            s = null;
                        }
                    }
                }
            }
            Debug.Fail("Don't know that we every get here (Connect when connected).");
            return((ObexStatusCode)0);
        }