int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion) #endif { int rc, port; char* addr; FUNC_ENTRY; aClient->good = 1; addr = MQTTProtocol_addressPort(ip_address, &port); rc = Socket_new(addr, port, &(aClient->net.socket)); if (rc == EINPROGRESS || rc == EWOULDBLOCK) aClient->connect_state = 1; /* TCP connect called - wait for connect completion */ else if (rc == 0) { /* TCP connect completed. If SSL, send SSL connect */ #if defined(OPENSSL) if (ssl) { if (SSLSocket_setSocketForSSL(&aClient->net, aClient->sslopts) != 1) { rc = SSLSocket_connect(aClient->net.ssl, aClient->net.socket); if (rc == -1) aClient->connect_state = 2; /* SSL connect called - wait for completion */ } else rc = SOCKET_ERROR; } #endif if (rc == 0) { /* Now send the MQTT connect packet */ if ((rc = MQTTPacket_send_connect(aClient, MQTTVersion)) == 0) aClient->connect_state = 3; /* MQTT Connect sent - wait for CONNACK */ else aClient->connect_state = 0; } } if (addr != ip_address) free(addr); FUNC_EXIT_RC(rc); return rc; }
/** * MQTT outgoing connect processing for a client * @param ip_address the TCP address:port to connect to * @param clientID the MQTT client id to use * @param cleansession MQTT cleansession flag * @param keepalive MQTT keepalive timeout in seconds * @param willMessage pointer to the will message to be used, if any * @param username MQTT 3.1 username, or NULL * @param password MQTT 3.1 password, or NULL * @return the new client structure */ int MQTTProtocol_connect(char* ip_address, Clients* aClient) { int rc, port; char* addr; FUNC_ENTRY; aClient->good = 1; time(&(aClient->lastContact)); addr = MQTTProtocol_addressPort(ip_address, &port); rc = Socket_new(addr, port, &(aClient->socket)); if (rc == EINPROGRESS || rc == EWOULDBLOCK) aClient->connect_state = 1; /* TCP connect called */ else if (rc == 0) { if ((rc = MQTTPacket_send_connect(aClient)) == 0) aClient->connect_state = 2; /* TCP connect completed, in which case send the MQTT connect packet */ else aClient->connect_state = 0; } FUNC_EXIT_RC(rc); return rc; }
int main(int argc, char* argv[]) { int i, c, rc, fc; int count = 0; char line_data[MAX_LINE]; /* variable to hold socket descriptor */ Socket connect_socket; if (argc < 3) { printf("No host and port\n"); return (-1); } /* connect to the server at the specified host and port; * blocks the process until the connection is accepted * by the server; creates a new data transfer socket. */ connect_socket = Socket_new(argv[1], atoi(argv[2])); if (connect_socket < 0) { printf("Failed to connect to server\n"); return (-1); } printf("%% "); // print initial prompt /* get a string from stdin up to and including * a newline or to the maximim input line size. * Continue getting strings from stdin until EOF. */ while ((fgets(line_data, sizeof(line_data), stdin) != NULL)) { count = strlen(line_data) + 1; /* count includes '\0' */ /* send the characters of the input line to the server * using the data transfer socket. */ for (i = 0; i < count; i++) { c = line_data[i]; rc = Socket_putc(c, connect_socket); // if (rc == EOF) // { // printf("Socket_putc EOF or error\n"); // Socket_close(connect_socket); // exit (-1); /* assume socket problem is fatal, end client */ // } } /* receive the converted characters for the string from * the server using the data transfer socket. */ printf("boutta receive\n"); while (1) { fc = Socket_getc(connect_socket); printf("%c", fc); if (fc == '\0') { break; } } printf("%% "); // print prompt for next command } /* end of while loop; at EOF */ Socket_close(connect_socket); exit(0); }
int main() { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; char buf[200]; int buflen = sizeof(buf); int msgid = 1; MQTTString topicString = MQTTString_initializer; int req_qos = 0; char* payload = "mypayload"; int payloadlen = strlen(payload); int len = 0; signal(SIGINT, cfinish); signal(SIGTERM, cfinish); rc = Socket_new("127.0.0.1", 1883, &mysock); data.clientID.cstring = "me"; data.keepAliveInterval = 20; data.cleansession = 1; data.username.cstring = "testuser"; data.password.cstring = "testpassword"; struct timeval tv; tv.tv_sec = 1; /* 1 second Timeout */ tv.tv_usec = 0; setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); len = MQTTSerialize_connect(buf, buflen, &data); rc = write(mysock, buf, len); /* wait for connack */ if (MQTTPacket_read(buf, buflen, getdata) == CONNACK) { int connack_rc; if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) { printf("Unable to connect, return code %d\n", connack_rc); goto exit; } } else goto exit; /* subscribe */ topicString.cstring = "substopic"; len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); rc = write(mysock, buf, len); if (MQTTPacket_read(buf, buflen, getdata) == SUBACK) /* wait for suback */ { int submsgid; int subcount; int granted_qos; rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); if (granted_qos != 0) { printf("granted qos != 0, %d\n", granted_qos); goto exit; } } else goto exit; topicString.cstring = "pubtopic"; while (!toStop) { if (MQTTPacket_read(buf, buflen, getdata) == PUBLISH) { int dup; int qos; int retained; int msgid; int payloadlen_in; char* payload_in; int rc; MQTTString receivedTopic; rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen); printf("message arrived %.*s\n", payloadlen_in, payload_in); } printf("publishing reading\n"); len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen); rc = write(mysock, buf, len); } printf("disconnecting\n"); len = MQTTSerialize_disconnect(buf, buflen); rc = write(mysock, buf, len); exit: rc = shutdown(mysock, SHUT_WR); rc = recv(mysock, NULL, 0, 0); rc = close(mysock); return 0; }
IoSocket *IoSocket_rawClone(IoSocket *proto) { IoObject *self = IoObject_rawClonePrimitive(proto); IoObject_setDataPointer_(self, Socket_new()); return self; }
IoSocket *IoSocket_proto(void *state) { IoObject *self = IoObject_new(state); IoObject_tag_(self, IoSocket_newTag(state)); IoObject_setDataPointer_(self, Socket_new()); IoState_registerProtoWithFunc_((IoState *)state, self, protoId); { IoMethodTable methodTable[] = { {"asyncStreamOpen", IoSocket_asyncStreamOpen}, {"asyncUdpOpen", IoSocket_asyncUdpOpen}, {"isOpen", IoSocket_isOpen}, {"isValid", IoSocket_isValid}, {"isStream", IoSocket_isStream}, {"asyncBind", IoSocket_asyncBind}, {"asyncListen", IoSocket_asyncListen}, {"asyncAccept", IoSocket_asyncAccept}, {"asyncConnect", IoSocket_asyncConnect}, {"asyncStreamRead", IoSocket_asyncStreamRead}, {"asyncStreamWrite", IoSocket_asyncStreamWrite}, {"asyncUdpRead", IoSocket_asyncUdpRead}, {"asyncUdpWrite", IoSocket_asyncUdpWrite}, {"close", IoSocket_close}, {"descriptorId", IoSocket_descriptorId}, {"setSocketReadBufferSize", IoSocket_setSocketReadBufferSize}, {"setSocketWriteBufferSize", IoSocket_setSocketWriteBufferSize}, {"getSocketReadLowWaterMark", IoSocket_getSocketReadLowWaterMark}, {"getSocketWriteLowWaterMark", IoSocket_getSocketWriteLowWaterMark}, {"setSocketReadLowWaterMark", IoSocket_setSocketReadLowWaterMark}, {"setSocketWriteLowWaterMark", IoSocket_setSocketWriteLowWaterMark}, {"setNoDelay", IoSocket_setNoDelay}, {"errorNumber", IoSocket_errorNumber}, {"errorDescription", IoSocket_errorDescription}, {"setAddress", IoSocket_setAddress}, {"fromFd", IoSocket_fromFd}, {NULL, NULL} }; IoObject_addMethodTable_(self, methodTable); } #define CONSTANT(name) IoObject_setSlot_to_(self, IOSYMBOL(#name), IONUMBER(name)); CONSTANT(AF_INET); CONSTANT(AF_UNIX); return self; }
Object* net_socket(Object* self) { return Socket_new(); }