Guest User

Example plugin

a guest
Apr 30th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <ts/ts.h>
  2. #include <stdlib.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5. #include <string.h>
  6.  
  7. static int
  8. handler (TSCont contp, TSEvent event, void *edata __attribute__((unused)))
  9. {
  10.     if (event == TS_EVENT_IMMEDIATE) {
  11.         struct sockaddr_in ip;
  12.         int ret;
  13.  
  14.         memset(&ip, 0, sizeof(struct sockaddr_in));
  15.         ip.sin_family = AF_INET;
  16.         ip.sin_port = htons(9999);
  17.         ret = inet_pton(AF_INET, "127.0.0.1", &ip.sin_addr);
  18.         assert( ret == 1 );
  19.  
  20.         TSAction actionp = TSNetConnect (contp, (struct sockaddr const*) &ip);
  21.         if (!TSActionDone (actionp)) {
  22.             TSContDataSet (contp, actionp);
  23.         } else {
  24.             /* We've already been called back... */
  25.             return 0;
  26.         }
  27.     } else if (event == TS_EVENT_NET_CONNECT) {
  28.         /* Net connection succeeded */
  29.         TSContDataSet (contp, NULL);
  30.         return 0;
  31.     } else if (event == TS_EVENT_NET_CONNECT_FAILED) {
  32.         /* Net connection failed */
  33.         TSContDataSet (contp, NULL);
  34.         return 0;
  35.     }
  36.     return 0;
  37. }
  38.  
  39.    void
  40. TSPluginInit (int argc  __attribute__((unused)), const char *argv[]  __attribute__((unused)))
  41. {
  42.     TSCont contp;
  43.  
  44.     contp = TSContCreate (handler, TSMutexCreate ());
  45.  
  46.     /* We don't want to call things out of TSPluginInit
  47.        directly since it's called before the rest of the
  48.        system is initialized. We'll simply schedule an event
  49.        on the continuation to occur as soon as the rest of
  50.        the system is started up. */
  51.     TSContSchedule (contp, 0, TS_THREAD_POOL_DEFAULT);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment