Connectify

Illustration

I was on business trip for few weeks and that meant that my home wireless network was missing my devices. More importantly, my devices were missing my network. Hotel did have wired Internet, but that solved only laptop connection. My phone was still Internet-less.

Fortunately, few months before that trip, I installed Connectify. It was small router program that requires Windows 7, it was in beta, I had no use for it - naturally I installed it and even updated it regularly. All that without even testing whether it works. Sometimes I surprise my self.

Program’s interface is quite simplistic. Almost every configurable setting is visible on main program screen. Mostly you just need to select password and which device has Internet to share and you are good-to-go. It is quite surprising that you can share your active wireless connection. Performance will go down a little (since your card will need to serve both as wireless client and wireless router) but it will work.

Security settings are limited to WPA2/PSK. No weak WEP but also no RADIUS authentication or anything more advanced. Since this would be probably overkill for most of uses, I cannot hold that against it.

What I miss the most here are statistics. You only get information which devices are connected and on which IP address. There is no information about their current resource spending. I was connected to Internet over 3G USB stick and I shared this connection with two friends. We had limited bandwidth and we were downloading way to much data. It was pain-in-the-ass to find which computer was “leaking” data.

As final conclusion I can only recommend this program. Once you set it up (not a big task to do) you can just forget about it. Prerequisites are not quite clear - Windows 7 and “better” wireless network card (although almost all newer Intel or Broadcom cards will do). But if it works once, it will work every time.

P.S. If you were wondering what black magic are they doing in order for this to work, you can check Virtual Router source code. It is open-source project based on same API functions that became available with rise of Windows 7.

Named Pipes in WCF

One of new things in .NET 3.5 was support for pipes (both named and anonymous). Since in some of my applications I used P/Interop for that exact purpose, it seemed quite logical to upgrade that code into native solution. As I started changing code I also started to hate that implementation.

Although new classes are quite easy to use, troubles start as soon as you start transferring little bigger amounts of data (more that cca 200 bytes). I spent better part of night troubleshooting problems with messages broken into multiple parts although buffer sizes were more than adequate and Message transmission was used.

Everything was worsened by design decision to kill off two things that make life easier when playing with those streams. First thing that I needed was ability to check whether there is more data before I start to read it (PeekNamedPipe). This comes in quite handy to dimension your buffers and generally detect when data stream is dry. I needed this function quite a lot because they also decided to kill timeouts for Read() function. Once you start reading data, there is no stopping. And since there is no way to know whether there is more data awaiting other than actually reading it I had quite a big problem at my hand.

I was on verge or restoring old P/Inteop code when I had revelation. Old project was based on .NET 2.0 and with this upgrade I will move it to 3.5 anyhow - why wouldn’t I use Windows Communication Foundation. I played with it before with great success but here I wanted to see how simple it can be and main goal was just to integrate it in already existing flow.

Old application used notion of sending actions to other party and just giving responses to user and that simplified what I needed to do drastically.

Main thing to do is specifying how your communication interface will look like:

[ServiceContract(Namespace = "http://example.com/Command")]
interface ICommandService {

    [OperationContract]
    string SendCommand(string action, string data);

}

This code I needed in both server and client part of my code. Beautiful thing is that it is quite enough to have this file in both places and there is no need for separate assembly to hold common definitions.

Only thing left to be done in client was creating proxy to actually call that method. I opted for separate class, but I could be as easy written in any other existing class:

class CommandClient {

    private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
    private static readonly string PipeName = "Command";
    private static readonly EndpointAddress ServiceAddress = new EndpointAddress(string.Format(CultureInfo.InvariantCulture, "{0}/{1}", ServiceUri.OriginalString, PipeName));
    private static readonly ICommandService ServiceProxy = ChannelFactory<ICommandService>.CreateChannel(new NetNamedPipeBinding(), ServiceAddress);

    public static string Send(string action, string data) {
        return ServiceProxy.SendCommand(action, data);
    }
}

Server part was little bit more complicated but not significantly so:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class CommandService : ICommandService {
    public string SendCommand(string action, string data) {
        //handling incoming requests
    }
}
static class CommandServer {

    private static readonly Uri ServiceUri = new Uri("net.pipe://localhost/Pipe");
    private static readonly string PipeName = "Command";

    private static CommandService _service = new CommandService();
    private static ServiceHost _host = null;

    public static void Start() {
        _host = new ServiceHost(_service, ServiceUri);
        _host.AddServiceEndpoint(typeof(ICommandService), new NetNamedPipeBinding(), PipeName);
        _host.Open();
    }

    public static void Stop() {
        if ((_host != null) && (_host.State != CommunicationState.Closed)) {
            _host.Close();
            _host = null;
        }
    }
}

Once server application starts just call CommandServer.Start() and all requests from client side will be auto-magically instantiated in CommandService. Whatever you decide to return from that method arrives directly to client. It cannot be simpler than that.

I created small example but do notice that it is a console application and no multi-threading issues are being addressed here. Since WCF is working on separate thread any conversion into Windows application will most probably require playing with delegates and that is subject for some other story.

P.S. Do not forget to add reference to System.ServiceModel assembly.

VHD Attach 1.60 (Beta)

Illustration

Since first version of VHD Attach there were requests for auto-mounting of drives upon startup. This beta finally gets that load of my shoulders. :)

Other change that some will like is possibility to enable/disable context menu items for explorer. Now you can choose whether you want all items there, some of them or none.

You can download beta immediately and final release will follow in a month or so.

Blog.jmedved.com

If everything went as planned. You should read this blog on new address - blog.jmedved.com. Please do update links for RSS feeds.

POSIX Time

If you are working with Unix a lot, you get to be aware of weird time-stamps. That markup is usually called Unix time (although POSIX time is more precise term) and it represents number of seconds from January 1st 1970 (in UTC).

I was handling some timing issues in log and I needed to convert between local and Unix time. Since I got quite annoyed during day, at night I created small utility to handle this simple task.

Both application and full code is available.