Original URL: Http://www.cnblogs.com/csdev
Networkcomms is a C # language written by TCP/UDP communication framework The author is the British used to charge the current author has open source address is: Https://github.com/MarcFletcher/NetworkComms.Net
When using the NETWORKCOMMS framework to communicate, the client sends a message and the server-side replies to the message.
Ensure that the received message is from a user, not a malicious message, and that a first-byte authentication mechanism can be added to the NETWORKCOMMS framework.
The idea is this:
If the message received by the server is a logon message, it is processed and if the login succeeds, the first byte validation succeeds and the server can process the subsequent messages on the current connection.
If the server does not receive a logon message, and only receives other types of messages, it is not processed.
This ensures that incoming messages are sent to the logged-in user, ensuring communication security.
The NETWORKCOMMS framework itself does not have this mechanism, so let's join this mechanism.
First step: Add firstverify members to the ConnectionInfo class
Public BOOL Get set; }
The second step, in the Networkcomms class, joins the Firstverify member
Public Static BOOL Get set; }
Step three, modify the Reservedpackettype enumeration type
This type is the type of message built into the communication framework, which is a reserved message type, and this type of data is handled with high precedence.
enum Reservedpackettype { Confirmation, checksumfailresend, alivetestpacket, Connectionsetup, unmanaged, Nestedpacket, userlogin, }
Userlogin is our login type.
Fourth step: Modify the relevant methods in the ConnectionIncomingData.cs file The red part is new.
If it is an internal type, direct processing
if(isreservedpackettype) {#ifWindows_phone | | Netfx_corequeueitempriority Priority=Queueitempriority.normal;#elsequeueitempriority Priority=(queueitempriority) Thread.CurrentThread.Priority;#endifPriorityqueueitem Item=NewPriorityqueueitem (priority, This, Toppacketheader, Packetbuilder.readdatasection (Packetheadersize, Toppacketheader.totalpayloadsize), Incomingpacketsendreceiveoptions); if(networkcomms.loggingenabled) NetworkComms.Logger.Trace ("... handling packet type '"+ Toppacketheader.packettype +"' inline. Loop Index-"+ loopcounter.tostring () +packetseqnumstr); Networkcomms.completeincomingitemtask (item); } Else { If verification is required and validation passes or does not need to be validated for processing if ((networkcomms.firstverify = = True && this. Connectioninfo.firstverify = = true) | | Networkcomms.firstverify==false) {
Client Login Code:
Roughly the following userlogin is the type of message logged in
Userlogincontract is a custom class that can be defined according to the actual situation
Userlogincontract logincontract = Newtcpconnection.sendreceiveobject<userinfo, UserLoginContract> (" Userlogin " " Resuserlogin " userInfo);
Server-side:
Networkcomms.firstverify = true;
This means that first byte authentication is enabled, and if set to false, no first byte validation is performed.
In the method of handling logins
Process User Login <4>
private void Incomingloginhandler (Packetheader header, Connection Connection, UserInfo UserInfo)
{
Verifying user logon
If the login succeeds
Set the first byte verification success
Connection. Connectioninfo.firstverify = true;
}
Original URL: Http://www.cnblogs.com/csdev
A first-byte validation of the open source. NET Communication Framework NETWORKCOMMS framework