Multi-threading in Silverlight

I’ve been working with Sockets in Silverlight over the past couple of days, in between getting the Pirates game to a workable Silverlight 2 state. One thing I came across today which sent me round and round was BeginInvoke. Anyone who has programmed multi-threaded applications in Windows has used this, mostly to execute code on the UI thread from events.

Anyway, controls in Silverlight 2 do not have the Invoke or BeginInvoke methods but controls do have a Dispatcher reference which lives in System.Windows.Threading. WPF programmers are probably already used to this but it was new for me. The Dispatcher allows you to check to see if you can access the current object on the current thread.

One thing to note about the Dispatcher is that it has a method called CheckAccess() that returns true if you can access the control on the current thread of false if you are not on the control’s thread. The CheckAccess() method is decorated with a EditorBrowsableAttribute and has it’s value set to Never. This prevents us from being able to see the method via intellisense in Visual Studio but does not prevent the method from being used. Not sure why the folks at MS wanted to hide this method and the documentation does not provide an explanation. The DependancyObject class also provides a CheckAccess method that is also hidden from the editor and just calls the Dispatcher method of the current object. Maybe it’s not documented because it might be removed in the future, it is in beta after all.

So, to sum up, if you need to be able to invoke methods on a control’s thread from a background thread you can do the following: (snippet from socket testing)

        private delegate void AppendTextDelegate(string text);
        public void AppendText(string text)
        {
            if (txtChat.CheckAccess())
            {
                // Append the message to the chat buffer.
                _content.Append(text).Append(Environment.NewLine);

                // Re-post the contents of the chat buffer to the chat text and scroll to
                // the bottom.
                txtChat.Content = _content.ToString();
                txtChat.ScrollToVerticalOffset(txtChat.ScrollableHeight);
            }
            else
            {
                txtChat.Dispatcher.BeginInvoke(new AppendTextDelegate(this.AppendText), text);
            }
        }

The _content control is just a StringBuilder that actually holds the content from the chat control.

Again, Silverlight 2 is in beta 1 so maybe this will change in the future.

Silverlight 2 Sockets

I started working with Sockets in Silverlight 2 tonight and got my Silverlight application connected to the server and sending and receiving commands. I am going to write some code to testing sending commands from the server at the same time commands are coming from the client to see how the app and my code will handle it.

I will post some code as I get further along. Also, I made more progress on getting the Pirates game upgraded to Silverlight 2.

HttpWebRequest Helper for Silverlight 2

I wrote this helper class to assist with processing an Http Request via Silverlight 2. Since we can’t provide a custom class by deriving from BrowserHttpWebRequest because it is internal I wrote this helper that will create and send the request and raise an event when the request completes. I am using it in my current game to send commands to the server and process the responses.

Edit: I revised the class slightly based on finding from Mike Briseno:

Edit: Some folks have experienced issues with this class after installing Silverlight 2 Beta 2. Be sure to check your domain access policy xml file for the new changes in beta 2 but if you experience issues feel free to contact me and we can try and figure them out.

Edit: Added HttpUtility.UrlEncode to the post values before writing them to the stream. Thanks for the suggestion Stefan!

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net;

namespace Lionsguard
{
    public class HttpHelper
    {
        private HttpWebRequest Request { get; set; }
        public Dictionary<string, string> PostValues { get; private set; }

        public event HttpResponseCompleteEventHandler ResponseComplete;
        private void OnResponseComplete(HttpResponseCompleteEventArgs e)
        {
            if (this.ResponseComplete != null)
            {
                this.ResponseComplete(e);
            }
        }

        public HttpHelper(Uri requestUri, string method, params KeyValuePair<string, string>[] postValues)
        {
            this.Request = (HttpWebRequest)WebRequest.Create(requestUri);
            this.Request.ContentType = "application/x-www-form-urlencoded";
            this.Request.Method = method;
            this.PostValues = new Dictionary<string, string>();
            if (postValues != null && postValues.Length > 0)
            {
                foreach (var item in postValues)
                {
                    this.PostValues.Add(item.Key, item.Value);
                }
            }
        }

