SilverlightShow: SocketsLight - Silverlight Sockets Framework Comments http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples SilverlightShow.net http://www.rssboard.org/rss-specification Argotic Syndication Framework 2008.0.2.0, http://www.codeplex.com/Argotic en-US estoychev@completit.com (Emil Stoychev) Re: SocketsLight - Silverlight Sockets Framework <p>This thing saved me a lot of time, mostly. I ran into an issue where clients were seemingly randomly receiving incomplete messages and throwing deserialization exceptions. What I thought was an issue with this project was actually a sleepy IIS problem.</p> <p> If you app has long lived clients and relatively infrequence web requests turn off IIS worker process idle timeout.</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment6425 Armadous http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Tue, 09 Aug 2011 17:26:49 GMT RE: SocketsLight - Silverlight Sockets Framework <p>There is some bugs.</p> <p>Now funct.</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment4212 dejan http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Wed, 11 Aug 2010 12:04:52 GMT RE: SocketsLight - Silverlight Sockets Framework <p>Same issue as peter. By big objects  packets doesnt reach for some clients  ( > 20 packets  ) . </p> <p>thx,</p> <p>dejan</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment4195 dejan http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Mon, 09 Aug 2010 10:05:39 GMT RE: SocketsLight - Silverlight Sockets Framework <p>VERY NICE work Alexey!!</p> <p>We are using sockets with Silverlight but the code is never tidy enough...</p> <p>Your implementation gave us a good direction where to go to...</p> <p>THANKS!</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment4011 Hu Bin http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Mon, 19 Jul 2010 15:39:40 GMT RE: SocketsLight - Silverlight Sockets Framework <p>some times when i use SendMessageAsync in loop to multi users the packet doesnt reach for some clients, when i put Thread.Sleep(300) before sending (in the loop) it work ok, but sometimes it happens again...., how can i be sure that the packets reach 100% ?</p> <p> </p> <p>thx,</p> <p>peter</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment3477 peter http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Fri, 23 Apr 2010 14:30:40 GMT RE: SocketsLight - Silverlight Sockets Framework <p>I do not understand the following piece of the SendMessageAsync method of the MessageClient class.  I assume it has something to do with making the byteframe, but what does adding a 0 every 1024 bytes and a 1 at the very end do?</p> <div class="reCodeBlock" style="border: #7f9db9 1px solid; overflow-y: auto;"> <div style="background-color: #fff;"><span style="margin-left: 0px !important;"><code style="color: #069; font-weight: bold;">byte</code><code style="color: #000;">[] serializedMessage = </code><code style="color: #069; font-weight: bold;">this</code><code style="color: #000;">.messageSerializer.Serialize(message); </code></span></div> <div style="background-color: #f8f8f8;"><span style="margin-left: 0px !important;"><code style="color: #000;">List<</code><code style="color: #069; font-weight: bold;">byte</code><code style="color: #000;">> messageByteList = serializedMessage.ToList(); </code></span></div> <div style="background-color: #fff;"><span><code> </code><span style="margin-left: 3px !important;"> </span></span></div> <div style="background-color: #f8f8f8;"><span style="margin-left: 0px !important;"><code style="color: #069; font-weight: bold;">for</code> <code style="color: #000;">(</code><code style="color: #069; font-weight: bold;">int</code> <code style="color: #000;">i = 1023; i < serializedMessage.Length; i += 1023) </code></span></div> <div style="background-color: #fff;"><span style="margin-left: 0px !important;"><code style="color: #000;">{ </code></span></div> <div style="background-color: #f8f8f8;"><span><code>     </code><span style="margin-left: 15px !important;"><code style="color: #000;">messageByteList.Insert(i, 0); </code></span></span></div> <div style="background-color: #fff;"><span><code>     </code><span style="margin-left: 15px !important;"><code style="color: #000;">i++; </code></span></span></div> <div style="background-color: #f8f8f8;"><span style="margin-left: 0px !important;"><code style="color: #000;">} </code></span></div> <div style="background-color: #fff;"><span><code> </code><span style="margin-left: 3px !important;"> </span></span></div> <div style="background-color: #f8f8f8;"><span style="margin-left: 0px !important;"><code style="color: #000;">messageByteList.Add(1);</code></span></div> </div> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment3133 Eric http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Mon, 01 Mar 2010 04:55:20 GMT RE: SocketsLight - Silverlight Sockets Framework <p>VERY NICE work Alexey!!</p> <p>We are using sockets with Silverlight but the code is never tidy enough...</p> <p>Your implementation gave us a good direction where to go to...</p> <p>THANKS!</p> <p> </p> <p>.peter.gfader.</p> <p><a href="http://peitor.blogspot.com/" target="_blank">http://peitor.blogspot.com/</a> </p> <p><a href="http://twitter.com/peitor" target="_blank">http://twitter.com/peitor</a> </p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment3097 Peter Gfader http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Thu, 18 Feb 2010 02:28:00 GMT RE: SocketsLight - Silverlight Sockets Framework simply awesome. thank you for this wonderful library.<div><br /></div><div>to 'bob': it comes with an example in the code... just check it out.</div> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment2801 James Peckham http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Thu, 17 Dec 2009 08:29:20 GMT RE: SocketsLight - Silverlight Sockets Framework <p>nice work Alex.</p> <p>i'm trying to create multi-player tic-tac-toe game using Silverlight 3.  can you supply some example how to use your code in such game?    thanks.</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment2256 bob http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Tue, 18 Aug 2009 14:04:58 GMT RE: SocketsLight - Silverlight Sockets Framework >> <span id="ctl00_cphMiddle_ContentPlaceHolderContent_itemComments_dlstComments_ctl06_lblCommentAuthor"><span id="ctl00_cphMiddle_ContentPlaceHolderContent_itemComments_dlstComments_ctl06_lblCommentAuthor">odanvin</span> <p><span>Thanks for you comment. You'd better write about it at codeplex discussion section. Also I can provide you access to contribute to the project.</span></p> <br /> </span> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1749 Alexey Zakharov http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Tue, 30 Jun 2009 15:18:38 GMT RE: SocketsLight - Silverlight Sockets Framework <p>Thank you for sharing this Alexey,</p> <p>Communication optimization is a key feature in the success of many business applications - nice work.</p> <p>David</p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1747 David Roh http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Tue, 30 Jun 2009 12:26:08 GMT RE: SocketsLight - Silverlight Sockets Framework <p>First of all thanks you for this added value on the sockets. I just want to share reconnection capabilities in case of lost connections. It could happen during web application recycling for example. In this case the messageServer just have to send a Message to all clients before it stops in the Application_End method of the Global.asax.cs. Then clients will be able to raise the ConnectionLost event and try to reconnect. Find below my the added feature on the MessageClient:</p> <div style="border-right: #7f9db9 1px solid; border-top: #7f9db9 1px solid; font-size: 11px; overflow: auto; border-left: #7f9db9 1px solid; line-height: 100%! important; border-bottom: #7f9db9 1px solid; font-family: courier new; background-color: white"> <table style="border-top-width: 0px; border-left-width: 0px; margin: 2px 0px; width: 99%; border-bottom: #eee 0px solid; border-collapse: collapse; background-color: #fff; border-right-width: 0px" cellspacing="0" cellpadding="0"> <colgroup><col style="padding-left: 10px; font-size: 11px; border-bottom: #f7f7f7 1px solid; font-family: courier new; white-space: nowrap" /></colgroup> <tbody> <tr> <td><span style="font-size: 11px">// --------------------------------------------------------------------------------------------------------------------  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">// <span style="color: blue"><</span><span style="font-size: 11px">copyright </span><span style="color: red">file</span><span style="font-size: 11px">=</span><span style="color: blue">"MessageClient.cs"</span><span style="font-size: 11px"> </span><span style="color: red">company</span><span style="font-size: 11px">=</span><span style="color: blue">"Alexey Zakharov"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>//   This file is part of SocketsLight Framework.  </td> </tr> <tr> <td style="background-color: #f7f7f7">//  </td> </tr> <tr> <td>//   SocketsLight Framework is free software: you can redistribute it and/or modify  </td> </tr> <tr> <td style="background-color: #f7f7f7">//   it under the terms of the GNU General Public License as published by   </td> </tr> <tr> <td>//   the Free Software Foundation, either version 3 of the License, or   </td> </tr> <tr> <td style="background-color: #f7f7f7">//   (at your option) any later version.   </td> </tr> <tr> <td>//     </td> </tr> <tr> <td style="background-color: #f7f7f7">//   SocketsLight Framework is distributed in the hope that it will be useful,   </td> </tr> <tr> <td>//   but WITHOUT ANY WARRANTY; without even the implied warranty of   </td> </tr> <tr> <td style="background-color: #f7f7f7">//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   </td> </tr> <tr> <td>//   GNU General Public License for more details.   </td> </tr> <tr> <td style="background-color: #f7f7f7">//     </td> </tr> <tr> <td>//   You should have received a copy of the GNU General Public License   </td> </tr> <tr> <td style="background-color: #f7f7f7">//   along with Foobar.  If not, see http://www.gnu.org/license.  </td> </tr> <tr> <td>// <span style="color: blue"></</span><span style="font-size: 11px">copyright</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>//   Defines the MessageClient type.  </td> </tr> <tr> <td style="background-color: #f7f7f7">// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>// --------------------------------------------------------------------------------------------------------------------  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>namespace Witcraft.SocketsLight.Client  </td> </tr> <tr> <td style="background-color: #f7f7f7">{  </td> </tr> <tr> <td>    using System;  </td> </tr> <tr> <td style="background-color: #f7f7f7">    using System.Collections.Generic;  </td> </tr> <tr> <td>    using System.ComponentModel;  </td> </tr> <tr> <td style="background-color: #f7f7f7">    using System.Linq;  </td> </tr> <tr> <td>    using System.Net;  </td> </tr> <tr> <td style="background-color: #f7f7f7">    using System.Net.Sockets;  </td> </tr> <tr> <td>    using System.Windows;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>    using Witcraft.SocketsLight.Server;  </td> </tr> <tr> <td style="background-color: #f7f7f7">using System.Windows.Threading;  </td> </tr> <tr> <td>    using System.Diagnostics;  </td> </tr> <tr> <td style="background-color: #f7f7f7">    using System.Threading;  </td> </tr> <tr> <td>    using System.Windows.Browser;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>    /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">    /// Send and recives messages.  </td> </tr> <tr> <td>    /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">    public class MessageClient : IMessageClient  </td> </tr> <tr> <td>    {  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Used to aggregate message frames.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private List<span style="color: blue"><</span><span style="font-size: 11px">byte</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">messageBuffer</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> List</span><span style="color: blue"><</span><span style="font-size: 11px">byte</span><span style="color: blue">></span><span style="font-size: 11px">();  </span></td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Serialize and deserialize messages.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private IMessageSerializer messageSerializer;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Port which will be used for message transfer.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private int port;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Socket which will be used for message transfer.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private Socket socket;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Dispatcher enabling code execution on UI thread.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private Dispatcher dispatcher;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Web client attempting connection re-establishment.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private WebClient reconnectClient;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Timer controlling the connection re-establishment.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private Timer reconnectTimer;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Date and time when connection re-establishment starts.   </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private DateTime reconnectTime;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Period between each connection re-establishment attempt.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private TimeSpan reconnectPolling;  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Period during connection re-establishment are attempted.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private TimeSpan reconnectTimeout;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Initializes a new instance of the <span style="color: blue"><</span><span style="font-size: 11px">see </span><span style="color: red">cref</span><span style="font-size: 11px">=</span><span style="color: blue">"MessageClient"</span><span style="font-size: 11px">/> class.  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"port"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"messageSerializer"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public MessageClient(int port, IMessageSerializer messageSerializer)  </td> </tr> <tr> <td style="background-color: #f7f7f7">            : this(port, messageSerializer, null)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Initializes a new instance of the <span style="color: blue"><</span><span style="font-size: 11px">see </span><span style="color: red">cref</span><span style="font-size: 11px">=</span><span style="color: blue">"MessageClient"</span><span style="font-size: 11px">/> class.  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"port"</span><span style="font-size: 11px">>The port.</span><span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"messageSerializer"</span><span style="font-size: 11px">>The message serializer.</span><span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"dispatcher"</span><span style="font-size: 11px">>The dispatcher.</span><span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public MessageClient(int port, IMessageSerializer messageSerializer, Dispatcher dispatcher)  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            <span style="color: red">this.port</span><span style="font-size: 11px"> = port;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            <span style="color: red">this.messageSerializer</span><span style="font-size: 11px"> = messageSerializer;  </span></td> </tr> <tr> <td>            <span style="color: red">this.socket</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">              </td> </tr> <tr> <td>            <span style="color: red">this.dispatcher</span><span style="font-size: 11px"> = dispatcher;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            <span style="color: red">this.reconnectPolling</span><span style="font-size: 11px"> = </span><span style="color: blue">TimeSpan</span><span style="font-size: 11px">.FromSeconds(5);  </span></td> </tr> <tr> <td>            <span style="color: red">this.reconnectTimeout</span><span style="font-size: 11px"> = </span><span style="color: blue">TimeSpan</span><span style="font-size: 11px">.FromSeconds(30);  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            <span style="color: red">this.reconnectClient</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> WebClient();  </span></td> </tr> <tr> <td>            this.reconnectClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ReconnectClient_DownloadStringCompleted);  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Prevents a default instance of the <span style="color: blue"><</span><span style="font-size: 11px">see </span><span style="color: red">cref</span><span style="font-size: 11px">=</span><span style="color: blue">"MessageClient"</span><span style="font-size: 11px">/> class from being created.  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private MessageClient()  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td>          </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Occurs when client has connected to message server  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public event EventHandler<span style="color: blue"><</span><span style="font-size: 11px">AsyncCompletedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> ConnectCompleted;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Occure if connection with message server has been lost.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        public event EventHandler<span style="color: blue"><</span><span style="font-size: 11px">EventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> ConnectionLost;  </span></td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Occurs when new message have been recieved.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public event EventHandler<span style="color: blue"><</span><span style="font-size: 11px">MessageRecievedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> MessageRecieved;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Occurs when message is sent  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        public event EventHandler<span style="color: blue"><</span><span style="font-size: 11px">AsyncCompletedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> SendCompleted;  </span></td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Indicates if client is connected to message server.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public bool Connected  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            get  </td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                return this.socket.Connected;  </td> </tr> <tr> <td style="background-color: #f7f7f7">            }  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Connect to message server async.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"state"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        public void ConnectAsync()  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            this.ConnectAsync(null);  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Connect to message server async.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"state"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        public void ConnectAsync(object state)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (!this.Connected)  </td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                var <span style="color: red">endPoint</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, this.port);  </span></td> </tr> <tr> <td>                var <span style="color: red">args</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> SocketAsyncEventArgs { </span><span style="color: red">UserToken</span><span style="font-size: 11px"> = </span><span style="color: blue">state</span><span style="font-size: 11px">, </span><span style="color: red">RemoteEndPoint</span><span style="font-size: 11px"> = </span><span style="color: blue">endPoint</span><span style="font-size: 11px"> };  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                args.Completed += this.OnSocketConnected;  </td> </tr> <tr> <td>                this.socket.ConnectAsync(args);  </td> </tr> <tr> <td style="background-color: #f7f7f7">            }  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Reconnects to message server.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void Reconnect()  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            //start reconnect timer  </td> </tr> <tr> <td>            <span style="color: red">reconnectTime</span><span style="font-size: 11px"> = </span><span style="color: blue">DateTime</span><span style="font-size: 11px">.Now;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            Debug.WriteLine(string.Format("Connection re-establishment started at {0}", reconnectTime.ToString("o")));  </td> </tr> <tr> <td>            <span style="color: red">this.reconnectTimer</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> Timer(this.Reconnect  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                , null  </td> </tr> <tr> <td>                , TimeSpan.Zero  </td> </tr> <tr> <td style="background-color: #f7f7f7">                , reconnectPolling);  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// Reconnects to message server.  </td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"state"</span><span style="font-size: 11px">>The state.</span><span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private void Reconnect(object state)  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            //consider reconnect timeout  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if ((DateTime.Now - reconnectTime) <span style="color: blue">></span><span style="font-size: 11px"> reconnectTimeout)  </span></td> </tr> <tr> <td>                throw new TimeoutException(string.Format("Connection re-establishment failed since {0} and during {1}", reconnectTime, reconnectTimeout));  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            //skip web request call if previous request is still busy  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (!reconnectClient.IsBusy)  </td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                Debug.WriteLine(string.Format("Connection re-establishment attempt at {0}", DateTime.Now.ToString("o")));  </td> </tr> <tr> <td>                dispatcher.BeginInvoke(() =<span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                {  </td> </tr> <tr> <td>                    reconnectClient.DownloadStringAsync(HtmlPage.Document.DocumentUri);  </td> </tr> <tr> <td style="background-color: #f7f7f7">                });  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        void ReconnectClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (<span style="color: red">e.Error</span><span style="font-size: 11px"> == null)  </span></td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                //stop reconnect timer  </td> </tr> <tr> <td>                <span style="color: red">reconnectTime</span><span style="font-size: 11px"> = </span><span style="color: blue">DateTime</span><span style="font-size: 11px">.MinValue;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                this.reconnectTimer.Dispose();  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">                //reconnect socket client  </td> </tr> <tr> <td>                dispatcher.BeginInvoke(() =<span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                {  </td> </tr> <tr> <td>                    <span style="color: red">this.socket</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                    this.ConnectAsync();  </td> </tr> <tr> <td>                    Debug.WriteLine(string.Format("Connection re-established in {0}", (DateTime.Now - reconnectTime).ToString()));  </td> </tr> <tr> <td style="background-color: #f7f7f7">                });  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Disconnect from message server.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public void Disconnect()  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            this.socket.Close();  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Send message.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"message"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public void SendMessageAsync(Message message)  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            this.SendMessageAsync(message, null);  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// Send message.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"message"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"userState"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        public void SendMessageAsync(Message message, object userState)  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            if ((<span style="color: red">this.socket</span><span style="font-size: 11px"> == null) || (</span><span style="color: red">this.Connected</span><span style="font-size: 11px"> == false))  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                // TODO : throw exception.  </td> </tr> <tr> <td style="background-color: #f7f7f7">            }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">            var <span style="color: red">args</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> SocketAsyncEventArgs();  </span></td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">            byte[] <span style="color: red">serializedMessage</span><span style="font-size: 11px"> = </span><span style="color: blue">this</span><span style="font-size: 11px">.messageSerializer.Serialize(message);  </span></td> </tr> <tr> <td>            List<span style="color: blue"><</span><span style="font-size: 11px">byte</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">messageByteList</span><span style="font-size: 11px"> = </span><span style="color: blue">serializedMessage</span><span style="font-size: 11px">.ToList();  </span></td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            for (int <span style="color: red">i</span><span style="font-size: 11px"> = </span><span style="color: blue">1023</span><span style="font-size: 11px">; i </span><span style="color: blue"><</span><span style="font-size: 11px"> </span><span style="font-weight: bold; color: black">serializedMessage.Length</span><span style="font-size: 11px">; i += 1023)  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                messageByteList.Insert(i, 0);  </td> </tr> <tr> <td style="background-color: #f7f7f7">                i++;  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            messageByteList.Add(1);  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            args.SetBuffer(messageByteList.ToArray(), 0, messageByteList.Count);  </td> </tr> <tr> <td style="background-color: #f7f7f7">            args.Completed += this.SocketSendCompleted;  </td> </tr> <tr> <td>            <span style="color: red">args.UserToken</span><span style="font-size: 11px"> = </span><span style="color: blue">userState</span><span style="font-size: 11px">;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            this.socket.SendAsync(args);  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void OnConnected(AsyncCompletedEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            EventHandler<span style="color: blue"><</span><span style="font-size: 11px">AsyncCompletedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">handler</span><span style="font-size: 11px"> = </span><span style="color: blue">this</span><span style="font-size: 11px">.ConnectCompleted;  </span></td> </tr> <tr> <td>            if (handler != null)  </td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                handler(this, e);  </td> </tr> <tr> <td style="background-color: #f7f7f7">            }  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private void OnConnectionLost()  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            EventHandler<span style="color: blue"><</span><span style="font-size: 11px">EventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">handler</span><span style="font-size: 11px"> = </span><span style="color: blue">this</span><span style="font-size: 11px">.ConnectionLost;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            if (handler != null)  </td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                handler(this, new EventArgs());  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void OnMessageRecieved(MessageRecievedEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            EventHandler<span style="color: blue"><</span><span style="font-size: 11px">MessageRecievedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">handler</span><span style="font-size: 11px"> = </span><span style="color: blue">this</span><span style="font-size: 11px">.MessageRecieved;  </span></td> </tr> <tr> <td>            if (handler != null)  </td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                handler(this, e);  </td> </tr> <tr> <td style="background-color: #f7f7f7">            }  </td> </tr> <tr> <td>        }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        private void OnSendCompleted(AsyncCompletedEventArgs e)  </td> </tr> <tr> <td style="background-color: #f7f7f7">        {  </td> </tr> <tr> <td>            EventHandler<span style="color: blue"><</span><span style="font-size: 11px">AsyncCompletedEventArgs</span><span style="color: blue">></span><span style="font-size: 11px"> </span><span style="color: red">handler</span><span style="font-size: 11px"> = </span><span style="color: blue">this</span><span style="font-size: 11px">.SendCompleted;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">            if (handler != null)  </td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                handler(this, e);  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"sender"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void OnSocketConnected(object sender, SocketAsyncEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            AsyncCompletedEventArgs asyncArgs;  </td> </tr> <tr> <td>            if (this.Connected)  </td> </tr> <tr> <td style="background-color: #f7f7f7">            {  </td> </tr> <tr> <td>                var <span style="color: red">response</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> byte[1024];  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                e.SetBuffer(response, 0, response.Length);  </td> </tr> <tr> <td>                e.Completed <span style="color: red">-</span><span style="font-size: 11px">= </span><span style="color: blue">this</span><span style="font-size: 11px">.OnSocketConnected;  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                e.Completed += this.OnSocketReceive;  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">                this.socket.ReceiveAsync(e);  </td> </tr> <tr> <td>                <span style="color: red">asyncArgs</span><span style="font-size: 11px"> = </span><span style="color: blue">new</span><span style="font-size: 11px"> AsyncCompletedEventArgs(null, false, e.UserToken);  </span></td> </tr> <tr> <td style="background-color: #f7f7f7">                this.OnConnected(asyncArgs);  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            // TODO: Handle error while connection.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"sender"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void OnSocketReceive(object sender, SocketAsyncEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (<span style="color: red">e.BytesTransferred</span><span style="font-size: 11px"> == 0)  </span></td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                this.OnConnectionLost();  </td> </tr> <tr> <td>                this.socket.Close();  </td> </tr> <tr> <td style="background-color: #f7f7f7">                //reconnect if possible  </td> </tr> <tr> <td>                if (dispatcher != null) this.Reconnect();  </td> </tr> <tr> <td style="background-color: #f7f7f7">                return;  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            this.messageBuffer.AddRange(e.Buffer.Take(e.BytesTransferred - 1));  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (e.Buffer[e.BytesTransferred - 1] <span style="color: blue">></span><span style="font-size: 11px"> 0)  </span></td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                this.OnMessageRecieved(  </td> </tr> <tr> <td>                    new MessageRecievedEventArgs(this.messageSerializer.Deserialize(this.messageBuffer.ToArray())));  </td> </tr> <tr> <td style="background-color: #f7f7f7">                this.messageBuffer.Clear();  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            this.socket.ReceiveAsync(e);  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td> </td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">summary</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"sender"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        /// <span style="color: blue"><</span><span style="font-size: 11px">param </span><span style="color: red">name</span><span style="font-size: 11px">=</span><span style="color: blue">"e"</span><span style="font-size: 11px">>  </span></td> </tr> <tr> <td>        /// <span style="color: blue"></</span><span style="font-size: 11px">param</span><span style="color: blue">></span><span style="font-size: 11px"> </span></td> </tr> <tr> <td style="background-color: #f7f7f7">        private void SocketSendCompleted(object sender, SocketAsyncEventArgs e)  </td> </tr> <tr> <td>        {  </td> </tr> <tr> <td style="background-color: #f7f7f7">            if (<span style="color: red">e.SocketError</span><span style="font-size: 11px"> == SocketError.Success)  </span></td> </tr> <tr> <td>            {  </td> </tr> <tr> <td style="background-color: #f7f7f7">                this.OnSendCompleted(new AsyncCompletedEventArgs(null, false, e.UserToken));  </td> </tr> <tr> <td>            }  </td> </tr> <tr> <td style="background-color: #f7f7f7"> </td> </tr> <tr> <td>            // TODO: Handle error if send is not successful.  </td> </tr> <tr> <td style="background-color: #f7f7f7">        }  </td> </tr> <tr> <td>    }  </td> </tr> <tr> <td style="background-color: #f7f7f7">} </td> </tr> </tbody> </table> </div> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1677 odanvin http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Sat, 20 Jun 2009 20:56:09 GMT RE: SocketsLight - Silverlight Sockets Framework Good work! <p>It is very useful when you try to push something from an application running in full trust mode into your Silverlight application. I have been using sockets to do so in the past one year but I have to say the SocketsLight framework can really simplify the coding and make development efficient.</p> <p>Thanks.</p> <br /> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1620 Lixin http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Thu, 28 May 2009 20:52:37 GMT RE: SocketsLight - Silverlight Sockets Framework <p>>> is this working just over http connection? </p> It uses tcp protocol <p>>> and if it is (by the Timer logic it look that it does), there is a possiblity to create a duplex logic with a always connected port >> (Dont care about firewall blocking) other than http port( http keep-alive or non-http protocol at all) ?</p> Timer is not used for polling. Timer is used in weather example to invoke server method which would broadcast message with new random weather data.<br /> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1618 Alexey http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Thu, 28 May 2009 12:35:58 GMT RE: SocketsLight - Silverlight Sockets Framework <p>Nice Project!</p> <p>it may become a very helpful tool when the perfomance may be an issue.</p> <p>i doubt WCF polling duplex could be more optimized than that, with that tons  of protocol implementation.</p> <p>is this working just over http connection?</p> <p>im asking this cause when firewall and proxy´s may become present, we could have connection problems. </p> <p>and if it is (by the Timer logic it look that it does), there is a possiblity to create a duplex logic with a always connected port (Dont care about firewall blocking) other than http port( http keep-alive or non-http protocol at all) ?</p> <p>thanks and congratulations!</p> <p> </p> http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx#comment1617 Fabio Kaminski http://www.silverlightshow.net/items/SocketsLight-Silverlight-Sockets-Framework.aspx Thu, 28 May 2009 11:59:41 GMT