Annotation of mqtt/src/client.c, revision 1.1.2.9
1.1.2.1 misho 1: #include "global.h"
2: #include "mqtt.h"
3:
4:
5: int
6: InitClient(void)
7: {
8: int sock;
9:
10: sock = socket(args->addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
11: if (sock == -1) {
12: printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
13: return -1;
14: }
15: if (connect(sock, &args->addr.sa, args->addr.sa.sa_len) == -1) {
16: printf("Error:: connect() #%d - %s\n", errno, strerror(errno));
17: return -1;
18: }
19:
20: return sock;
21: }
1.1.2.2 misho 22:
23: int
1.1.2.9 ! misho 24: ConnectClient(int sock)
1.1.2.2 misho 25: {
1.1.2.4 misho 26: int siz = 0;
1.1.2.6 misho 27: struct pollfd pfd;
1.1.2.4 misho 28:
1.1.2.7 misho 29: siz = mqtt_msgCONNECT(args->msg, (char*) AIT_GET_STR(&args->ConnID), args->ka,
30: (char*) AIT_GET_STR(&args->User), (char*) AIT_GET_STR(&args->Pass),
31: (char*) args->Will.Topic.val.string, (char*) args->Will.Msg.val.string,
1.1.2.5 misho 32: !args->notClear, args->QoS, args->Retain);
1.1.2.4 misho 33: if (siz == -1) {
34: printf("Error:: msgCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
35: return -1;
36: }
37:
38: siz = send(sock, args->msg->msg_base, siz, 0);
39: if (siz == -1) {
40: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
41: return -1;
42: } else
1.1.2.7 misho 43: ioVERBOSE(3) printf("Sended CONNECT %d bytes\n", siz);
1.1.2.4 misho 44:
1.1.2.6 misho 45: pfd.fd = sock;
1.1.2.9 ! misho 46: pfd.events = POLLIN | POLLPRI;
1.1.2.6 misho 47: switch (poll(&pfd, 1, args->ka * 1000)) {
48: case -1:
49: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
50: return -1;
51: case 0:
1.1.2.7 misho 52: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.1.2.6 misho 53: return -1;
54: }
55: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
56: return -1;
57:
58: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
59: if (siz == -1) {
60: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
61: return -1;
62: } else
1.1.2.7 misho 63: ioVERBOSE(3) printf("Received %d bytes\n", siz);
1.1.2.6 misho 64:
65: return (u_char) mqtt_readCONNACK(args->msg);
1.1.2.2 misho 66: }
1.1.2.9 ! misho 67:
! 68: int
! 69: CloseClient(int sock)
! 70: {
! 71: int siz = 0;
! 72:
! 73: siz = mqtt_msgDISCONNECT(args->msg);
! 74: if (siz == -1) {
! 75: printf("Error:: msgDISCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
! 76: return -1;
! 77: }
! 78:
! 79: siz = send(sock, args->msg->msg_base, siz, 0);
! 80: if (siz == -1) {
! 81: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
! 82: return -1;
! 83: } else
! 84: ioVERBOSE(3) printf("Sended DISCONNECT %d bytes\n", siz);
! 85:
! 86: close(sock);
! 87: return siz;
! 88: }
! 89:
! 90: int
! 91: KAClient(int sock)
! 92: {
! 93: int siz = 0;
! 94: struct pollfd pfd;
! 95:
! 96: siz = mqtt_msgPINGREQ(args->msg);
! 97: if (siz == -1) {
! 98: printf("Error:: msgPINGREQ #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
! 99: return -1;
! 100: }
! 101:
! 102: siz = send(sock, args->msg->msg_base, siz, 0);
! 103: if (siz == -1) {
! 104: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
! 105: return -1;
! 106: } else
! 107: ioVERBOSE(3) printf("Sended PINGREQ %d bytes\n", siz);
! 108:
! 109: pfd.fd = sock;
! 110: pfd.events = POLLIN | POLLPRI;
! 111: switch (poll(&pfd, 1, args->ka * 1000)) {
! 112: case -1:
! 113: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
! 114: return -1;
! 115: case 0:
! 116: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
! 117: return -1;
! 118: }
! 119: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
! 120: return -1;
! 121:
! 122: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
! 123: if (siz == -1) {
! 124: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
! 125: return -1;
! 126: } else
! 127: ioVERBOSE(3) printf("Received %d bytes\n", siz);
! 128:
! 129: return (u_char) mqtt_readPINGRESP(args->msg);
! 130: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>