经典代码IOCP(完成端口)的C#.Net实现----解读

通过C#编写集成IOCP高性能高并发优势服务器服务源码 3、系统目前支持modbus 、modbus rtu协议,可定制开发集成Modbus TCp、西门子PLC S7协议、OPC协议、三菱PLC协议以及集成MQTT服务(以上协议在框架中没有集成,可以定制集成)。WEB API功能有服务器日志查询、WEB API接口认证用户管理、远端设备注册管理、服务器轮询读取任务启停、服务器参数设置、查询历史数据记录、下发指令到终端设备。5、与传统协议方法不同,比如Modbus设备,需要PC端主动去连接设备,而这套框架只需要监听端口,服务器就能自动去轮询终端所有设备。 阅读详情

感谢原作者

经典代码IOCP(完成端口)的C#.Net实现

重复造轮子的意义在于深刻的理解和学习---

main.cs:

/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace c2iocp
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
*/
using System;
using System.Threading;  // Included for the Thread.Sleep call
using Continuum.Threading;
namespace Sample
{
    //============================================
    /// <summary> Sample class for the threading class </summary>
    public class UtilThreadingSample
    {
        //******************************************* 
        /// <summary> Test Method </summary>
        static void Main()
        {
            // Create the MSSQL IOCP Thread Pool
            IOCPThreadPool pThreadPool = new IOCPThreadPool(0, 5, 10, new IOCPThreadPool.USER_FUNCTION(IOCPThreadFunction));
            pThreadPool.PostEvent(10);
            Thread.Sleep(10000);
            pThreadPool.Dispose();
        }
        //*****************************************
        /// <summary> Function to be called by the IOCP thread pool.  Called when
        ///           a command is posted for processing by the SocketManager </summary>
        /// <param name="iValue"> The value provided by the thread posting the event </param>
        static public void IOCPThreadFunction(Int32 iValue)
        {
            try
            {
                Console.WriteLine("Value: {0}", iValue);
            }
            catch (Exception pException)
            {
                Console.WriteLine(pException.Message);
            }
        }
    }
}

continuum.cs

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace Continuum.Threading
{

    // Structures
    //==========================================
    /// <summary> This is the WIN32 OVERLAPPED structure </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public unsafe struct OVERLAPPED
    {
        UInt32* ulpInternal;
        UInt32* ulpInternalHigh;
        Int32 lOffset;
        Int32 lOffsetHigh;
        UInt32 hEvent;
    }

    // Classes
    //============================================
    /// <summary> This class provides the ability to create a thread pool to manage work.  The
    ///           class abstracts the Win32 IOCompletionPort API so it requires the use of
    ///           unmanaged code.  Unfortunately the .NET framework does not provide this functionality </summary>
    public sealed class IOCPThreadPool
    {

        // Win32 Function Prototypes
        /// <summary> Win32Func: Create an IO Completion Port Thread Pool </summary>
        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        private unsafe static extern UInt32 CreateIoCompletionPort(UInt32 hFile, UInt32 hExistingCompletionPort, UInt32* puiCompletionKey, UInt32 uiNumberOfConcurrentThreads);

        /// <summary> Win32Func: Closes an IO Completion Port Thread Pool </summary>
        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        private unsafe static extern Boolean CloseHandle(UInt32 hObject);

        /// <summary> Win32Func: Posts a context based event into an IO Completion Port Thread Pool </summary>
        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        private unsafe static extern Boolean PostQueuedCompletionStatus(UInt32 hCompletionPort, UInt32 uiSizeOfArgument, UInt32* puiUserArg, OVERLAPPED* pOverlapped);

        /// <summary> Win32Func: Waits on a context based event from an IO Completion Port Thread Pool.
        ///           All threads in the pool wait in this Win32 Function </summary>
        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        private unsafe static extern Boolean GetQueuedCompletionStatus(UInt32 hCompletionPort, UInt32* pSizeOfArgument, UInt32* puiUserArg, OVERLAPPED** ppOverlapped, UInt32 uiMilliseconds);

        // Constants
        /// <summary> SimTypeConst: This represents the Win32 Invalid Handle Value Macro </summary>
        private const UInt32 INVALID_HANDLE_VALUE = 0xffffffff;

        /// <summary> SimTypeConst: This represents the Win32 INFINITE Macro </summary>
        private const UInt32 INIFINITE = 0xffffffff;

        /// <summary> SimTypeConst: This tells the IOCP Function to shutdown </summary>
        private const Int32 SHUTDOWN_IOCPTHREAD = 0x7fffffff;



        // Delegate Function Types
        /// <summary> DelType: This is the type of user function to be supplied for the thread pool </summary>
        public delegate void USER_FUNCTION(Int32 iValue);


        // Private Properties
        private UInt32 m_hHandle;
        /// <summary> SimType: Contains the IO Completion Port Thread Pool handle for this instance </summary>
        private UInt32 GetHandle { get { return m_hHandle; } set { m_hHandle = value; } }

        private Int32 m_uiMaxConcurrency;
        /// <summary> SimType: The maximum number of threads that may be running at the same time </summary>
        private Int32 GetMaxConcurrency { get { return m_uiMaxConcurrency; } set { m_uiMaxConcurrency = value; } }

        private Int32 m_iMinThreadsInPool;
        /// <summary> SimType: The minimal number of threads the thread pool maintains </summary>
        private Int32 GetMinThreadsInPool { get { return m_iMinThreadsInPool; } set { m_iMinThreadsInPool = value; } }

        private Int32 m_iMaxThreadsInPool;
        /// <summary> SimType: The maximum number of threads the thread pool maintains </summary>
        private Int32 GetMaxThreadsInPool { get { return m_iMaxThreadsInPool; } set { m_iMaxThreadsInPool = value; } }

        private Object m_pCriticalSection;
        /// <summary> RefType: A serialization object to protect the class state </summary>
        private Object GetCriticalSection { get { return m_pCriticalSection; } set { m_pCriticalSection = value; } }

        private USER_FUNCTION m_pfnUserFunction;
        /// <summary> DelType: A reference to a user specified function to be call by the thread pool </summary>
        private USER_FUNCTION GetUserFunction { get { return m_pfnUserFunction; } set { m_pfnUserFunction = value; } }

        private Boolean m_bDisposeFlag;
        /// <summary> SimType: Flag to indicate if the class is disposing </summary>
        private Boolean IsDisposed { get { return m_bDisposeFlag; } set { m_bDisposeFlag = value; } }

        // Public Properties
        private Int32 m_iCurThreadsInPool;
        /// <summary> SimType: The current number of threads in the thread pool </summary>
        public Int32 GetCurThreadsInPool { get { return m_iCurThreadsInPool; } set { m_iCurThreadsInPool = value; } }
        /// <summary> SimType: Increment current number of threads in the thread pool </summary>
        private Int32 IncCurThreadsInPool() { return Interlocked.Increment(ref m_iCurThreadsInPool); }
        /// <summary> SimType: Decrement current number of threads in the thread pool </summary>
        private Int32 DecCurThreadsInPool() { return Interlocked.Decrement(ref m_iCurThreadsInPool); }
        private Int32 m_iActThreadsInPool;
        /// <summary> SimType: The current number of active threads in the thread pool </summary>
        public Int32 GetActThreadsInPool { get { return m_iActThreadsInPool; } set { m_iActThreadsInPool = value; } }
        /// <summary> SimType: Increment current number of active threads in the thread pool </summary>
        private Int32 IncActThreadsInPool() { return Interlocked.Increment(ref m_iActThreadsInPool); }
        /// <summary> SimType: Decrement current number of active threads in the thread pool </summary>
        private Int32 DecActThreadsInPool() { return Interlocked.Decrement(ref m_iActThreadsInPool); }
        private Int32 m_iCurWorkInPool;
        /// <summary> SimType: The current number of Work posted in the thread pool </summary>
        public Int32 GetCurWorkInPool { get { return m_iCurWorkInPool; } set { m_iCurWorkInPool = value; } }
        /// <summary> SimType: Increment current number of Work posted in the thread pool </summary>
        private Int32 IncCurWorkInPool() { return Interlocked.Increment(ref m_iCurWorkInPool); }
        /// <summary> SimType: Decrement current number of Work posted in the thread pool </summary>
        private Int32 DecCurWorkInPool() { return Interlocked.Decrement(ref m_iCurWorkInPool); }



        // Constructor, Finalize, and Dispose 
        //***********************************************
        /// <summary> Constructor </summary>
        /// <param name = "iMaxConcurrency"> SimType: Max number of running threads allowed </param>
        /// <param name = "iMinThreadsInPool"> SimType: Min number of threads in the pool </param>
        /// <param name = "iMaxThreadsInPool"> SimType: Max number of threads in the pool </param>
        /// <param name = "pfnUserFunction"> DelType: Reference to a function to call to perform work </param>
        /// <exception cref = "Exception"> Unhandled Exception </exception>
        public IOCPThreadPool(Int32 iMaxConcurrency, Int32 iMinThreadsInPool, Int32 iMaxThreadsInPool, USER_FUNCTION pfnUserFunction)
        {
            try
            {
                // Set initial class state
                GetMaxConcurrency = iMaxConcurrency;//系统允许的最大线程数,如果为0,则和内核数一致,一般都是0,
                GetMinThreadsInPool = iMinThreadsInPool;//最小线程数目
                GetMaxThreadsInPool = iMaxThreadsInPool;//最大线程数目,都是设定给的参数
                GetUserFunction = pfnUserFunction;//传递过来的回调函数,或者说事件处理函数
                // Init the thread counters
                GetCurThreadsInPool = 0;//当前线程池里的线程数
                GetActThreadsInPool = 0;//活动的线程数,正在工作的线程数
                GetCurWorkInPool = 0;//被投递的事件数目
                // Initialize the Monitor Object
                GetCriticalSection = new Object();//来包含类状态的对象????
                // Set the disposing flag to false
                IsDisposed = false;//是否资源释放标志
                unsafe//c#调用外面dll的时候,需要用这个unsafe
                {
                    // Create an IO Completion Port for Thread Pool use
                    //步骤1:创建iocp端口,
                    /*
                     * 参数说明编辑
FileHandle是有效的文件句柄或INVALID_HANDLE_VALUE。
ExistingCompletionPort是已经存在的完成端口。如果为NULL,则为新建一个IOCP。
CompletionKey是传送给处理函数的参数。
NumberOfConcurrentThreads是有多少个线程在访问这个消息队列。当参数ExistingCompletionPort不为0的时候,系统忽略该参数,当该参数为0表示允许同时相等数目于处理器个数的线程访问该消息队列。
返回值编辑
返回一个IOCP的句柄。若为NULL则创建失败,不为NULL则创建成功。
                     * 
                     * CreateIoCompletionPort的前三个参数只在把设备同Complete Port相关联时才有用。
此时我们只需传递INVALID_HANDLE_VALUE,NULL和0即可。
第四个参数告诉端口同时能运行的最多线程数,这里设置为0,表示默认为当前计算机的CPU数目。
                     */
                    GetHandle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, null, (UInt32)GetMaxConcurrency);
                }
                // Test to make sure the IO Completion Port was created
                if (GetHandle == 0)
                    throw new Exception("Unable To Create IO Completion Port");
                // Allocate and start the Minimum number of threads specified
                Int32 iStartingCount = GetCurThreadsInPool;//当前线程池里的线程数目
                ThreadStart tsThread = new ThreadStart(IOCPFunction);//// 摘要: 表示在 System.Threading.Thread 上执行的方法。public delegate void ThreadStart();
                /*
                 * 2 我们的ThreadFun线程函数执行一些初始化之后,将进入一个循环,该循环会在服务进程终止时才结束。
 在循环中,调用GetQueuedCompletionStatus,这样就把当前线程的ID放入一个等待线程队列中,I/O CP内核对象就总能知道哪个线程在等待处理完成的I/O请求。
 如果在IDLE_THREAD_TIMEOUT规定的时间内I/O CP上还没有出现一个Completion Packet,则转入下一次循环。在这里我们设置的IDLE_THREAD_TIMEOUT为1秒。
 当端口的I/O完成队列中出现一项时,完成端口就唤醒等待线程队列中的这个线程,该线程将得到完成的I/O项中的信息: 传输的字节数、完成键和OVERLAPPED结构的地址。
 在我们的程序中可以用智能指针或者BSTR或者int来接受这个OVERLAPPED结构的地址的值,从而得到消息;然后在这个线程中处理消息。
 GetQueuedCompletionStatus的第一个参数hCompletionPort指出了要监视哪一个端口,这里我们传送先前从CreateIoCompletionPort返回的端口句柄。
                 */
                for (Int32 iThread = 0; iThread < GetMinThreadsInPool; ++iThread)
                {
                    // Create a thread and start it
                    Thread thThread = new Thread(tsThread);
                    thThread.Name = "IOCP " + thThread.GetHashCode();
                    thThread.Start();
                    // Increment the thread pool count
                    IncCurThreadsInPool();//原子操作+1初始为0
                    /*
                     * long m_dwRefCount..
InterlockedIncrement(&m_dwRefCount)
对m_dwRefCount加1
在对m_dwRefCount访问的时候其他线程不能访问这个变量
InterlockedIncrement实现的是原子性的加减,什么是原子性的加减呢?

举例:如果一个变量long value = 0;
首先说一下正常情况下的加减操作,value+=1;
1.系统从value的空间取出值,并动态生成一个空间存储取出来的值;
2.将取出来的值和1做加法,并将和放入value的空间覆盖掉原值,操作结束;
                    
如果此时有2个Thread,分别记作AThread,BThread
1.AThread将value从存储空间取出,为0;
2.BThread将value从存储空间取出,为0;
3.AThread将取出来的值和1做加法,并将和放入value的空间覆盖掉原值,加法结束,value = 1;
4.BThread将取出来的值和1做加法,并将和放入value的空间覆盖掉原值,加法结束,value = 1;
最后value应该是2,InterlockedIncrement保证一个线程访问变量时其他线程不能访问。
 
用于增减变量的并不是常用的Inc/Dec过程,而是用了InterlockedIncrement/InterlockedDecrement这一对过程,它们实现的功能完全一样,都是对变量加一或减一。
但它们有一个最大的区别,那就是InterlockedIncrement/InterlockedDecrement是线程安全的。
即它们在多线程下能保证执行结果正确,而Inc/Dec不能。
或者按操作系统理论中的术语来说,这是一对“原语”操作。
                     */
                }
            }
            catch
            {
                throw new Exception("Unhandled Exception");
            }
        }

        //***********************************************
        /// <summary> Finalize called by the GC </summary>
        ~IOCPThreadPool()
        {
            /*
             * 这个类结束,应该也意味着是主线程了,所以只有主线程访问?,既然是主线程,为什么还要加标志位呢????
             */
            if (!IsDisposed)//还没有释放资源,则释放,这个应该也是原子操作对isdisposeD吧
                Dispose();
        }

        //**********************************************
        /// <summary> Called when the object will be shutdown.  This
        ///           function will wait for all of the work to be completed
        ///           inside the queue before completing </summary>
        public void Dispose()
        {
            try
            {
                // Flag that we are disposing this object
                IsDisposed = true;
                // Get the current number of threads in the pool
                Int32 iCurThreadsInPool = GetCurThreadsInPool;//现在生成的线程数,用原子操作实现的
                // Shutdown all thread in the pool
                for (Int32 iThread = 0; iThread < iCurThreadsInPool; ++iThread)
                {
                    unsafe
                    {
                        bool bret = PostQueuedCompletionStatus(GetHandle, 4, (UInt32*)SHUTDOWN_IOCPTHREAD, null);
                        /*
                         * PostQueuedCompletionStatus函数,向每个工作者线程都发送—个特殊的完成数据包。该函数会指示每个线程都“立即结束并退出”.
                         * 下面是PostQueuedCompletionStatus函数的定义: 
[cpp] view plain copy
BOOL PostQueuedCompletionStatus(   
    HANDLE CompletlonPort,   
    DW0RD dwNumberOfBytesTrlansferred,   
    DWORD dwCompletlonKey,   
LPOVERLAPPED lpoverlapped,  
);  


       其中,CompletionPort参数指定想向其发送一个完成数据包的完成端口对象。而就dwNumberOfBytesTransferred,dwCompletionKey和lpOverlapped
                         * 这三个参数来说.每—个都允许我们指定—个值,直接传递给GetQueuedCompletionStatus函数中对应的参数。这样—来。
                         * —个工作者线程收到传递过来的三个GetQueuedCompletionStatus函数参数后,便可根据由这三个参数的某一个设置的特殊值,
                         * 决定何时应该退出。例如,可用dwCompletionPort参数传递0值,而—个工作者线程会将其解释成中止指令。一旦所有工作者线程都已关闭,
                         * 便可使用CloseHandle函数,关闭完成端口。最终安全退出程序。 
        PostQueuedCompletionStatus函数提供了一种方式来与线程池中的所有线程进行通信。如,当用户终止服务应用程序时,我们想要所有线程都完全利索地退出。
                         * 但是如果各线程还在等待完成端口而又没有已完成的I/O 请求,那么它们将无法被唤醒。 
       通过为线程池中的每个线程都调用一次PostQueuedCompletionStatus,我们可以将它们都唤醒。每个线程会对GetQueuedCompletionStatus的返回值进行检查,
                         * 如果发现应用程序正在终止,那么它们就可以进行清理工作并正常地退出。
                         */
                    }
                }
                // Wait here until all the threads are gone
                while (GetCurThreadsInPool != 0) Thread.Sleep(100);
                unsafe
                {
                    // Close the IOCP Handle
                    CloseHandle(GetHandle);//便可使用CloseHandle函数,关闭完成端口。最终安全退出程序。 
                }
            }
            catch
            {
            }
        }

        // Private Methods
        //*******************************************
        /// <summary> IOCP Worker Function that calls the specified user function </summary>
        private void IOCPFunction()
        {
            UInt32 uiNumberOfBytes;
            Int32 iValue;
            try
            {
                while (true)
                {
                    unsafe
                    {
                        OVERLAPPED* pOv;
                        // Wait for an event
                        /*
                         * BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort,
LPDWORD lpNumberOfBytes,
PULONG_PTR lpCompletionKey,
LPOVERLAPPED *lpOverlapped,
DWORD dwMilliseconds);
调用参数:
CompletionPort:指定的IOCP,该值由CreateIoCompletionPort函数创建。
lpnumberofbytes:一次完成后的I/O操作所传送数据的字节数。
lpcompletionkey:当文件I/O操作完成后,用于存放与之关联的CK。
lpoverlapped:为调用IOCP机制所引用的OVERLAPPED结构。
dwmilliseconds:用于指定调用者等待CP的时间。
返回值:
调用成功,则返回非零数值,相关数据存于lpNumberOfBytes、lpCompletionKey、lpoverlapped变量中。失败则返回零值。
                         */
                        GetQueuedCompletionStatus(GetHandle, &uiNumberOfBytes, (UInt32*)&iValue, &pOv, INIFINITE);
                    }
                    // Decrement the number of events in queue
                    DecCurWorkInPool();//原子操作,减少一个io里面的任务数目
                    // Was this thread told to shutdown
                    if (iValue == SHUTDOWN_IOCPTHREAD)//关闭标志位,post传递一个io完成数据,这里读到,知道该结束了,提前约定
                        break;
                    // Increment the number of active threads
                    IncActThreadsInPool();//这个线程读到任务开始工作,增加活动线程数
                    try
                    {
                        // Call the user function
                        GetUserFunction(iValue);
                    }
                    catch
                    {
                    }
                    // Get a lock
                    Monitor.Enter(GetCriticalSection);
                    try
                    {
                        // If we have less than max threads currently in the pool
                        //当前已经生成了符合设定最小线程数目的线程,如果当前每到最大线程数,继续创建
                        if (GetCurThreadsInPool < GetMaxThreadsInPool)
                        {
                            // Should we add a new thread to the pool
                            //所有线程都已在工作
                            if (GetActThreadsInPool == GetCurThreadsInPool)
                            {
                                if (IsDisposed == false)//没有进行资源释放,标志位的作用在这里
                                {
                                    // Create a thread and start it
                                    ThreadStart tsThread = new ThreadStart(IOCPFunction);
                                    Thread thThread = new Thread(tsThread);
                                    thThread.Name = "IOCP " + thThread.GetHashCode();
                                    thThread.Start();
                                    // Increment the thread pool count
                                    IncCurThreadsInPool();//增加当前的活动总数,子线程还可以继续生成线程????
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                    // Relase the lock
                    Monitor.Exit(GetCriticalSection);//这个是为了保证,比如当前进程为5都在工作了,最大上限设定10,
                    //发现都在工作只增加1个额外空闲线程,如果不用 Monitor 是不是会都发现不够一下增加太多,最后可能当前线程数超过10
                    // Increment the number of active threads
                    DecActThreadsInPool();//干活结束,减少忙碌状态的线程
                }
            }
            catch
            {
            }
            // Decrement the thread pool count
            DecCurThreadsInPool();//线程退出,减少当前进程数
        }

        // Public Methods
        //******************************************
        /// <summary> IOCP Worker Function that calls the specified user function </summary>
        /// <param name="iValue"> SimType: A value to be passed with the event </param>
        /// <exception cref = "Exception"> Unhandled Exception </exception>
        public void PostEvent(Int32 iValue)
        {
            try
            {
                // Only add work if we are not disposing
                if (IsDisposed == false)
                {
                    unsafe
                    {
                        // Post an event into the IOCP Thread Pool
                        PostQueuedCompletionStatus(GetHandle, 4, (UInt32*)iValue, null);
                    }
                    // Increment the number of item of work
                    IncCurWorkInPool();
                    // Get a lock
                    Monitor.Enter(GetCriticalSection);
                    try
                    {
                        // If we have less than max threads currently in the pool
                        if (GetCurThreadsInPool < GetMaxThreadsInPool)
                        {
                            // Should we add a new thread to the pool
                            if (GetActThreadsInPool == GetCurThreadsInPool)
                            {
                                if (IsDisposed == false)
                                {
                                    // Create a thread and start it
                                    ThreadStart tsThread = new ThreadStart(IOCPFunction);
                                    Thread thThread = new Thread(tsThread);
                                    thThread.Name = "IOCP " + thThread.GetHashCode();
                                    thThread.Start();
                                    // Increment the thread pool count
                                    IncCurThreadsInPool();
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                    // Release the lock
                    Monitor.Exit(GetCriticalSection);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            catch
            {
                throw new Exception("Unhandled Exception");
            }
        }
        //*****************************************
        /// <summary> IOCP Worker Function that calls the specified user function </summary>
        /// <exception cref = "Exception"> Unhandled Exception </exception>
        public void PostEvent()
        {
            try
            {
                // Only add work if we are not disposing
                if (IsDisposed == false)
                {
                    unsafe
                    {
                        // Post an event into the IOCP Thread Pool
                        PostQueuedCompletionStatus(GetHandle, 0, null, null);
                    }
                    // Increment the number of item of work
                    IncCurWorkInPool();
                    // Get a lock
                    Monitor.Enter(GetCriticalSection);
                    try
                    {
                        // If we have less than max threads currently in the pool
                        if (GetCurThreadsInPool < GetMaxThreadsInPool)
                        {
                            // Should we add a new thread to the pool
                            if (GetActThreadsInPool == GetCurThreadsInPool)
                            {
                                if (IsDisposed == false)
                                {
                                    // Create a thread and start it
                                    ThreadStart tsThread = new ThreadStart(IOCPFunction);
                                    Thread thThread = new Thread(tsThread);
                                    thThread.Name = "IOCP " + thThread.GetHashCode();
                                    thThread.Start();
                                    // Increment the thread pool count
                                    IncCurThreadsInPool();
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                    // Release the lock
                    Monitor.Exit(GetCriticalSection);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            catch
            {
                throw new Exception("Unhandled Exception");
            }
        }
    }
}


C#高性能大容量SOCKET并发(一):IOCP完成端口例子介绍 例子主要包括SocketAsyncEventArgs通讯封装、服务端实现日志查看、SCOKET列表、上传、下载、远程文件流、吞吐量协议,用于测试SocketAsyncEventArgs的性能和压力,最大连接数支持65535个长连接,最高命令交互速度达到250MB/S(使用的是127.0.0.1的方式,相当于千兆网卡1Gb=125MB/S两倍的吞吐量)。服务端用C#编写,并使用log4net作为日志模块。 阅读详情

相关推荐

ASP.NET异步操作深度解析:从IOCP到ValueTask的性能实战

异步编程是现代Web服务高并发能力的底层基石,其本质并非语法糖,而是基于操作系统I/O完成端口(IOCP)的事件驱动模型。理解async/await背后的状态机编译机制、线程复用原理与上下文捕获开销,是规避线程池饥饿、连接池耗尽和GC压力激增的关键。在ASP.NET生态中,.NET Framework与Core的异步管道存在代际差异,尤其体现在HttpContext传递、SynchronizationContext行为及ValueTask优化支持上。掌握Task/ValueTask选型、ConfigureA

aobai7842的博客 409

C# IOCP SocketAsyncEventArgs 使用流程

如果在 “4” 处采用异步,则初始化后将同步接收代码交由异步处理,后续操作则不会引发错误。从上图不难看出:如果极端情况下,Accept后的Socket会一直或较长一段时间内处于同步状态,此时将会占用主线程来执行相关数据处理工作,会影响后续Socket的Accept。在Windows系统下,该异步方法采用IOCP来实现,在Linux方法下通过epoll来实现,C#原生Socket仅有Select能够支持简单的“并发”,如果想在Linux平台实现高效率,可以采用此方法以达到epoll的效率。

少年的博客 3063

C#网络编程技术FastSocket实战项目演练

一、FastSocket课程介绍        .NET框架虽然微软提供了socket通信的类库,但是还有很多事情要自己处理,比如TCP协议需要处理分包、组包、粘包、维护连接列表等,UDP协议需要处理丢包、乱序,而且对于多连接并发,还要自己处理多线程等等。本期分享课程阿笨给大家带来的是来源于github开源Socket通信中间件:FastSocket,目的就是把大家从繁琐的网络编程技术中彻底地解放...

跟着阿笨一起玩NET 2343

C#-IOCP-SAEA使用(解决“现在已经正在使用此 SocketAsyncEventArgs“问题)

C#-IOCP-SAEA使用(解决"现在已经正在使用此 SocketAsyncEventArgs"问题) 解决思路:在server端以下socket通讯各异步过程中,规避使用相同的saea对象; 接受连接(_listenSocket.AcceptAsync(acceptSaea)), 接收数据(token.UserSocket.ReceiveAsync(recvSaea)), 发送数据(token.UserSocket.SendAsync(sendSaea))

1278

线程本质与异步真相:从操作系统到.NET运行时的深度解析

线程是操作系统调度的最小执行单元,其创建、调度与销毁直接受内核管理,涉及栈分配、上下文切换和资源回收等底层机制。理解线程并非语法糖(如async/await)的载体,而是承载CPU密集型任务或协调I/O等待的关键资源。真正的异步价值在于释放线程——让其不阻塞等待磁盘、网络或定时器,转而由IOCP、epoll或Timer回调驱动续体执行。这决定了高并发服务的吞吐上限与响应稳定性。本文结合Windows/Linux系统调用、.NET线程池自适应算法及async状态机原理,剖析线程在Web服务、桌面应用与后台任务

baochuanying9531的博客 312

C#机器视觉入门:从环境搭建到实时图像处理实战

机器视觉作为计算机视觉的重要分支,其核心原理是通过算法处理图像数据以提取信息、做出决策。在工程实践中,选择合适的技术栈对项目成功至关重要。C#凭借其优雅的语法、强大的.NET框架和成熟的工业自动化生态,在机器视觉应用开发中展现出独特的技术价值。它不仅能高效调用OpenCV等核心库完成图像处理、特征提取等任务,更能无缝集成到需要稳定、可靠且带图形界面的上位机系统中,适用于工业检测、自动化控制等典型应用场景。本文聚焦于C#结合OpenCvSharp进行机器视觉开发,详细阐述了从环境配置、核心图像处理操作到构建实

anjichan4261的博客 484

.NET技术博客的深度内容生产方法论

.NET 是微软推出的跨平台开源开发框架,其核心价值在于统一的 Runtime、高性能 JIT 编译器与成熟的工具链。理解 .NET 不仅需掌握 C# 语法和 API 使用,更需穿透至 Framework 层与 Runtime 层,厘清内存管理(如 Span<T>)、异步模型(async/await 状态机)及依赖注入等机制的底层原理。这种穿透式学习能显著提升系统可维护性与故障定位能力,广泛应用于企业级 Web 服务、微服务架构与云原生应用开发。本文聚焦真实工程场景中 .NET 博客如何通过源码验证、实证测

dejz8829的专栏 473

.NET 4.0/4.5底层能力图谱:CLR运行时、GC与async演进核心解析

.NET运行时(CLR)是托管代码执行的基础引擎,其内存管理、线程调度与异步模型直接决定企业级应用的稳定性与性能边界。从.NET 4.0开始,CLR迎来分水岭式重构:并行标记(Parallel Marking)首次实现GC标记阶段多线程化,显著降低大堆暂停时间;.NET 4.5则通过编译器+运行时协同,落地`async/await`原生支持,推动I/O完成端口(IOCP)与托管层深度耦合。这些变化不仅塑造了现代.NET的GC策略(如后台回收)和并发编程范式(如无锁集合),更构成遗留系统调优、金融低延迟网关设

didi9310的博客 453

博毅创为 Unity_0基础就业班

多年一线大厂开发经验,10年讲课教学经验。基础课程比较多,因为Unity 3D开发相对其他变成课程来说,入门的门槛要高一些,这列列举了比较多的入门课程,涵盖了各个方面,建议零基础的都学学。第078课unity_NGUl_05_NGUI_打字机效果_Tab商城_背包之Scrollview1(基于panel)第083课unity_iTween_02_缓动介绍_值变化_缩放_旋转_摇晃_摆动_颜色变化。第006课unity_C#数组_string_static_const_泛型编程_名字空间。

Thomas_YXQ的博客 870

C#简易Socket通信工具开发实战

很多人第一次接触Socket时,都会被告知:“这是网络通信的端点。”听起来很抽象,其实可以打个比方:IP地址 + 端口 = 一栋大楼的门牌号Socket = 大楼里的具体房间(可通话的办公室)比如你的电脑访问百度,本质上是向发起请求。这个组合就是远程服务器上的一个“房间”。而你在本地也会被分配一个临时端口(如50123),形成一条双向通道——这就是Socket的作用:创建并管理这条虚拟链路。但关键在于,Socket并不是简单的“连接工具”,它是操作系统内核提供的资源句柄。

weixin_42499004的博客 1049

C# IOCP完成端口模型(简单实用高效)

最近有项目要做一个高性能网络服务器,去网络上搜到到的都是C++版本而且是英文或者简单的DEMO,所以自己动手写了C# 的DEMO。 网络上只写接收到的数据,没有说怎么处理缓冲区数据,本DEMO简单的介绍如何处理接收到的数据。简单易用,希望对大家有用. 1、在C#中,不用去面对完成端口的操作系统内核对象,Microsoft已经为我们提供了SocketAsyncEventArgs类,它封装了IOCP的使用。请参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1。 2、我的SocketAsyncEventArgsPool类使用List对象来存储对客户端来通信的SocketAsyncEventArgs对象,它相当于直接使用内核对象时的IoContext。我这样设计比用堆栈来实现的好处理是,我可以在SocketAsyncEventArgsPool池中找到任何一个与服务器连接的客户,主动向它发信息。而用堆栈来实现的话,要主动给客户发信息,则还要设计一个结构来存储已连接上服务器的客户。 3、对每一个客户端不管还发送还是接收,我使用同一个SocketAsyncEventArgs对象,对每一个客户端来说,通信是同步进行的,也就是说服务器高度保证同一个客户连接上要么在投递发送请求,并等待;或者是在投递接收请求,等待中。本例只做echo服务器,还未考虑由服务器主动向客户发送信息。 4、SocketAsyncEventArgs的UserToken被直接设定为被接受的客户端Socket。 5、没有使用BufferManager 类,因为我在初始化时给每一个SocketAsyncEventArgsPool中的对象分配一个缓冲区,发送时使用Arrary.Copy来进行字符拷贝,不去改变缓冲区的位置,只改变使用的长度,因此在下次投递接收请求时恢复缓冲区长度就可以了!如果要主动给客户发信息的话,可以new一个SocketAsyncEventArgs对象,或者在初始化中建立几个来专门用于主动发送信息,因为这种需求一般是进行信息群发,建立一个对象可以用于很多次信息发送,总体来看,这种花销不大,还减去了字符拷贝和消耗。 6、测试结果:(在我的笔记本上时行的,我的本本是T420 I7 8G内存) 100客户 100,000(十万次)不间断的发送接收数据(发送和接收之间没有Sleep,就一个一循环,不断的发送与接收) 耗时3004.6325 秒完成 总共 10,000,000 一千万次访问 平均每分完成 199,691.6 次发送与接收 平均每秒完成 3,328.2 次发送与接收 整个运行过程中,内存消耗在开始两三分种后就保持稳定不再增涨。 看了一下对每个客户端的延迟最多不超过2秒。

游戏网络延迟实测:C#中UDP与TCP性能对比与选型指南

在网络编程中,传输层协议是构建实时应用的基础。TCP协议通过连接管理、重传机制和流量控制保证了数据的可靠、有序交付,其原理基于三次握手和滑动窗口。UDP协议则采用无连接、尽最大努力交付的模型,减少了协议栈的处理开销。在技术价值上,TCP提供了开箱即用的可靠性,简化了开发,而UDP则在延迟和资源消耗上具备潜在优势。对于高实时性要求的应用场景,如多人在线游戏、实时音视频通信,协议的延迟表现和稳定性直接关乎用户体验。本文聚焦于**游戏网络通讯**这一核心场景,通过C#与**BenchmarkDotNet**构建实

diaohuyi6830的博客 388

c#写的iocp服务端和客户端代码

用c#语言写的iocp网络服务,和客户端,有注释,程序可直接运行

C#使用完成端口

对IOCP主要做了几个函数的封装,PostConnect、PostAccept、PostSend、PostRecv、ConnectCompleted、AcceptCompleted、SendCompleted、RecvCompleted。最近在做一个C#的项目,需要作为Tcp的Server端来工作,于是研究了一下C#下面的完成端口用法,做了一些简单的封装。这里仅列出一些基础代码,全部的代码可以在。Client与Server的定义就比较简单了。此外还有一些参数设置的细节。...............

xunmeng2002的专栏 1147

(侧边抽屉)移动端APP汉堡页设计全解:从规范落地到体验优化

汉堡页是一种隐藏式侧边导航组件,由汉堡图标(三条水平横线)、侧边抽屉面板、主界面遮罩层三部分构成。点击汉堡图标后,面板通常从左侧滑入,覆盖主界面部分区域(或悬浮于主界面之上);关闭时可通过点击关闭按钮、遮罩层、返回键或侧滑手势完成,是移动端特有的空间优化设计模式。

qq_67370502的博客 620

【OTDR曲线工具箱】04 批量生成sor文件

软件下载地址:http://120.78.150.214:9999/isora.zip 通过一个测试数据sor文件,批量生成多个测试文件(基于Bellcore GR196及SR-4731标准)。主要考虑哪些问题: 1、sor测试是否符合要求(主要是衰减系数) 2、多个文件的创建时间 3、文件必须随机处理,不要看省去一模一样。 来说说怎么操作的 第一步:选择参考曲线 参考曲线的选择由两种方法:从文件中导入、从曲线库中选择。 其中曲线库分为公共库和个人库两种,平时浏览的sor文件都可以加载到库中。 第二步

夜晚是传说的博客 5292
上一篇: 关押罪犯-详解-noip2010-并查集--搜索--二分图
下一篇: 利用Flexpaper实现对多篇pdf文章在线切换预览评分
legan
博客等级 码龄17年 47粉丝 36原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值