Skip to content

Commit

Permalink
add socket service. (WIP, untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
starwing committed Jan 28, 2016
1 parent 348e40f commit 7d8ad38
Show file tree
Hide file tree
Showing 5 changed files with 743 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "znet"]
path = znet
url = https://github.com/starwing/znet.git
46 changes: 45 additions & 1 deletion loki_services.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,57 @@ LK_API void lk_starttimer (lk_Timer *timer, lk_Time delayms);
LK_API void lk_canceltimer (lk_Timer *timer);


/* socket service */

LKMOD_API int loki_service_socket(lk_State *S);

typedef struct lk_Accept lk_Accept;
typedef struct lk_Tcp lk_Tcp;
typedef struct lk_Udp lk_Udp;

typedef void lk_TcpHandler (lk_State *S, void *ud, unsigned err, lk_Tcp *tcp);
typedef void lk_UdpHandler (lk_State *S, void *ud, unsigned err, lk_Udp *udp);

typedef size_t lk_HeaderHandler (lk_State *S, void *ud, lk_Tcp *tcp, const char *buff, size_t len);
typedef void lk_PacketHandler (lk_State *S, void *ud, lk_Tcp *tcp, const char *buff, size_t len);
typedef void lk_RecvFromHandler (lk_State *S, void *ud, lk_Udp *udp, unsigned err,
const char *buff, unsigned count,
const char *addr, unsigned port);

LK_API void lk_setheaderhandler (lk_Service *svr, lk_HeaderHandler *h, void *ud);
LK_API void lk_setpackethandler (lk_Service *svr, lk_PacketHandler *h, void *ud);
LK_API void lk_setrecvfromhandler (lk_Service *svr, lk_RecvFromHandler *h, void *ud);

LK_API lk_Accept *lk_newaccept (lk_Service *svr, lk_TcpHandler *h, void *ud);
LK_API void lk_delaccept (lk_Accept *accept);

LK_API void lk_listen (lk_Accept *accept, const char *addr, unsigned port);

LK_API void lk_connect (lk_Service *svr, const char *addr, unsigned port,
lk_TcpHandler *h, void *ud);
LK_API void lk_deltcp (lk_Tcp *tcp);

LK_API unsigned lk_getsession (lk_Tcp *tcp);
LK_API void lk_setsession (lk_Tcp *tcp, unsigned session);

LK_API void lk_send (lk_Tcp *tcp, const char *buff, unsigned size);

LK_API void lk_bindudp (lk_Service *svr, const char *addr, unsigned port,
lk_UdpHandler *h, void *ud);
LK_API void lk_deludp (lk_Udp *udp);

LK_API void lk_sendto (lk_Udp *udp, const char *buff, unsigned len,
const char *addr, unsigned port);


#endif /* loki_services_h */

#if defined(LOKI_IMPLEMENTATION) && !defined(loki_services_implemented)
#define loki_services_implemented

LK_API int lk_openlibs(lk_State *S) {
lk_preload(S, "timer", loki_service_timer);
lk_preload(S, "timer", loki_service_timer);
lk_preload(S, "socket", loki_service_socket);
return LK_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions lokilib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define LOKI_IMPLEMENTATION
#include "loki_services.h"
Loading

0 comments on commit 7d8ad38

Please sign in to comment.