TCP Server
Server class diagram
Here I have imitated the ihttphandler of webfrom. I have designed an itcphandler to provide it to servicelistener for external parsing. The implementation class is keymethodhandler.
The tcpcontext of the message contains the tcprequest and tcpresponse. The tcprequest and the message structure are the same. The tcpendpoint obtained from the socket is added. tcpresponse is only a callback status and message (not used currently ).
Standard examples on servicelistener, buffermanager, socketasynceventargspool and mdsn are similar. In principle, they are designed to reuse the buffer and socketasynceventargs objects to save memory overhead.
Review socket code writing in socketasynceventargs Mode
var e = new SocketAsyncEventArgs();
e.Completed += this.OnAccept;
e.AcceptSocket = null;
...
private void ProcessAccept(SocketAsyncEventArgs e){
...
bool willRaiseEvent = this.listener.AcceptAsync(e);
if (!willRaiseEvent)
{
this.OnAccept(null, e);
}
}
...
private void OnAccept(object sender, SocketAsyncEventArgs e)
{
try
{
if (e.SocketError != SocketError.Success)
{
...
return;
}
Socket client = e.AcceptSocket;
if (client == null)
{
...
return;
}
var asyncEventArg = eventPool.Pop();
if (asyncEventArg == null)
{
...
return;
}
var token = (AsyncUserToken)asyncEventArg.UserToken;
token.Socket = client;
token.EndPoint = (IPEndPoint)client.RemoteEndPoint;
bool willRaiseEvent = client.ReceiveAsync(asyncEventArg);
if (!willRaiseEvent)
{
this.ProcessReceive(asyncEventArg);
}
}
catch (Exception err)
{
Debug.Print("Accept exception,{0}", err);
}
finally
{
e.AcceptSocket = null;
this.ProcessAccept(e);
}
}
Command Parsing
Class Diagram of Command Parsing
Keymethodhandler includes two icommand interfaces, which are implemented by keyboardcommand to trigger direct button events, and mediacommand to trigger media buttons. Both classes contain the ikeyboardhook interface, which is used to call the system interface.
Media keys are generally combined with shortcut keys and can be configured. The configuration class is mediamethodconfig.
Call the system interface to trigger the keyboard:
public interface IKeyBoardHook { void PressKey(Keys keyCode); void PressKey(Keys[] keyCode); } public sealed class KeyBoardHook : IKeyBoardHook { private static readonly KeyBoardHook instance = new KeyBoardHook(); public static KeyBoardHook Instance { get { return instance; } } private KeyBoardHook() { } private enum KeyBoard { KeyDown = 0x0001, KeyUp = 0x0002 } public void PressKey(Keys keyCode) { PressKey(new[] { keyCode }); } public void PressKey(Keys[] keyCodes) { if (keyCodes == null || keyCodes.Length == 0) return; int length = keyCodes.Length; for (int i = 0; i < length; i++) { PutDownKey(keyCodes[i]); } Thread.SpinWait(5000); for (int i = length - 1; i >= 0; i--) { ReceiveKey(keyCodes[i]); } } private static void PutDownKey(Keys keyCode) { KeyboardEvent((byte)keyCode, 0, KeyBoard.KeyDown, 0); } private static void ReceiveKey(Keys keyCode) { KeyboardEvent((byte)keyCode, 0, KeyBoard.KeyUp, 0); } [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "keybd_event")] private static extern void KeyboardEvent( byte bVk, byte bScan, KeyBoard dwFlags, int dwExtraInfo ); }