AIM bookmark bot

I coded this AIM bot, quite a while ago, which lets you add/remove/search bookmarks. It was written using the C interface to Amazon’s SimpleDB and the AIM SDK. It’s pretty basic. Just send an IM to “whutsnu” on AIM, and he’ll reply with the list of commands.

The “export” command hasn’t worked for quite a while, because the domain expired. The other features work, though. This was originally meant to be one part of a larger project.

Node.js w/1M concurrent connections!

I’ve decided to ramp up the Node.js experiments, and pass the 1 million concurrent connections milestone. It worked, using a swarm of 500 Amazon EC2 test clients, each establishing ~2000 active long-poll COMET connections to a single 15GB rackspace cloud server.

This isn’t landing the mars rover, or curing cancer. It’s just a pretty cool milestone IMO, which may help a few people who want to use Node.js for a large number of concurrent connections. So, hopefully it’s of some benefit to a few Node developers who can use these settings as a starting point in their own projects.

Here’s the connection count as displayed on the sprite’s page:

Here’s a sysctl dumping the number of open file handles (sockets are file handles):

Here’s the view of “top” showing system resources in use:

I think it’s pretty reasonable for 1M connections to consume 16GB of memory, but it could probably be trimmed down quite a bit. I haven’t spent any time optimizing that. I’ll leave that for another day.

Here’s a latency test run against the comet URL:

The new tweaks, placed in /etc/sysctl.conf (CentOS) and then reloaded with “sysctl -p” :

net.core.rmem_max = 33554432
net.core.wmem_max = 33554432
net.ipv4.tcp_rmem = 4096 16384 33554432
net.ipv4.tcp_wmem = 4096 16384 33554432
net.ipv4.tcp_mem = 786432 1048576 26777216
net.ipv4.tcp_max_tw_buckets = 360000
net.core.netdev_max_backlog = 2500
vm.min_free_kbytes = 65536
vm.swappiness = 0
net.ipv4.ip_local_port_range = 1024 65535

Other than that, the steps were identical to the steps described in my previous blog posts, except this time using Node.js version 0.8.3.

Here is the server source code, so you can get a sense of the complexity level. Each connected client is actively sending messages, for the purpose of verifying the connections are alive. I haven’t pushed that throughput yet, to see what data rate can be sent. Since the modest 16GB memory was consumed, this would have likely caused swapping and meant little. I’ll give it a shot with a higher memory server next time.

Escape the 1.4GB V8 heap limit in Node.jsSprites Project

Fix for multiple monitors with fullscreen games

There’s a pretty annoying issue that happens in windows when you are playing a game in fullscreen on one monitor, and have a second (or third, fourth) monitor with other stuff open on it. If your game is playing at a resolution that is different than your normal desktop resolution on that monitor, everything gets all moved around on the other monitors.

To fix this, I wrote a tiny utility program which you can run before you start the game, and it fixes this behavior by calculating what the correct location should be for all your windows and moving them there. It’s useful if you like having IM windows open, notes or a web browser, etc. You can’t interact with those programs while you’re in the game, but at least you can *see* them (for example, if you have a strategy guide or map, etc open)

Of course, I’m not responsible for any damage this program does to your computer. I’ve tested it a fair amount, but it’s entirely possible somebody with an unusual setup could find a situation where you may lose a program’s window due to it being positioned in the wrong place.

Let me know if this is useful, I’ll try to improve it and turn it into a proper project.

fsfix

caustik

Prettier C/C++ code using explicit scope

Probably the single most impactful epiphony I’ve had in my years of programming in C/C++ is discovering the ability to provide explicit scope inside a function. For snippets of code that are conceptually self-contained, but only used once in the program, they improve readability, provide optimization hints to the compiler, and are just kind of cool =)

Here’s some “before” code, like you’d expect to see throughout C source code:

int func1(int param1, int param2)
{
    int ret = GREAT_SUCCESS;
    int tmp1, tmp2;
    structType struct1;
    otherStructType struct2;

    struct1.val1 = param1;
    struct1.val2 = 20;

    /*! do something */
    ret = func2(&struct1);

    if(FAILED(ret)) { return ret; }

    struct2.whatever = param2;
    struct2.somethingElse = 4;

    /*! do something else */
    ret = func2(&struct2);

    if(FAILED(ret)) { return ret; }

    return ret;
}

The variables are declared at the top of the function, the parameters for each function call
are determined, the struct parameter is filled out, return code checked.

Compare to using explicit scope:

int func1(int param1, int param2)
{
    int ret = GREAT_SUCCESS;

    /*! do something */
    {
        structType struct1;

        struct1.val1 = param1;
        struct1.val2 = 20;

        ret = func2(&struct1);

        if(FAILED(ret)) { return ret; }
    }

    /*! do something else */
    {
        otherStructType struct2;

        struct2.whatever = param2;
        struct2.somethingElse = 4;

        ret = func2(&struct2);

        if(FAILED(ret)) { return ret; }
    }

    return ret;
}

What this has accomplished is your eyes immediately see that “do something” and “do something” else
are functional blocks, which you can quickly read as independent units. In addition, you can very
easily tell that the function simply “does something”, then “does something else”, then returns the result.

Each functional block has it’s variables declared only inside it’s explicit scope. So you don’t have this huge block of declarations at the top of your function, making things much easier to read. Also, the compiler now knows without ambiguity which variables are used where, which can help optimize it’s memory usage (e.g. it can re-use stack space instead of reserving it all at the top).

