int processPacket(int fd) { //struct svmp_sensor_event_t *p = (struct svmp_sensor_event_t*)malloc(sizeof(struct svmp_sensor_event_t));; int len = sizeof(struct svmp_sensor_event_t); //char buf [64]; //struct svmp_sensor_event_t *p = NULL; //malloc(sizeof(struct svmp_sensor_event_t));; struct svmp_sensor_event_t event; int tot = 0; int tmp; memset(&event,0,sizeof(event)); ERROR("processing packet \n"); tot = read(fd,&event,sizeof(event)); ERROR("processing packet : read %d bytes \n",tot); if (tot < 0) { ERROR("read error : %s\n",strerror(errno)); return tot; } if (tot == 0){ ERROR("read 0 bytes : %s\n",strerror(errno)); return tot; } ERROR("packet received type :%d, len %d\n",event.type,tot); handlepacket(&event); //free(p); return tot; // sanity check on the packet }
static int callback_packetreceived(void * cookie, int status) { struct netpacket_internal * NPC = cookie; struct netpacket_op * head = NPC->pending_head; handlepacket_callback * handlepacket = head->handlepacket; int rc = 0; /* If we're being cancelled, return. */ if (status == NETWORK_STATUS_CANCEL) goto done; /* On non-protocol errors, try to reconnect. */ if ((status != NETWORK_STATUS_OK) && (status != NETPROTO_STATUS_PROTERR)) { if (reconnect(NPC)) goto err0; goto done; } /* Call upstream callback. */ head->handlepacket = NULL; NPC->pending_current = head; rc = handlepacket(head->cookie, NPC, status, NPC->packettype, NPC->packetbuf, NPC->packetlen); /* If we didn't send a follow-up packet, this operation is done. */ if (head->handlepacket == NULL) { if (NPC->pending_tail == head) NPC->pending_tail = NULL; NPC->pending_head = head->next; free(head); /* We have successfully performed an operation. */ NPC->ndrops = 0; /* * If a 'connection lost' message was printed for this * connection, tell the user that the connection has been * re-established. We don't do this earlier because * obtaining a TCP connection is useless if we can't actually * manage to send a request and get a response back through * it. */ if (NPC->connlostmsgprinted) { warn0("Connection re-established"); /* Don't print this again. */ NPC->connlostmsgprinted = 0; } } /* Read another packet if appropriate. */ if ((NPC->pending_head != NULL) && (NPC->pending_head->handlepacket != NULL)) { if (netproto_readpacket(NPC->NC, NPC->pending_head->getbuf, callback_packetreceived, NPC)) goto err0; } else { NPC->reading = 0; } done: /* Free the packet buffer. */ free(NPC->packetbuf); NPC->packetbuf = NULL; /* Return value from callback. */ return (rc); err0: /* Failure! */ return (-1); }