void skynet_mq_popst(struct skynet_mq *mq) { if (mq->head == mq->tail) { return; } pop_message(mq); }
static void resend_pushes(PushServer* server, uint32_t start) { char* message; uint32_t id; size_t len; time_t sent; LM_INFO("Resending data to apns: start id %u, top id %u\n", start, top_message_id()); while(start >= top_message_id()) { if (0 == pop_message(NULL, NULL, NULL, NULL)) { // nothing to send, return return; } } time_t time_start = time(NULL); while (time_start > top_message_sent()) { if (0 == pop_message(&id, &message, &len, &sent)) { return; // No message anymore } char *buf; bin2str(message, len, &buf); LM_INFO("Resending data to apns: id %d, sent %lu, message [%s], length %lu\n", id, sent, buf, len); free(buf); if (-1 == send_push_data(server, message, len)) { LM_ERR("Push sending failed\n"); } LM_DBG("OK\n"); // re-add message add_push_to_queue(id, message, len); } }
int skynet_mq_popmt(struct skynet_mq *mq, struct skynet_message_package *pack) { if (mq->head == mq->tail) { return 0; } rwlock_rlock(&mq->lock); // only one reader, so use read lock is ok. // but we can call skynet_mq_pushmt in another thread *pack = mq->q[mq->head]; pop_message(mq); rwlock_runlock(&mq->lock); return 1; }
void ClientConnection::process_messages() { Uint32 now = SDL_GetTicks(); std::map<SDLKey, Uint32> releases; std::set<SDLKey> presses; std::map<SDLKey, Uint32> holds; for (ReceivedPacket packet = pop_message(); packet.key != SDLK_UNKNOWN; packet = pop_message()) { if (packet.released) releases[packet.key] = packet.arrival_time; else { active_keys[packet.key] = packet.arrival_time; presses.insert(packet.key); } //printf("Processed %s (%d) from %s\n", SDL_GetKeyName(packet.key), packet.key, get_name().c_str()); } Uint32 hold_time = 100; // ms for (typeof(active_keys.begin()) it = active_keys.begin(); it != active_keys.end(); ++it) { Uint32 t; typeof(releases.begin()) r = releases.find(it->first); if (r != releases.end()) { t = r->second - it->second; printf("%d released after %d clocks, hold_time: %d\n", it->first, t, (int) (hold_time)); } else { t = now - it->second; //printf("%d active for %d clocks, hold_time: %d\n", it->first, t, (int)(hold_time)); } if (t >= hold_time) holds[it->first] = t; } // TODO: sort actions by arrival time for (typeof(holds.begin()) it = holds.begin(); it != holds.end(); ++it) { typeof(bindings.begin()) b = bindings.find(it->first); if (b != bindings.end() && presses.find(it->first) == presses.end()) { // Call the member function on the entity b->second.onRepeat(entity, it->second); } } for (typeof(presses.begin()) it = presses.begin(); it != presses.end(); ++it) { typeof(bindings.begin()) b = bindings.find(*it); if (b != bindings.end()) { // Call the member function on the entity b->second.onPressed(entity); } } for (typeof(releases.begin()) it = releases.begin(); it != releases.end(); ++it) { active_keys.erase(it->first); typeof(bindings.begin()) b = bindings.find(it->first); if (b != bindings.end()) { // Call the member function on the entity b->second.onReleased(entity); } } }
void push_check_status(PushServer* apns) { uint32_t id = 0; #define STATUS_LEN 6 char *buf; unsigned char status_buf[STATUS_LEN]; int err = 0; // LM_DBG("Check push status...."); do { err = read_push_status(apns, (char*)status_buf, STATUS_LEN); switch(err) { case 0: { // LM_DBG("There is no status message"); break; } case -1: // LM_DBG("There is error occured"); break; default: break; } if (err == 0 || err == -1) { time_t before = time(NULL) - 2; while(before > top_message_sent()) { if (0 == pop_message(NULL, NULL, NULL, NULL)) return; } return; } { bin2str((char*)status_buf, STATUS_LEN, &buf); LM_DBG("Got status message from apns: [%s], length %d\n", buf, STATUS_LEN); free(buf); } if (status_buf[0] != STATUS_CMD) { LM_ERR("Received wrong status cmd (%c), expecting '8'", status_buf[0]); return; } memcpy(status_buf+2, &id, sizeof(id)); LM_INFO("Status message for %d (%u): response status: [%01x]", ntohl(id), id, status_buf[1]); switch( status_buf[1]) { case 0: // No errors encountered break; case 1: // Processing error case 2: // Missing device token case 3: // Missing topic case 4: // Missing payload case 5: // Invalid token size case 6: // Invalid topic size case 7: // Invalid payload size LM_ERR("APNS push: error is critical will not resend"); break; case 8: // Invalid token resend_pushes(apns, ntohl(id)); break; case 10: // Shutdown case 255: //None (unknown) break; } } while(1); }