Annotation of mqtt/src/client.c, revision 1.2.2.2

1.2       misho       1: #include "global.h"
                      2: #include "mqtt.h"
                      3: #include "client.h"
                      4: 
                      5: 
                      6: int
                      7: InitClient(void)
                      8: {
                      9:        int sock;
                     10: 
                     11:        sock = socket(args->addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
                     12:        if (sock == -1) {
                     13:                printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
                     14:                return -1;
                     15:        }
                     16:        if (connect(sock, &args->addr.sa, args->addr.sa.sa_len) == -1) {
                     17:                printf("Error:: connect() #%d - %s\n", errno, strerror(errno));
                     18:                return -1;
                     19:        }
                     20: 
                     21:        return sock;
                     22: }
                     23: 
                     24: int
                     25: ConnectClient(int sock)
                     26: {
                     27:        int siz = 0;
                     28:        struct pollfd pfd;
                     29: 
                     30:        siz = mqtt_msgCONNECT(args->msg, (char*) AIT_GET_STR(&args->ConnID), args->ka, 
                     31:                        (char*) AIT_GET_STR(&args->User), (char*) AIT_GET_STR(&args->Pass), 
                     32:                        (char*) args->Will.Topic.val.string, (char*) args->Will.Msg.val.string, 
                     33:                        !args->notClear, args->QoS, args->Retain);
                     34:        if (siz == -1) {
                     35:                printf("Error:: msgCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     36:                return -1;
                     37:        }
                     38: 
                     39:        siz = send(sock, args->msg->msg_base, siz, 0);
                     40:        if (siz == -1) {
                     41:                printf("Error:: send() #%d - %s\n", errno, strerror(errno));
                     42:                return -1;
                     43:        } else
                     44:                ioVERBOSE(3) printf("Sended CONNECT %d bytes\n", siz);
                     45: 
                     46:        pfd.fd = sock;
                     47:        pfd.events = POLLIN | POLLPRI;
                     48:        switch (poll(&pfd, 1, args->ka * 1000)) {
                     49:                case -1:
                     50:                        printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
                     51:                        return -1;
                     52:                case 0:
                     53:                        ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
                     54:                        return -1;
                     55:        }
                     56:        if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
                     57:                return -1;
                     58: 
                     59:        siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
                     60:        if (siz == -1) {
                     61:                printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
                     62:                return -1;
                     63:        } else
                     64:                ioVERBOSE(3) printf("Received %d bytes\n", siz);
                     65: 
                     66:        return (u_char) mqtt_readCONNACK(args->msg);
                     67: }
                     68: 
                     69: int
                     70: CloseClient(int sock)
                     71: {
                     72:        int siz = 0;
                     73: 
                     74:        siz = mqtt_msgDISCONNECT(args->msg);
                     75:        if (siz == -1) {
                     76:                printf("Error:: msgDISCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     77:                return -1;
                     78:        }
                     79: 
1.2.2.2 ! misho      80:        siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2       misho      81:        if (siz == -1) {
                     82:                printf("Error:: send() #%d - %s\n", errno, strerror(errno));
                     83:                return -1;
                     84:        } else
                     85:                ioVERBOSE(3) printf("Sended DISCONNECT %d bytes\n", siz);
                     86: 
                     87:        shutdown(sock, SHUT_RDWR);
                     88:        close(sock);
                     89:        return siz;
                     90: }
                     91: 
                     92: int
                     93: KAClient(int sock)
                     94: {
                     95:        int siz = 0;
                     96:        struct pollfd pfd;
                     97: 
                     98:        siz = mqtt_msgPINGREQ(args->msg);
                     99:        if (siz == -1) {
                    100:                printf("Error:: msgPINGREQ #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                    101:                return -1;
                    102:        }
                    103:        if (SendTo(sock, siz) == -1)
                    104:                return -1;
                    105: 
                    106:        pfd.fd = sock;
                    107:        pfd.events = POLLIN | POLLPRI;
                    108:        switch (poll(&pfd, 1, args->ka * 1000)) {
                    109:                case -1:
                    110:                        printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
                    111:                        return -1;
                    112:                case 0:
                    113:                        ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
                    114:                        return -1;
                    115:        }
                    116:        if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
                    117:                return -1;
                    118: 
                    119:        siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
                    120:        if (siz == -1) {
                    121:                printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
                    122:                return -1;
                    123:        } else
                    124:                ioVERBOSE(3) printf("Received %d bytes\n", siz);
                    125: 
                    126:        return mqtt_readPINGRESP(args->msg);
                    127: }
                    128: 
                    129: void *
                    130: OpenFile(void)
                    131: {
                    132:        int f, siz = 0;
                    133:        void *mem;
                    134: 
                    135:        if (!args->isFile)
                    136:                return NULL;
                    137: 
                    138:        f = open(AIT_GET_STR(&args->Value), O_RDONLY);
                    139:        if (f == -1) {
                    140:                printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
                    141:                return NULL;
                    142:        }
                    143:        mem = mmap(NULL, siz, PROT_READ, MAP_PRIVATE, f, 0);
                    144:        if (mem == MAP_FAILED) {
                    145:                printf("Error:: in map file #%d - %s\n", errno, strerror(errno));
                    146:                close(f);
                    147:                return NULL;
                    148:        } else
                    149:                close(f);
                    150: 
                    151:        AIT_SET_PTR(&args->Value, mem, siz);
                    152:        return mem;
                    153: }
                    154: 
                    155: void
                    156: CloseFile(void)
                    157: {
                    158:        if (args->isFile) {
                    159:                munmap(AIT_GET_PTR(&args->Value), AIT_LEN(&args->Value));
                    160:                AIT_FREE_VAL(&args->Value);
                    161:        }
                    162: }
                    163: 
                    164: inline int
                    165: SendTo(int sock, int siz)
                    166: {
1.2.2.2 ! misho     167:        siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2       misho     168:        if (siz == -1) {
                    169:                printf("Error:: send() #%d - %s\n", errno, strerror(errno));
                    170:                return -1;
                    171:        } else
1.2.2.1   misho     172:                ioVERBOSE(3) printf("Sended %d bytes\n", siz);
1.2       misho     173: 
                    174:        return siz;
                    175: }
                    176: 
                    177: inline int
                    178: RecvFrom(int sock)
                    179: {
                    180:        struct pollfd pfd;
                    181:        int siz = 0;
                    182: 
                    183:        pfd.fd = sock;
                    184:        pfd.events = POLLIN | POLLPRI;
                    185:        do {
                    186:                switch (poll(&pfd, 1, args->ka * 1000)) {
                    187:                        case -1:
                    188:                                printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
                    189:                                return -1;
                    190:                        case 0:
                    191:                                ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
                    192:                                if (KAClient(sock) == -1)
                    193:                                        return -1;
                    194:                                continue;
                    195:                }
                    196:                if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
                    197:                        return -1;
                    198:        } while (0);
                    199: 
                    200:        siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
                    201:        if (siz == -1) {
                    202:                printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
                    203:                return -1;
                    204:        } else
                    205:                ioVERBOSE(3) printf("Received %d bytes\n", siz);
                    206: 
                    207:        return siz;
                    208: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>