void RpcPdu::fragLenSet () { TRACE_CALL; int len = hdrLen () + payloadLen (); m_header.request.hdr.fragLen = len; if (packetType () == RPC_CN_PKT_REQUEST) m_header.request.allocHint = len; else if (packetType () == RPC_CN_PKT_RESPONSE) m_header.response.allocHint = len; }
void Packet::decodePacket() { if (packetType() == PACKET_ERROR) { char message[sizeof(packet.data.error.message)+1]; memset(message, 0, sizeof(message)); strncpy(message, (const char *)packet.data.error.message, sizeof(message)); errorString = message; errorString.remove("\n"); } else errorString = ""; if (packetType() == PACKET_COMMAND) { char cmd[sizeof(packet.data.command.cmd)+1]; memset(cmd, 0, sizeof(cmd)); memcpy(cmd, packet.data.command.cmd, sizeof(packet.data.command.cmd)); commandNameString = cmd; commandNameString.remove("\n"); } else commandNameString = ""; if (packetType() == PACKET_SD_CID) { cardCID.sprintf("%02x %02x %02x %02x %02x %02x %02x %02x", packet.data.cid.cid[0], packet.data.cid.cid[1], packet.data.cid.cid[2], packet.data.cid.cid[3], packet.data.cid.cid[4], packet.data.cid.cid[5], packet.data.cid.cid[6], packet.data.cid.cid[7] ); } else cardCID = ""; if (packetType() == PACKET_SD_CSD) { cardCSD.sprintf("%02x %02x %02x %02x %02x %02x %02x %02x", packet.data.csd.csd[0], packet.data.csd.csd[1], packet.data.csd.csd[2], packet.data.csd.csd[3], packet.data.csd.csd[4], packet.data.csd.csd[5], packet.data.csd.csd[6], packet.data.csd.csd[7] ); } else cardCSD = ""; }
// Try to get a single packet from a stream of data. // Returns 0 if the packet was successfully processed, or // -1 on an error, in which case the state is unchanged. static ssize_t dataReceivedSingle(NetConnection *conn, const uint8 *data, size_t dataLen) { uint32 packetLen; PacketType type; int result; if (dataLen < sizeof (PacketHeader)) { // Incomplete packet. We'll have to wait for the rest. return 0; } packetLen = packetLength((const Packet *) data); type = packetType((const Packet *) data); if (!validPacketType(type)) { log_add(log_Warning, "Packet with invalid type %d received.\n", type); errno = EBADMSG; return -1; } if (packetLen < packetTypeData[type].len) { // Bad len field of packet. log_add(log_Warning, "Packet with bad length field received (type=" "%s, lenfield=%d.\n", packetTypeData[type].name, packetLen); errno = EBADMSG; return -1; } if (dataLen < packetLen) { // Incomplete packet. We'll have to wait for the rest. return 0; } #ifdef NETPLAY_STATISTICS NetConnection_getStatistics(conn)->packetsReceived++; NetConnection_getStatistics(conn)->packetTypeReceived[type]++; #endif #ifdef NETPLAY_DEBUG if (type != PACKET_BATTLEINPUT && type != PACKET_CHECKSUM) { // Reporting BattleInput and Checksum would get so spammy that it // would slow down the battle. log_add(log_Debug, "NETPLAY: [%d] <== Received packet of type %s.\n", NetConnection_getPlayerNr(conn), packetTypeData[type].name); } #endif result = packetTypeData[type].handler(conn, data); if (result == -1) { // An error occured. errno is set by the handler. return -1; } return packetLen; }
void queuePacket(NetConnection *conn, Packet *packet, bool urgent) { PacketQueue *queue; PacketQueueLink *link; assert(NetConnection_isConnected(conn)); assert(!urgent || !packetTypeData[packetType(packet)].inTurn); // Urgent packets should never stall the connection. queue = &conn->queue; link = PacketQueueLink_alloc(); link->packet = packet; link->next = NULL; if (urgent) { *queue->endUrgent = link; queue->endUrgent = &link->next; } else { *queue->end = link; queue->end = &link->next; } queue->size++; // XXX: perhaps check that this queue isn't getting too large? #ifdef NETPLAY_DEBUG if (packetType(packet) != PACKET_BATTLEINPUT && packetType(packet) != PACKET_CHECKSUM) { // Reporting BattleInput or Checksum would get so spammy that it // would slow down the battle. log_add(log_Debug, "NETPLAY: [%d] ==> Queueing packet of type %s.\n", NetConnection_getPlayerNr(conn), packetTypeData[packetType(packet)].name); } #endif }
// If an error occurs during sending, we leave the unsent packets in // the queue, and let the caller decide what to do with them. // This function may return -1 with errno EAGAIN if we're waiting for // the other party to act first. static int flushPacketQueueLinks(NetConnection *conn, PacketQueueLink **first) { PacketQueueLink *link; PacketQueueLink *next; PacketQueue *queue = &conn->queue; for (link = *first; link != NULL; link = next) { if (packetTypeData[packetType(link->packet)].inTurn && (!conn->stateFlags.myTurn || conn->stateFlags.endingTurn)) { // This packet requires it to be 'our turn', and it isn't, // or we've already told the other party we wanted to end our // turn. // This should never happen in the urgent queue. assert(first != &queue->firstUrgent); if (!conn->stateFlags.myTurn && !conn->stateFlags.endingTurn) { conn->stateFlags.endingTurn = true; if (sendEndTurnDirect(conn) == -1) { // errno is set *first = link; return -1; } } *first = link; errno = EAGAIN; // We need to wait for the reply to the turn change. return -1; } if (sendPacket(conn, link->packet) == -1) { // Errno is set. *first = link; return -1; } next = link->next; Packet_delete(link->packet); PacketQueueLink_delete(link); queue->size--; } *first = link; return 0; }
uint8_t Packet::resetVersion() const { if (packetType() != PACKET_RESET) return 0; return packet.data.reset.version; }
uint8_t Packet::sdRegister() const { if (packetType() != PACKET_SD_CMD_ARG) return 0; return packet.data.sd_cmd_arg.reg; }
//----------------------------------------------------------------------------- CCSDS::PACKET::TCpacket::TCpacket() //----------------------------------------------------------------------------- { packetType(TC_PACKET_TYPE); }
uint8_t Packet::nandControl() const { if (packetType() != PACKET_NAND_CYCLE) return 0; return packet.data.nand_cycle.control; }
bool RpcPdu::isFault () const { return (packetType () == RPC_CN_PKT_FAULT); }
uint8_t Packet::nandData() const { if (packetType() != PACKET_NAND_CYCLE) return 0; return nand_unscramble_byte(packet.data.nand_cycle.data); }
bool RpcPdu::isBindAck () const { return (packetType () == RPC_CN_PKT_BIND_ACK); }
bool RpcPdu::isAuth3 () const { return (packetType () == RPC_CN_PKT_AUTH3); }
bool RpcPdu::isRequest () const { return (packetType () == RPC_CN_PKT_REQUEST); }
uint32_t Packet::errorSubsystem() const { if (packetType() != PACKET_ERROR) return 0; return packet.data.error.subsystem; }
uint32_t Packet::errorArgument() const { if (packetType() != PACKET_ERROR) return 0; return _ntohs(packet.data.error.arg); }
uint32_t Packet::errorCode() const { if (packetType() != PACKET_ERROR) return 0; return packet.data.error.code; }
uint16_t Packet::nandUnknown() const { if (packetType() != PACKET_NAND_CYCLE) return 0; return _ntohs(packet.data.nand_cycle.unknown); }
bool RpcPdu::isBind () const { return ((packetType () == RPC_CN_PKT_BIND) || (packetType () == RPC_CN_PKT_ALTER_CONTEXT)); }
uint8_t Packet::sdValue() const { if (packetType() != PACKET_SD_CMD_ARG) return 0; return packet.data.sd_cmd_arg.val; }
bool RpcPdu::isBindNak () const { return (packetType () == RPC_CN_PKT_BIND_NAK); }
uint8_t Packet::commandState() const { if (packetType() != PACKET_COMMAND) return 0; return packet.data.command.start_stop; }
bool RpcPdu::isResponse () const { return (packetType () == RPC_CN_PKT_RESPONSE); }
uint32_t Packet::commandArg() const { if (packetType() != PACKET_COMMAND) return 0; return _ntohl(packet.data.command.arg); }
bool RpcPdu::isAlterContextResp () const { return (packetType () == RPC_CN_PKT_ALTER_CONTEXT_RESP); }
uint8_t Packet::helloVersion() const { if (packetType() != PACKET_HELLO) return 0; return packet.data.hello.version; }
//----------------------------------------------------------------------------- CCSDS::PACKET::TMpacket::TMpacket() //----------------------------------------------------------------------------- { packetType(TM_PACKET_TYPE); }
uint8_t Packet::sdResponse() const { if (packetType() != PACKET_SD_RESPONSE) return 0; return packet.data.response.byte; }
//----------------------------------------------------------------------------- CCSDS::PACKET::TCpacket::TCpacket(size_t p_bufferSize): CCSDS::PACKET::Packet::Packet(p_bufferSize) //----------------------------------------------------------------------------- { packetType(TC_PACKET_TYPE); }
uint8_t Packet::bufferDrainEvent() const { if (packetType() != PACKET_BUFFER_DRAIN) return 0; return packet.data.buffer_drain.start_stop; }