PageKite in C

These days, I am putting the finishing touches on a basic implementation of a PageKite back-end connector, written in C. This C version (libpagekite) will be suitable for embedding in other programs, for use on small embedded devices (routers, specialized hardware) and will also be the core of our Android SDK and app.

This is the second complete implementation of PageKite, and although C is a tricky (fun!) language to write code in, I am a little surprised at how well it is going. In some respects, writing PageKite in C is actually easier than writing it in Python.

Part of that is of course down to experience. This time around I understand the problem much better. No time wasted floundering about with dead end designs.

But there is more to it than that.

PageKite is inherently a very low level program, it is concerned with sockets and network connections, DNS, IP, IPv6, SSL... and although all of these things are all supported by Python, it turns out that a lot of the bugs I have to deal with are caused by poor abstractions and wrappers around low level C libraries. The SSL support in particular, is a mess. Writing PageKite in C allows me to cut out the middle man, as it were, and just interact with the native OS libraries directly.

I am having to write a bit more code in C than I did in Python, but not by a huge margin. Pagekite.py v0.3.10 was about 3200 lines of code. I expect the C implementation to end up being two to three times longer by the time it is has feature parity, but it will probably also be an order of magnitude more efficient in terms of RAM and CPU usage, and will include advanced features like proper flow control and IPv6 support, which the 0.3 versions of PageKite lacked.

Long term, it will probably make sense to drop the native Python code and create a Python wrapper around libpagekite instead. That will let me avoid the buggy Python layers while still using Python for the high level things it excels at.

Tags: pagekite, tech

Recent posts