Git Product home page Git Product logo

asyncsocketer's Introduction

AsyncSocketer

AsyncSocketer 是对标准套接字的以事件驱动的封装,修改自CodeProject中关于高性能异步Socket的文章:C# SocketAsyncEventArgs High Performance Socket Code 。该文章的主要目的是完善MSDN中关于异步Socket简介中的示例项目。该文的思路是提供一个缓冲管理类 BufferManager 用于接收和发送网络数据,提供一个保存套接字上下文的UserToken,以及拟定收发数据的协议定义及解析。

本项目是继承上文中缓存管理和套接字上下文,并于套接字的连接、接收、发送、断开时,以事件通知的形式,简化套接字中关于接收和发送的直接调用。

本项目提供以下类以实现项目目的:

###事件委托

  • SocketEventHandler 套接字事件委托
  • ServerSocketEventHandler 服务端套接字事件委托
  • SocketErrorHandler 套接字错误事件委托
  • SocketPerformanceHandler 套接字性能计数事件委托

###接口

  • IDentity 标识定义

###

  • SocketConfigure AsyncSocketer 参数设置定义
  • ISocketer 标准套接字封装
  • TcpSocketer : ISocketer 使用 TCP 协议的套接字
  • UdpSocketer : ISocketer 使用 UDP 协议的套接字
  • PerformanceBase : Component 性能计数基础对象定义
  • EventSocketer : PerformanceBase 异步套接字,标准套接字的事件驱动封装
  • PartialSocket : EventSocketer 使用多个 SocketAsyncEventArgs 池和缓存池分开管理事件的异步套接字
  • PartialSocketServer : PartialSocket 分开管理事件的服务端异步套接字
  • MixedSocket : EventSocketer 使用同一个 SocketAsyncEventArgs 池和缓存池分开管理事件的异步套接字
  • BufferManager 管理缓存
  • SocketEventArgs : EventArgs 套接字事件参数
  • ServerSocketEventArgs : SocketEventArgs 服务端套接字事件参数
  • SocketErrorArgs : EventArgs 套接字错误事件参数
  • PerformanceCountArgs : EventArgs 套接字性能计数事件参数
  • ServerPerformanceCountArgs : PerformanceCountArgs 服务端套接字性能计数事件参数
  • Pooler<TEArtType> where TEArtType : IDentity 抽象对象池
  • EventArgObject : IDentity SocketAsyncEventArgs 的 IDentity 封装定义
  • EventPool SocketAsyncEventArgs 池
  • MessageFragment : IDentity 套接字的收发包数据分片
  • EventToken SocketAsyncEventArgs 的套接字上下文
  • MessagePool 网络数据池

###处理流程

通过使用EventSocketer派生的子类,并重载CreateClientSocket来获得一个重载自ISocketer的套接字实例以实现具体的网络操作,通过重载类似Get***AsyncEvents的相关函数来获得异步操作时需要使用到的SocketAsyncEventArgs,通过重载形如Get***Buffer的相关函数来获得缓存管理BufferManager类的实例,通过重载与OnSended相似的函数在事件发生后来处理SocketAsyncEventArgs对象,通过重载ReceiveSend函数来自定义接收和发送数据的方式,通过重载Reset***AsyncEvents相似的函数在断开连接时来重置 SocketAsyncEventArgs 池。

  • 连接

EventSocketer的构造函数EventSocketer(SocketConfigure sc)调用它的无参构造函数来初始化事件注册对象和收发数据线程。EventSocketer及其子类使用Connect连接到指定的远程主机,并引发Connecting事件,在Connect函数中使用CreateClientSocket()得到的ISocketer实例ClientSocket,使用GetConnectEventsPooler函数得到一个EventPool实例,使用ClientSocketConnect函数使用实际Socket套接字的异步连接方法连接到服务器。连接成功之后,在OnConnected函数中引发AfterConnected事件,并启动ReceiveSend的收发线程,开始收发网线数据。

  • 接收

在连接成功之后,EventSocketer的私有成员mbrWaitForDisconnect会被置为网络状态,一般情况下为true。在Receive线程中,会使用while循环检查该状态值,为true时检查ClientSocketAvailable值,该值大于 0 时,重置lostCount计数为1024,并调用GetReceiveEventsPooler函数得到EventPool池的实例,并使用Pop(SocketConfigure config)函数中获取用于进行异步接收的SocketAsyncEventArgs实例,在OnReceived函数中将,使用SocketEventArgs复制收到的网络数据,使用MessagePool的实例IncommeMessagePushMessage将网络数据存入接收数据缓存,将性能计数项增加实际数字,并引发Recevied事件。异步接收完成之后,检查ClientSocket.SocketUnAvailable属性检查网络状态,在为true时,连接暂时不可用,对lostCount逐减,减到0时,连接丢失,调用Disconnect函数断开连接。

  • 发送

在连接成功之后,EventSocketer的私有成员mbrWaitForDisconnect会被置为网络状态,一般情况下为true。在Send线程中,会使用while循环检查该状态值,为true时调用GetSendEventsPooler函数得到EventPool池的实例,并使用Pop(SocketConfigure config)函数中获取用以异步发送的SocketAsyncEventArgs实例,使用MessagePool的实例OutMessageGetMessage函数获取需要发送的数据,使用ClientSocketSend异步发送,在发送成功之后,调用OnSended函数,进行发送后处理,并引发Sended事件,将性能计数项增加实际数字。

  • 断开

在收发数据的线程中,如果连接丢失,通过调用Disconnect函数,或直接调用该函数,将调用ClientSocketDisconnect函数进行异步断开连接,并调用MessagePool的实例的ForceClose函数,取消队列等待信号,调用ResetConnectAsyncEventsResetDisconnectAsyncEventsResetReceiveAsyncEventsResetSendAsyncEvents取消EventPool池的等待信号。

  • 错误处理

在所有的与OnSended相似的函数处理中,会检查SocketAsyncEventArgsSocketError值,不为SocketError.Success时,以该SocketAsyncEventArgs为参数构造SocketErrorArgs实例,并引发Error事件。如果SocketConfigure的实例的OnErrorContinue属性为false,后续处理将不会继续。


A .Net Async Eventabled Server or Client Socketer compmont. ##Event List: ###Connectting: For begin connect to a remote host ###Connected: For end of connect ###Recevied: For recevie some network bytes ###Sended: For send some bytes ###Disconnected: For disconnectd ###Error: For a network error


EOF

asyncsocketer's People

Contributors

toiiggww avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.