WP7 Application Development Notes (6) Server Design

Source: Internet
Author: User
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            );    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.