Skip to content

Instantly share code, notes, and snippets.

@hoffoo
Created July 14, 2014 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffoo/af3668fa5296aa42d83b to your computer and use it in GitHub Desktop.
Save hoffoo/af3668fa5296aa42d83b to your computer and use it in GitHub Desktop.
typedef char* itch_msg; // message content only
typedef void (*itch_handler)(itch_msg); // message handler func type
typedef itch_handler itch_map[122]; // map of message name and func ptr
// emit looks up the data in itch_map and calls appropriate function
void itch_emit(itch_map* m, char* data);
// Handlers
#define MSG_SYSTEM_EVENT 'S'
void msg_system_event(itch_msg msg);
#define MSG_STOCK_DIRECTORY 'R'
void msg_stock_directory(itch_msg msg);
#define MSG_STOCK_TRADE 'H'
void msg_stock_trade(itch_msg msg);
#include <stdlib.h>
#include <stdio.h>
#include "itch.h"
void itch_add_handler(itch_map i_map, char c, itch_handler i_handler) {
i_map[(int)c] = i_handler;
}
void itch_emit(itch_map* m, char* msg) {
itch_handler handler = (*m)[(int)msg[1]];
//printf("Got event %c with len %i\n", data[1], data[0]);
if (handler) {
handler(msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment