Implement an event-based notification .NET socket

zhaozj2021-02-16  51

I used to learn MFC, I have used it in the packaging of Winsock's two CSocket and CasyncSocket, I always think which event notification is more good, especially when the two parties transmit and receive data do not have certain regularities. Comparison Useful, although it does not need to be implemented, you need to loop the status of the socket or block waiting, if you do these trivial work every time, it is not very troublesome, so I want to make these functions. One package. Of course, it is the best choice to use Delegate and Event in .NET. Below is some details I implemented:

Let's talk about thinking: In fact, this is still very simple, it is estimated that the masters will dismiss it. ^ _ ^. When the socket starts, such as starting to listen, start the connection, start a state of the thread non-stop detection socket, triggering this event when the status condition of a certain event is satisfied, know the specific detection socket The method of state, please see it down.

The function that may need to be used in the usual use of the socket is: connection success or failure notification, there is a notification of the listening connection, there is a notification that can be accepted, the socket is closed Notice, there is also a notification that is idle can send data, and I will make the above functions, but I think the last side is not big.??

My Socket class is inherited from system.net.sockets.socket, class TCPEventSocket, as follows:

First, the principal type of the incident, specifically see the code

Public Delegate Void AcceptConnectionHandler ();

Public Delegate Void ConnectCompletedHandler (BOOL Connected); // Connected Indicates whether the connection is successful

Public delegate void DatacansendHandler ();

Public Delegate Void DataCanReceiveHandler (int buffersize); // Currently acceptable data

Public Delegate Void SocketCloseDHandler ();

What types of types should be able to see, but also claim that the corresponding events

Public Event AcceptConnectionHandler AcceptConnection;

Public Event ConnectCompleted;

Public Event DatacansendHandler Datacansend;

Public Event DataCanreceiveHandler DatacanReceive;

Public evenet socketclosedhandler socketclosed;

Also added several virtual methods, these methods used to trigger events

Protected virtual void onacceptconnection ();

Protected Virtual Void OnConnectCompleted (BOOL Connected)

protected virtual void ondatacansend ()

Protected Virtual Void OnDataCanReceive (int buffersize)

protected virtual void onsocketclosed ()

In order to start the detection thread in the appropriate consociation, I rewrite several basic classes:

New Public Void Listen (int Backlog)

{

Base.listen (backlog);

SockState = SocketState.listenning;

IF (! Checkthread.isalive)

Checkthread.start ();

}

New public void connect (endpoint remoteep) {

Try

{

Base.Connect (Remoteep);

this.blocking = false; // Set the non-blocking state so that the efficiency of event notification

IF (! Checkthread.isalive)

Checkthread.start ();

}

Catch (socketexception)

{

OnConnectCompleted (False);

}

}

Both of these two methods should be called first, so they have their launch detection threads, and the thread is started to stop, so I rewritten the Close method.

New public void close ()

{

If (checkthread.isalive) // First stop thread and then turn off the connection

Checkthread.abort ();

Base.close ();

SockState = SocketState.disconnected;

Onsetclosed ();

}

So the remaining work is how to detect the socket, the Socket class has a SELECT static method, it can detect a lot of sockets, but only one, so directly with the Socket's Poll method, POLL specific Usage can be seen by MSDN, I use the code to explain my detection method for sockets.

While (TRUE) // cycle check

{

IF (SockState == SocketState.disconnected) // If there is no connection

{

IF (Poll (500, SelectMode.selectWrite))

OnConnectCompleted (TRUE); // If you can be written, the connection is successful

}

Else IF (SockState == SocketState.Listenning)

{

IF (Poll (500, SelectMode.selectread) // If there is a data readable in the listening state, it indicates that there is already connected to accept the Accepting connection.

OnacceptConnection ();

}

Else // SockState = SocketState.Connected

{

IF (Poll (500, SelectMode.selectWrite) // If there is a write state, it indicates that you can send data.

OnDatacansend ();

IF (Poll (500, SelectMode.selectread) // If there is a readable state

{

IF (Available> 0) // If there is a data readable representation, you can call Receive Accept data.

OnDataCanReceive (Available);

Else

{

OnsocketClosed (); // No data readable means connection has been closed

Break;

}

}

}

// If there is no connection and have an error, the connection failed

IF (SockState == SocketState.disconnected && Poll (500, SelectMode.SelectElectEn)

OnConnectCompleted (False);

}

The ONXXX method here is a method of executing an event notification, and the derived class can overload these methods directly obtain event notification without the need for an event notification processing function (similar to the MFC's onaccept, etc.). However, the corresponding method of the derived function set is called the corresponding method. Unfortunately, I didn't rewrite the socket.accept method, so that it returned to a TcPeventSocket, which should be more complete, but I don't know how to do ^ _ ^, if you know, please welcome the advice.

The event notification mechanism has been basically completed, and the lack of only a large number of tests (I have done a few simple tests, embarrassing !! ^ _ ^) If there is a problem, please contact YZX110@bit.edu.cn

转载请注明原文地址:https://www.9cbs.com/read-22770.html

New Post(0)