To me, the difference is night and day – and this is only a really simple example. Try it out on some
more complex code, and watch your code quality/readability improve. This works in both C and C++, it’s
particularly great for C since you normally are forced to declare your variables at the top of the function.
When you explicitly declare your scope, this is no longer the case! Really, C just makes you declare at the
start of {} blocks.

Painless Debug Console

I have had the pleasure of writing a pretty large number of apps for Windows. There are a few things that I have learned, over time, that have really helped to improve my productivity. One of those things is a basic debug console.

Typically, a debug console is handy – go figure – while you’re debugging. Once you have a release quality build, you won’t want the debug console hanging around anymore.

This is one of those snippets of code that, while brutally simple, is rather non-obvious and most people don’t know of.

#ifdef MYAPP_DEBUG_TRACE
if(AllocConsole())
{
    freopen("CONOUT$", "wt", stdout);

    SetConsoleTitle(L"MyApp : Debug Console");

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);

    printf("n");
    printf(" MyApp Debug Consolen");
    printf("n");
}
#endif

Basically this code, if debug trace preprocessor is set (add MYAPP_DEBUG_TRACE to your preprocessor settings), will allocate a nice little debug console window, change the title, set the font colors, and redirect standard output to the console.

This snippet of code can be added to any Win32 application, and gives you the instant capability to spit out oodles of delicious debugging data. Typically, I combine this method with something along these lines:

#ifdef __cplusplus
inline void null_func(char *format, ...) { }
#define DbgEmpty null_func
#else
#define DbgEmpty { }
#endif
/* Debug Trace Enabled */
#ifdef  MYAPP_DEBUG_TRACE
#include
#define DbgPrintf printf
#else
/* Debug Trace Disabled */
#define DbgPrintf DbgEmpty
#endif

Now, you can use the function “DbgPrintf” just as if it was “printf” – and it will compile away to nothing in a release mode build!

DOS “debug” fun

Sometimes you get the urge to do some programming, but you don’t have access to a compiler. Luckily, virtually every Windows PC in the world has a utility called “debug” installed. This little program lets you input 16-bit x86 assembly language, and allows you to write it out to create a .com file. This file can then be executed inside of a DOS shell.

The following is an example of something simple but cool you can do with this program. I will be using some basic DOS interrupts in order to do console input and output. (Lookup ‘DOS interrupt 21h’ on google to find out more).

I’ll highlight everything i’m typing in green so you can try this at home. You won’t need to type the optional comments (anything after the semicolon is ignored), and they get lost when you write the file anyway.

C:>debug kthx.com

-a 100
1476:0100 mov cl, d7 ; default cl to character 'd7'
1476:0102 mov ah, 6  ; ah:6 is console input/output
1476:0104 mov dl, ff ; dl:ff specifies input
1476:0106 int 21     ; interrupt 21 call
1476:0108 jz 10c     ; skip next instruction if failure
1476:010A mov cl, al ; save result of console input
1476:010C mov dl, cl ; load current character as output
1476:010E int 21     ; interrupt 21 call
1476:0110 jmp 102    ; loop forever!
1476:0112
-r cx
CX 0012
:12
-w
Writing 00012 bytes
-q
C:>

Now, when you execute “kthx.com”, you will see the screen swamped with the funky ‘d7’ ASCII character.

Whenever you type a character, the screen with update to display that character. Try alternating between visible characters and not visible characters (like space). For more fun, try to as quickly as possible type “|/-|/-” (animates like a little progress bar).

Note that you cant really exit the program. Pressing ctrl+c will actually just display a little heart character. It would be easy to modify the code to accept a character (like escape or control+c) to exit, but i’ll leave that up to you :].

This is just one basic thing you can make a .com file do. For a cooler example, check out neetro. Neetro is a little bit more complex than the program above, so it would have been a pain to write using “debug”. The source is instead compiled using nasm.

Ridiculous C-code

For no particular reason I decided to write the following confusing line of C code today…

const _=0;do;while(0?0,1:!_,_);

Might make an interesting interview question. Can you parse it in your head?

Here are some reasons why this seems very confusing:

  1. const _=0;This is not much different than “const int a = 0;”… It is just using the implied “int” type, and rudely using the single underscore character, which is acceptable (but taboo) as a variable name.
  2. do;whileThis looks odd, but is actually valid. Typically, people implement a do/while as “do{}while(…);” – however, it is equally valid to replace the {}’s with a single statement, even an empty do-nothing statement (“;”).
  3. a?b:cI am tempted to fail somebody in an interview immediately if they have no clue what this operator does. It isn’t that it’s particularly critical or anything -but if you’re a C coder and you’ve been around the block – it would be very odd for you to not be familiar with the conditional operator. Still, it is rare enough to be difficult to brain-parse.All this does is evaluate “a”, and if the result is true, evaluates “b”. If the result if false, it evaluates “c”. Simple.
  4. !_,_ Okay so this one is nasty. I might not expect everybody to be familiar with this guy. What happens here is the variable “_” is being evaluated as negated, then the variable “_” itself is being evaluated. The trick is, only the right side of the comma is actually used as the value of the expression. Huh?

    This is primarily useful in things like a “for” loop. Since the general syntax of a for loop is “for(a;b;c)”, you at first may feel a little limited. How can I increment two variables on each step? Easy. “for(a;b;d++,e++)” – note the comma operator allows you to squeeze two subexpressions into “c”. In this case, nobody knows or cares that the expression value is, in the end, defined only by e++. The result is not being used at all.

    I’d be interested to hear some real world valueable uses for the comma operator, aside from a “for” loop.