C# 实现Wince 建立网络连接的类实现

来源:互联网 发布:淘宝买苹果6美版靠谱吗 编辑:程序博客网 时间:2024/05/17 01:47
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using PAMS2CE.UTIL;

namespace PAMS2CE.UTIL
{
    #region enum
    /// <summary>
    /// ConnectType
    /// </summary>
    public enum ConnType
    {
        Offline = -1,
        Gprs,
        Wlan
    }

    /// <summary>
    /// ConnState
    /// </summary>
    public enum ConnState
    {
        Connecting = 2,
        ConnectSuccess = 1,
        NoConnect = 0
    }
    #endregion

    #region ConnectNotifyEvent
    /// <summary>
    /// ConnectNotifyEventArgs
    /// </summary>
    public class ConnectNotifyEventArgs : EventArgs
    {
        private ConnState connectResult;

        private ConnType connectType;

        public ConnState ConnectResult
        {
            get { return connectResult; }
        }

        public ConnType ConnectType
        {
            get { return connectType; }
        }

        public ConnectNotifyEventArgs(ConnState connectResult, ConnType connectType)
        {
            this.connectResult = connectResult;
            this.connectType = connectType;
        }
    }

    /// <summary>
    /// ConnectNotifyEvent delegate
    /// </summary>
    /// <param name="sender">sender</param>
    /// <param name="e">ConnectNotifyEventArgs</param>
    public delegate void ConnectNotifyEventHandler(object sender, ConnectNotifyEventArgs e);
    #endregion

    public class NetworkManager
    {
        private Thread NetConnThread = null; //连接网络的线程

        public enum NetImageType
        {
            NoConnect = 0,
            Connecting = 1,
            WlanConnected = 2,
            GprsConnected = 3
        }

        public enum SignalImageType
        {
            None = 0,
            Low = 1,
            Normal = 2,
            Good = 3,
            Strong = 4,
        }


        #region Event
        public event ConnectNotifyEventHandler ConnectNotify;

        protected virtual void OnConnectNotify(ConnectNotifyEventArgs e)
        {
            if (ConnectNotify != null)
            {
                ConnectNotify(this, e);
            }
        }

        #endregion

        #region Variable
        IntPtr[] hEvent = new IntPtr[2];

        const string gprsName = "GPRS";
        #endregion

        #region Property

        /// <summary>
        /// ConnectType
        /// </summary>
        public static ConnType ConnectType = ConnType.Offline;

        /// <summary>
        /// IsConnecting
        /// </summary>
        public static bool IsConnecting = false;

        /// <summary>
        /// GprsName
        /// </summary>
        public static string GprsName
        {
            get { return gprsName; }
        }

        #endregion

        #region Structure

        public NetworkManager()
        {
            for (int i = 0; i < 2; i++)
            {
                hEvent[i] = Win32Util.CreateEvent(IntPtr.Zero, false, false, null);

            }
        }

        ~NetworkManager()
        {
            for (int i = 0; i < 2; i++)
            {
                Win32Util.CloseHandle(hEvent[i]);
            }
        }

        #endregion

        #region Method
        /// <summary>
        /// Start NetworkManagement Thread
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            NetConnThread  = new Thread(new ThreadStart(this.NetworkThreadPorc));
            NetConnThread.Start();
            return true;
        }

        /// <summary>
        /// Stop NetworkManagement Thread
        /// </summary>
        public void Stop()
        {
            if (NetConnThread != null)
            {
                NetConnThread.Abort();
                Thread.Sleep(500);
            }
            Win32Util.EventModify(hEvent[0], Win32Util.EVENT_SET);
        }

        /// <summary>
        /// Send Connect Request
        /// </summary>
        /// <param name="type"></param>
        public void Connect(ConnType type)
        {
            NetworkManager.ConnectType = type;
            Win32Util.EventModify(hEvent[1], Win32Util.EVENT_SET);
        }

        /// <summary>
        /// NetworkThreadPorc
        /// </summary>
        private void NetworkThreadPorc()
        {
            while (true)
            {
                //wait for 3 minute if not connect request.
                //the thread will auto maintenance the network state.
                //uint evt = Win32Util.WaitForMultipleObjects(2, hEvent, false, 3 * 60 * 1000);
                uint evt = Win32Util.WaitForMultipleObjects(2, hEvent, false, 5 * 1000);
                if (evt == 0)//return the thread
                {
                    return;
                }
                else
                {
                    IsConnecting = true;

                    //fire the ConnectNotify Event
                    OnConnectNotify(new ConnectNotifyEventArgs(ConnState.Connecting, ConnectType));

                    //if the connect request then disable the no need Network.
                    if (evt == 1)
                    {
                        switch (ConnectType)
                        {
                            case ConnType.Offline:
                                Device.DisConnectGprs(gprsName);
                                Device.DisableGprs();
                                Device.DisableWlan();
                                break;
                            case ConnType.Gprs:
                                Device.DisableWlan();
                                break;
                            case ConnType.Wlan:
                                Device.DisConnectGprs(gprsName);
                                Device.DisableGprs();
                                break;
                        }
                    }

                    ConnState res = ConnState.ConnectSuccess;
                    if (!Device.CheckNetworkStat())
                    {
                        res = ConnState.NoConnect;

                        switch (ConnectType)
                        {
                            case ConnType.Gprs:
                                if (Device.EnableGprs())
                                {
                                    if (Device.GetGprsStatus(gprsName))
                                    {
                                        Device.DisConnectGprs(gprsName);
                                    }

                                    if (Device.ConnectGprs(gprsName))
                                    {
                                        res = ConnState.ConnectSuccess;
                                        Thread.Sleep(5000);
                                    }
                                }
                                break;
                            case ConnType.Wlan:

                                bool ret;
                                if (!Device.GetWlanPowerStatus())//判断无线模块是否已经开启,true:开启
                                {
                                    ret = Device.EnableWlan();
                                }
                                else
                                {
                                    ret = Device.ConnectWlan();
                                }

                                if (ret)
                                {
                                }

                                Thread.Sleep(5000);
                                for (int i = 0; i < 15; i++)
                                {
                                    if (Device.CheckNetworkStat())//检测设备是否已经连接到网关,true:已经连接到网关
                                    {
                                        res = ConnState.ConnectSuccess;
                                        Thread.Sleep(5000);
                                        break;
                                    }
                                    Thread.Sleep(1000);
                                }
                                break;
                        }
                    }
                    OnConnectNotify(new ConnectNotifyEventArgs(res, ConnectType));
                    IsConnecting = false;
                }
            }
        }
        #endregion
    }
}

原创粉丝点击