GPRS连接类

本文介绍了一个用于管理GPRS连接的类GPRSConnection,该类通过设置连接参数来建立和释放GPRS连接,支持HTTP代理及不同连接优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     public class GPRSConnection
    {
        const int S_OK = 0;
        const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
        const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;                       //使用CMNET连接
        const uint CONNMGR_FLAG_PROXY_WAP = 0x2;                        //使用CMWAP连接
        const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
        const uint INFINITE = 0xffffffff;
        const uint CONNMGR_STATUS_CONNECTED = 0x10;
        static Hashtable ht = new Hashtable();
        static GPRSConnection()
        {
            System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
            mre.Handle = ConnMgrApiReadyEvent();
            mre.WaitOne();
            CloseHandle(mre.Handle);
        }
        ~GPRSConnection()
        {
            ReleaseAll();
        }
        public static bool Setup(Uri url)
        {
            return Setup(url.ToString());
        }
        public static bool Setup(string urlStr)
        {
            ConnectionInfo ci = new ConnectionInfo();
            IntPtr phConnection = IntPtr.Zero;
            uint status = 0;
            if (ht[urlStr] != null) return true;
            if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK) return false;
            ci.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(ci);
            ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;
            ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
            ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
            ci.bExclusive = 0;
            ci.bDisabled = 0;
            ci.hWnd = IntPtr.Zero;
            ci.uMsg = 0;
            ci.lParam = 0;
            if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status) != S_OK && status != CONNMGR_STATUS_CONNECTED)
                return false;
            ht[urlStr] = phConnection; return true;
        }
        public static bool Release(Uri url)
        {
            return Release(url.ToString());
        }
        public static bool Release(string urlStr)
        {
            return Release(urlStr, true);
        }
        private static bool Release(string urlStr, bool removeNode)
        {
            bool res = true;
            IntPtr ph = IntPtr.Zero;
            if (ht[urlStr] == null) return true;
            ph = (IntPtr)ht[urlStr];
            if (ConnMgrReleaseConnection(ph, 1) != S_OK) res = false;
            CloseHandle(ph);
            if (removeNode) ht.Remove(urlStr);
            return res;
        }
        public static void ReleaseAll()
        {
            foreach (DictionaryEntry de in ht)
            {
                Release((string)de.Key, false);
            }
            ht.Clear();
        }
        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct ConnectionInfo
        {
            public uint cbSize;
            public uint dwParams;
            public uint dwFlags;
            public uint dwPriority;
            public int bExclusive;
            public int bDisabled;
            public Guid guidDestNet;
            public IntPtr hWnd;
            public uint uMsg;
            public uint lParam;
            public uint ulMaxCost;
            public uint ulMinRcvBw;
            public uint ulMaxConnLatency;
        }
       
        [System.Runtime.InteropServices.DllImport("cellcore.dll")]
        private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex);
        [System.Runtime.InteropServices.DllImport("cellcore.dll")]
        private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
        [System.Runtime.InteropServices.DllImport("cellcore.dll")]
        private static extern IntPtr ConnMgrApiReadyEvent();
        [System.Runtime.InteropServices.DllImport("cellcore.dll")]
        private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        private static extern int CloseHandle(IntPtr hObject);

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值