Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
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
                        {
                            BluetoothAddress ba;

                            if (BluetoothAddress.TryParse(uri.Host, out 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);
                                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);
        }
Beispiel #4
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
                        {
                            BluetoothAddress ba;

                            if (BluetoothAddress.TryParse(uri.Host, out 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;

                                default:
                                    serviceGuid = BluetoothService.ObexObjectPush;
                                    break;
                                }

                                cli.Connect(ba, serviceGuid);
                                ns = cli.GetStream();
                            }
                            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.

#pragma warning disable 618
                                    ipa = System.Net.Dns.Resolve(uri.Host).AddressList[0];
#pragma warning restore 618
                                }

                                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);
        }