Tuesday, 30 August 2011

DateTime.ToString() custom format

I always forget which letters represent what date/time format in the ToString() method.
Commonly I use it for logging, and the format is:
DateTime.Now.ToString("yyyyMMdd HH:mm:ss:FFFFFF")
, which results in:
20110830 08:22:26:160586
The full list is available here (MSDN)

Tuesday, 23 August 2011

TCP Load-testing

Just had to whip up a load-tester for a server that communicates with clients via TCP. Can process concurrent TCP connections, and has graphical reporting, and should be easily extensible to dynamically change requests. Made use of the open-source ZedGraph graphing utility, which is pretty damn cool.

Download the source-code here
The ZedGraph binary is located here

Monday, 22 August 2011

BER-TLV Parsing

One of the EMV devices I need to connect to uses the BER-TLV (wiki) protocol for communications. Simply put, data fields are defined by a (T)ag, (L)ength and (V)alue.
  •  The tag field (T) consists of one or more consecutive bytes. It codes a class, a type, and
    a number:

  • The length field (L) consists of one or more consecutive bytes. It codes the length of
    the following field.
  • The value field (V) codes the value of the data object
Sample code removed

Hexidecimal logging

I do a fair bit of serial comms in my current job (interfacing to EMV terminals, printers etc), and needed to log a low-level trace of the binary data transferred to the com port.
I wanted the output to look something like the hex-editor of Norton Commander (now I'm showing my age). i.e. the part in blue below:
22/08/2011 10:34:54:293 Sending 'DisplayMessageRequest' request (33 bytes):
01 00 1D D2 01 00 01 18 09 00 09 0D 09 50 6C 65 ...?.........Ple 61 73 65 20 53 69 67 6E 20 52 65 63 65 69 70 74 ase Sign Receipt 93 ?
And here's a way to to this:

Cross-process application detection

Further to my previous post on detecting a terminal-service session, one may need to limit an application being started up twice within the same terminal-service session, but allow the application to be started up in different sessions (possibly started by the same user).

Normally (i.e. without TS), You can detect if your app is already running by using something like this:
public static bool IsAppAlreadyRunning()
{
    Process currentProcess = Process.GetCurrentProcess();
    return Process.GetProcessesByName(currentProcess.ProcessName).Length > 1;
}
Now obviously, this will find a running instance in a different TS session too - not what we want.

Enter the Mutex:

Wednesday, 17 August 2011

Detect a Terminal Server session

For a project I'm currently working on, I needed to determine if the session the app is running in is a TS session.

Well, It's this easy:
/// <summary>
/// Detect if app is running through a remote session, like terminal-services
/// </summary>
public static bool IsRemoteSession()
{
    return System.Windows.Forms.SystemInformation.TerminalServerSession;
    // TODO: Citrix check
}
OK, I'm trying to get the code-formatting working in Blogspot. This will have to do for now!
As you can see, I'm still looking to see how to get this right when running in Citrix, so I'll have to get back to this once I've got a 2008 VM installed with Hyper-V enabled, in order to mount the Citrix server I've just downloaded for evaluation.

I'm (a)live!

Well, this is it. After years of having no central repository for my software thoughts, and continually having to search old projects for pieces of code, I've decided to create a blog to keep all my ramblings. Hey, perhaps someone else could make use of them too.