Skip to content
Asynchronous network library, support MacOS/FreeBSD/Linux/Windows with Lua binding.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
extension/lua
src
test
.gitignore
.travis.yml
LICENSE
Makefile
README.md

README.md

MIT licensed Build Status

About

m_net was a single file cross platform network library, provide a simple and efficient interface for covenient use.

Support Linux/MacOS/FreeBSD/Windows, using epoll/kqueue/select underlying.

Please use gmake to build demo under FreeBSD.

Features

  • simple API in C++ wrapper
  • with TCP/UDP support
  • nonblocking & event driven interface
  • using epoll/kqueue/select in Linux/MacOS/FreeBSD/Windows
  • support Lua binding

Server

It's very convenience for use, an echo server example, with CPP wrapper:

// subclass Chann to handle event
class CntChann : public Chann {
public:
   CntChann(Chann *c) : Chann(c) {}

   // implement virtual defaultEventHandler
   void defaultEventHandler(Chann *accept, chann_event_t event, int err) {

      if (event == CHANN_EVENT_RECV) {
         int ret = channRecv(m_buf, 256);
         channSend(m_buf, ret);
      }
      if (event == CHANN_EVENT_DISCONNECT) {
         delete this;           // release chann
      }
   }
   char m_buf[256];
};

int main(int argc, char *argv[]) {
   if (argc < 2) {
      cout << argv[0] << ": 'svr_ip:port'" << endl;
   } else {
      Chann echoSvr("tcp");

      if ( echoSvr.channListen(argv[1]) ) {
         cout << "svr start listen: " << argv[1] << endl;

         echoSvr.setEventHandler([](Chann *self, Chann *accept, chann_event_t event, int err) {
               if (event == CHANN_EVENT_ACCEPT) {
                  CntChann *cnt = new CntChann(accept);
                  char welcome[] = "Welcome to echoServ\n";
                  cnt->channSend((void*)welcome, sizeof(welcome));
                  delete accept;
               }
            });

         ChannDispatcher::startEventLoop();
      }
   }
   return 0;
}

the nested code need Closure support, with environment:

  • Apple LLVM version 8.1.0 (clang-802.0.42)
  • g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

Client

with bare C:

static void
_on_cnt_event(chann_msg_t *msg) {
   if (msg->event == CHANN_EVENT_RECV) {
      char buf[256] = {0};
      if (mnet_chann_recv(msg->n, buf, 256) > 0) {
         printf("%s", buf);;
         if ( fgets(buf, 256, stdin) ) {
            mnet_chann_send(msg->n, buf, strlen(buf));
         } else {
            mnet_chann_close(msg->n);
         }
      }
   }
   if (msg->event == CHANN_EVENT_DISCONNECT) {
      mnet_chann_close(msg->n);
   }
}

int
main(int argc, char *argv[]) {

   if (argc < 2) {
      printf("%s 'cnt_ip:port'\n", argv[0]);
   } else {
      chann_addr_t addr;
      if (mnet_parse_ipport(argv[1], &addr) > 0)  {
         mnet_init();
         chann_t *cnt = mnet_chann_open(CHANN_TYPE_STREAM);
         mnet_chann_set_cb(cnt, _on_cnt_event, NULL);

         printf("cnt try connect %s:%d...\n", addr.ip, addr.port);
         mnet_chann_connect(cnt, addr.ip, addr.port);
         while (mnet_poll(-1) > 0) {
         }
         mnet_fini();
      }
   }
   return 0;
}

In the other hand, the C interface with more flexible options.

Lua Wrapper

try '$make lua' then run lua example:

lua examples/chann_web.lua '127.0.0.1:8080'

open your browser to visit '127.0.0.1:8080'.

in FreeBSD, modify Makefile to include/link to proper lua lib version.

Example & Tests

in examples and test dir.

Projects

  • m_tunnel: provide a secure socks5 interface tcp connection between local <-> remote side.

  • m_kcptun: provide a speedup TCP connection between local <-> remote, with KCP underlying

  • m_dnscnt: asynchronous DNS query client/library, query every DNS server in list at one time.

Thanks

Thanks NTP source code from https://github.com/edma2/ntp, author: Eugene Ma (edma2)

You can’t perform that action at this time.