client.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "hloop.h"
  2. #include "hsocket.h"
  3. #define RECV_BUFSIZE 4096
  4. static char readbuf[RECV_BUFSIZE];
  5. void on_timer(htimer_t* timer) {
  6. static int cnt = 0;
  7. printf("on_timer timer_id=%lu time=%lus cnt=%d\n", timer->event_id, hloop_now(timer->loop), ++cnt);
  8. }
  9. void on_idle(hidle_t* idle) {
  10. static int cnt = 0;
  11. printf("on_idle idle_id=%lu cnt=%d\n", idle->event_id, ++cnt);
  12. }
  13. void on_write(hio_t* io, const void* buf, int writebytes) {
  14. printf("on_write fd=%d writebytes=%d\n", io->fd, writebytes);
  15. }
  16. void on_stdin(hio_t* io, void* buf, int readbytes) {
  17. printf("on_stdin fd=%d readbytes=%d\n", io->fd, readbytes);
  18. printf("> %s\n", buf);
  19. hio_t* iosock = (hio_t*)io->userdata;
  20. hwrite(iosock->loop, iosock->fd, buf, readbytes, on_write);
  21. }
  22. void on_read(hio_t* io, void* buf, int readbytes) {
  23. printf("on_read fd=%d readbytes=%d\n", io->fd, readbytes);
  24. printf("< %s\n", buf);
  25. printf(">>");
  26. fflush(stdout);
  27. }
  28. void on_close(hio_t* io) {
  29. printf("on_close fd=%d error=%d\n", io->fd, io->error);
  30. hio_t* iostdin = (hio_t*)io->userdata;
  31. hio_del(iostdin, READ_EVENT);
  32. }
  33. void on_connect(hio_t* io, int state) {
  34. printf("on_connect fd=%d state=%d\n", io->fd, state);
  35. if (state == 0) {
  36. printf("error=%d:%s\n", io->error, strerror(io->error));
  37. return;
  38. }
  39. struct sockaddr_in localaddr, peeraddr;
  40. socklen_t addrlen;
  41. addrlen = sizeof(struct sockaddr_in);
  42. getsockname(io->fd, (struct sockaddr*)&localaddr, &addrlen);
  43. addrlen = sizeof(struct sockaddr_in);
  44. getpeername(io->fd, (struct sockaddr*)&peeraddr, &addrlen);
  45. printf("connect connfd=%d [%s:%d] => [%s:%d]\n", io->fd,
  46. inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port),
  47. inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
  48. // NOTE: just on loop, readbuf can be shared.
  49. hio_t* iostdin = hread(io->loop, 0, readbuf, RECV_BUFSIZE, on_stdin);
  50. iostdin->userdata = io;
  51. hio_t* iosock = hread(io->loop, io->fd, readbuf, RECV_BUFSIZE, on_read);
  52. iosock->close_cb = on_close;
  53. iosock->userdata = iostdin;
  54. printf(">>");
  55. fflush(stdout);
  56. }
  57. int main(int argc, char** argv) {
  58. if (argc < 3) {
  59. printf("Usage: cmd host port\n");
  60. return -10;
  61. }
  62. const char* host = argv[1];
  63. int port = atoi(argv[2]);
  64. int connfd = Connect(host, port, 1);
  65. printf("connfd=%d\n", connfd);
  66. if (connfd < 0) {
  67. return connfd;
  68. }
  69. hloop_t loop;
  70. hloop_init(&loop);
  71. //hidle_add(&loop, on_idle, INFINITE);
  72. //htimer_add(&loop, on_timer, 1000, INFINITE);
  73. hconnect(&loop, connfd, on_connect);
  74. hloop_run(&loop);
  75. return 0;
  76. }