Permalink
Browse files

clean codes, refactor map and add gyp config for Mac

  • Loading branch information...
1 parent 4d838ed commit 4cc0935045f1db1e22bd30490c0c67b0f2e19a81 @changchang changchang committed Mar 16, 2013
View
@@ -18,5 +18,8 @@
# Others
out/
+build/
.tags
-.tags_sorted_by_file
+.tags_sorted_by_file
+**xcodeproj/
+**dSYM/
View
@@ -1,5 +1,6 @@
CFLAGS = -g -I./include -I./deps/uv/include -I./deps/jansson/src
-LDFLAGS = -L./deps/uv -L./deps/jansson/lib -L. -lm -luv -B static -ljansson -pthread -lpomelo -Bdynamic -framework CoreServices
+LDFLAGS = -L./deps/uv -L./deps/jansson/lib -L. -lm -luv -B static -ljansson -lpomelo -Bdynamic -framework CoreServices
+TLDFLAGS = -L. -lm -lpomelo -Bdynamic -framework CoreServices
OBJDIR := out
OBJS += src/client.o
@@ -23,7 +24,7 @@ OBJS := $(addprefix $(OBJDIR)/,$(OBJS))
all: libpomelo.a
-$(OBJDIR)/src/%.o: src/%.c include/pomelo-client.h \
+$(OBJDIR)/src/%.o: src/%.c include/pomelo.h \
include/pomelo-protocol/package.h \
include/pomelo-private/map.h \
include/pomelo-private/listener.h \
@@ -72,6 +73,9 @@ reconn: test/reconn.c
stop: test/stop.c
$(CC) $(CFLAGS) $(LDFLAGS) -o test/stop test/stop.c
+map: test/map.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o test/map test/map.c
+
hashtest: test/hashtest.c
$(CC) $(CFLAGS) $(LDFLAGS) -o test/hashtest test/hashtest.c
View
@@ -0,0 +1,32 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'jansson',
+ 'type': '<(library)',
+ 'include_dirs': [
+ './src',
+ ],
+ 'sources': [
+ 'src/dump.c',
+ 'src/error.c',
+ 'src/hashtable.c',
+ 'src/hashtable.h',
+ 'src/jansson_private.h',
+ 'src/load.c',
+ 'src/memory.c',
+ 'src/pack_unpack.c',
+ 'src/strbuffer.c',
+ 'src/strbuffer.h',
+ 'src/strconv.c',
+ 'src/utf.c',
+ 'src/utf.h',
+ 'src/value.c',
+ ],
+ 'ldflags': [
+ '-no-undefined',
+ '-export-symbols-regex \'^json_\'',
+ '-version-info 8:0:4',
+ ]
+ },
+ ],
+}
@@ -1,7 +1,9 @@
#ifndef PC_COMMON_H
#define PC_COMMON_H
-#include <uv.h>
+#include "uv.h"
+
+#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
void pc__handle_close_cb(uv_handle_t* handle);
@@ -0,0 +1,14 @@
+#ifndef PC_INTERNAL_H
+#define PC_INTERNAL_H
+
+#include "pomelo.h"
+
+int pc__handshake_resp(pc_client_t *client, const char *data, size_t len);
+
+int pc__heartbeat(pc_client_t *client);
+
+void pc__heartbeat_cb(uv_timer_t* heartbeat_timer, int status);
+
+void pc__timeout_cb(uv_timer_t* timeout_timer, int status);
+
+#endif /* PC_INTERNAL_H */
@@ -1,7 +1,7 @@
#ifndef PC_LISTENER_H
#define PC_LISTENER_H
-#include <pomelo-client.h>
+#include "pomelo.h"
typedef struct pc_listener_s pc_listener_t;
@@ -1,26 +1,36 @@
#ifndef PC_MAP_H
#define PC_MAP_H
-#include <jansson.h>
-#include <hashtable.h>
+#include <stdlib.h>
+#include "pomelo-private/ngx-queue.h"
/**
- * Wrapper for hashtable from jansson.
+ * Simple hash map implements.
*/
+#define PC__MAP_DEFAULT_CAPACITY 64
+
typedef struct pc_map_s pc_map_t;
+typedef struct pc__pair_s pc__pair_t;
typedef void (*pc_map_value_release)(pc_map_t *map, const char *key,
void *value);
struct pc_map_s {
- hashtable_t *table;
+ size_t capacity;
+ ngx_queue_t *buckets;
pc_map_value_release release_value;
};
-pc_map_t *pc_map_new(pc_map_value_release release_value);
+struct pc__pair_s {
+ const char *key;
+ void *value;
+ ngx_queue_t queue;
+};
+
+pc_map_t *pc_map_new(size_t capacity, pc_map_value_release release_value);
-int pc_map_init(pc_map_t *map, pc_map_value_release release_value);
+int pc_map_init(pc_map_t *map, size_t capacity, pc_map_value_release release_value);
void pc_map_destroy(pc_map_t *map);
@@ -30,7 +40,7 @@ int pc_map_set(pc_map_t *map, const char *key, void *value);
void *pc_map_get(pc_map_t *map, const char *key);
-int pc_map_del(pc_map_t *map, const char *key);
+void *pc_map_del(pc_map_t *map, const char *key);
void pc_map_clear(pc_map_t *map);
@@ -0,0 +1,129 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#ifndef NGX_QUEUE_H_INCLUDED_
+#define NGX_QUEUE_H_INCLUDED_
+
+
+typedef struct ngx_queue_s ngx_queue_t;
+
+struct ngx_queue_s {
+ ngx_queue_t *prev;
+ ngx_queue_t *next;
+};
+
+
+#define ngx_queue_init(q) \
+ do { \
+ (q)->prev = q; \
+ (q)->next = q; \
+ } \
+ while (0)
+
+
+#define ngx_queue_empty(h) \
+ (h == (h)->prev)
+
+
+#define ngx_queue_insert_head(h, x) \
+ do { \
+ (x)->next = (h)->next; \
+ (x)->next->prev = x; \
+ (x)->prev = h; \
+ (h)->next = x; \
+ } \
+ while (0)
+
+
+#define ngx_queue_insert_after ngx_queue_insert_head
+
+
+#define ngx_queue_insert_tail(h, x) \
+ do { \
+ (x)->prev = (h)->prev; \
+ (x)->prev->next = x; \
+ (x)->next = h; \
+ (h)->prev = x; \
+ } \
+ while (0)
+
+
+#define ngx_queue_head(h) \
+ (h)->next
+
+
+#define ngx_queue_last(h) \
+ (h)->prev
+
+
+#define ngx_queue_sentinel(h) \
+ (h)
+
+
+#define ngx_queue_next(q) \
+ (q)->next
+
+
+#define ngx_queue_prev(q) \
+ (q)->prev
+
+
+#if defined(NGX_DEBUG)
+
+#define ngx_queue_remove(x) \
+ do { \
+ (x)->next->prev = (x)->prev; \
+ (x)->prev->next = (x)->next; \
+ (x)->prev = NULL; \
+ (x)->next = NULL; \
+ } \
+ while (0)
+
+#else
+
+#define ngx_queue_remove(x) \
+ do { \
+ (x)->next->prev = (x)->prev; \
+ (x)->prev->next = (x)->next; \
+ } \
+ while (0)
+
+#endif
+
+
+#define ngx_queue_split(h, q, n) \
+ do { \
+ (n)->prev = (h)->prev; \
+ (n)->prev->next = n; \
+ (n)->next = q; \
+ (h)->prev = (q)->prev; \
+ (h)->prev->next = h; \
+ (q)->prev = n; \
+ } \
+ while (0)
+
+
+#define ngx_queue_add(h, n) \
+ do { \
+ (h)->prev->next = (n)->next; \
+ (n)->next->prev = (h)->prev; \
+ (h)->prev = (n)->prev; \
+ (h)->prev->next = h; \
+ } \
+ while (0)
+
+
+#define ngx_queue_data(q, type, link) \
+ ((type *) ((unsigned char *) q - offsetof(type, link)))
+
+
+#define ngx_queue_foreach(q, h) \
+ for ((q) = ngx_queue_head(h); \
+ (q) != ngx_queue_sentinel(h) && !ngx_queue_empty(h); \
+ (q) = ngx_queue_next(q))
+
+
+#endif /* NGX_QUEUE_H_INCLUDED_ */
@@ -1,8 +1,8 @@
#ifndef PC_TRANSPORT_H
#define PC_TRANSPORT_H
-#include <uv.h>
-#include <pomelo-client.h>
+#include "uv.h"
+#include "pomelo.h"
pc_transport_t *pc_transport_new(pc_client_t *client);
@@ -9,7 +9,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
-#include <jansson.h>
+#include "jansson.h"
/*
* protobuf encode
@@ -1,10 +0,0 @@
-#ifndef PC_DICTIONARY_H
-#define PC_DICTIONARY_H
-
-#include <jansson.h>
-
-const char *pc__msg_dic_route(json_t *dic, int code);
-
-int pc__msg_dic_code(json_t *dic, const char *route);
-
-#endif /* PC_DICTIONARY_H */
@@ -1,8 +1,8 @@
#ifndef PC_MESSAGE_H
#define PC_MESSAGE_H
-#include <jansson.h>
-#include <pomelo-client.h>
+#include "jansson.h"
+#include "pomelo.h"
#define PC_MSG_FLAG_BYTES 1
@@ -1,8 +1,9 @@
#ifndef POMELO_PACKAGE_H
#define POMELO_PACKAGE_H
-#include <uv.h>
-#include <jansson.h>
+#include "uv.h"
+#include "jansson.h"
+#include "pomelo-private/common.h"
/**
* Pomelo package format:
@@ -22,16 +22,15 @@ extern "C" {
# define PC_EXTERN /* nothing */
#endif
-#include <uv.h>
-#include <jansson.h>
-#include <pomelo-private/map.h>
-#include <pomelo-protocol/package.h>
+#include "uv.h"
+#include "jansson.h"
+#include "pomelo-private/map.h"
+#include "pomelo-protocol/package.h"
#define PC_TYPE "c"
#define PC_VERSION "0.0.1"
#define PC_EVENT_DISCONNECT "disconnect"
-#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define PC_DEFAULT_HEARTBEAT 3000
#define PC_DEFAULT_TIMEOUT (PC_DEFAULT_HEARTBEAT + 5000)
@@ -306,6 +305,8 @@ int pc_client_join(pc_client_t *client);
void pc_client_stop(pc_client_t *client);
+void pc_emit_event(pc_client_t *client, const char *event, void *data);
+
/* Don't export the private CPP symbols. */
#undef PC_TCP_REQ_FIELDS
#undef PC_REQ_FIELDS
Oops, something went wrong.

0 comments on commit 4cc0935

Please sign in to comment.