        public void Execute()
        {
            this.Request.BeginGetRequestStream(new AsyncCallback(HttpHelper.BeginRequest), this);
        }

        private static void BeginRequest(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                if (helper.PostValues.Count > 0)
                {
                    using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar)))
                    {
                        foreach (var item in helper.PostValues)
                        {
                            writer.Write("{0}={1}&", item.Key, HttpUtility.UrlEncode(item.Value));
                        }
                    }
                }
                helper.Request.BeginGetResponse(new AsyncCallback(HttpHelper.BeginResponse), helper);
            }
        }

        private static void BeginResponse(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar);
                if (response != null)
                {
                    Stream stream = response.GetResponseStream();
                    if (stream != null)
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            helper.OnResponseComplete(new HttpResponseCompleteEventArgs(reader.ReadToEnd()));
                        }
                    }
                }
            }
        }
    }

    public delegate void HttpResponseCompleteEventHandler(HttpResponseCompleteEventArgs e);
    public class HttpResponseCompleteEventArgs : EventArgs
    {
        public string Response { get; set; }

        public HttpResponseCompleteEventArgs(string response)
        {
            this.Response = response;
        }
    }
}

And this is how I am using it in my app:

        private void ProcessCommand(short cmd, string msg)
        {
            App app = App.Current as App;
            HttpHelper helper = new HttpHelper(app.ServerUri, "POST",
                new KeyValuePair<string, string>("authKey", app.AuthKey),
                new KeyValuePair<string, string>("cmd", cmd.ToString()),
                new KeyValuePair<string, string>("msg", msg));
            helper.ResponseComplete += new HttpResponseCompleteEventHandler(this.CommandComplete);
            helper.Execute();

        }

        private void CommandComplete(HttpResponseCompleteEventArgs e)
        {
            txtAlert.Text = e.Response;
        }

For the VB Developers out there David Thiessen has converted my code to VB:

' Usage....

'Private Sub ProcessCommand(ByVal cmd As Short, ByVal msg As String)
'    Dim app As App = TryCast(App.Current, App)
'    Dim helper As New HttpHelper(app.ServerUri, "POST", New KeyValuePair(Of String, String)("authKey", app.AuthKey), New KeyValuePair(Of String, String)("cmd", cmd.ToString()), New KeyValuePair(Of String, String)("msg", msg))
'    AddHandler helper.ResponseComplete, AddressOf CommandComplete
'    helper.Execute()

'End Sub

'Private Sub CommandComplete(ByVal e As HttpResponseCompleteEventArgs)
'    txtAlert.Text = e.Response
'End Sub

''' <summary>
''' Converted C# code from http://www.cameronalbert.com/post/2008/03/HttpWebRequest-Helper-for-Silverlight-2.aspx
''' </summary>
''' <remarks></remarks>
Public Class HttpHelper

    Private Property Request() As HttpWebRequest
        Get
            Return _Request
        End Get
        Set(ByVal value As HttpWebRequest)
            _Request = value
        End Set
    End Property
    Private _Request As HttpWebRequest

    Public Property PostValues() As Dictionary(Of String, String)
        Get
            Return _PostValues
        End Get
        Private Set(ByVal value As Dictionary(Of String, String))
            _PostValues = value
        End Set
    End Property
    Private _PostValues As Dictionary(Of String, String)

    Public Event ResponseComplete As HttpResponseCompleteEventHandler
    Private Sub OnResponseComplete(ByVal e As HttpResponseCompleteEventArgs)
        RaiseEvent ResponseComplete(e)
    End Sub

    Public Sub New(ByVal requestUri As Uri, ByVal method As String, ByVal ParamArray postValues As KeyValuePair(Of String, String)())
        Me.Request = DirectCast(WebRequest.Create(requestUri), HttpWebRequest)
        Me.Request.ContentType = "application/x-www-form-urlencoded"
        Me.Request.Method = method
        Me.PostValues = New Dictionary(Of String, String)()
        For Each item In postValues
            Me.PostValues.Add(item.Key, item.Value)
        Next
    End Sub

    Public Sub Execute()
        Me.Request.BeginGetRequestStream(New AsyncCallback(AddressOf HttpHelper.BeginRequest), Me)
    End Sub

    Private Shared Sub BeginRequest(ByVal ar As IAsyncResult)
        Dim helper As HttpHelper = TryCast(ar.AsyncState, HttpHelper)
        If helper IsNot Nothing Then
            Using writer As New StreamWriter(helper.Request.EndGetRequestStream(ar))
                For Each item In helper.PostValues
                    writer.Write("{0}={1}&", item.Key, HttpUtility.UrlEncode(item.Value))
                Next
            End Using
            helper.Request.BeginGetResponse(New AsyncCallback(AddressOf HttpHelper.BeginResponse), helper)
        End If
    End Sub

    Private Shared Sub BeginResponse(ByVal ar As IAsyncResult)
        Dim helper As HttpHelper = TryCast(ar.AsyncState, HttpHelper)
        If helper IsNot Nothing Then
            Dim response As HttpWebResponse = DirectCast(helper.Request.EndGetResponse(ar), HttpWebResponse)
            If response IsNot Nothing Then
                Dim stream As Stream = response.GetResponseStream()
                If stream IsNot Nothing Then
                    Using reader As New StreamReader(stream)
                        helper.OnResponseComplete(New HttpResponseCompleteEventArgs(reader.ReadToEnd()))
                    End Using
                End If
            End If
        End If
    End Sub
End Class

Public Delegate Sub HttpResponseCompleteEventHandler(ByVal e As HttpResponseCompleteEventArgs)

Public Class HttpResponseCompleteEventArgs
    Inherits EventArgs

    Public Property Response() As String
        Get
            Return _Response
        End Get
        Set(ByVal value As String)
            _Response = value
        End Set
    End Property
    Private _Response As String

    Public Sub New(ByVal response As String)
        Me.Response = response
    End Sub

End Class

 

Silverlight 2.0 and My Game Engine

I got the chat portion of the game engine working with Silverlight 2.0 tonight. I am just using web services to send the commands back and forth for now. Once I complete incorporating the other portions of the engine into Silverlight I will start playing around with sockets. 

Data Format for Socket Server Packets

For my PBBG Engine I started off programming it all for the web and AJAX. Recently I added support for sockets by porting over my socket server code I wrote for a Flash server and incorporating the code into the PBBG Engine. In a previous post about my PBBG Engine Architecture I outlined my plans for how the socket and web pieces would work together. I spent some time today refining my command pipeline and server protocol for the socket side of the engine and will probably utilize that on the web side as well.

I decided on the following format for the data packets that will go to and from the server and client. Since it is just bytes going back and forth and I wanted to keep the data streams as small as possible I decided to abandon the string based command protocol and use this instead:

The first 2 bytes of the packet will be used to store an Int16 (short) value that indicates the command to execute. I have an event being raised from the game engine that allows the implementing libraries to set the game commands. This was a Dictionary<string, ICommand> but I am going to change it to a Dictionary<short, ICommand>. That will save me some bytes back and forth without passing the command names. I can have the clientsend the proper short value for the command typed into the console or just have the links and events in the client supply the proper short value.

The next 4 bytes will be the integer length value of the message. This will allow me to know when this message ends and the next one begins.

All the bytes following up to the length value will be the actual message sent to the client or sent to the server.

The server is already validating commands, I will just need to modify to validate these bytes and watch for buffer over runs. I still need to work out validating that a logged in user is sending the information but I will work out something, maybe have the client send the encrypted authentication key to the server after a connection is opened but before commands can be processed.