/** Request a disconnection from a peer. @param peer peer to request a disconnection @param data data describing the disconnection @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service() once the disconnection is complete. */ void enet_peer_disconnect (ENetPeer * peer, enet_uint32 data) { ENetProtocol command; if (peer -> state == ENET_PEER_STATE_DISCONNECTING || peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT || peer -> state == ENET_PEER_STATE_ZOMBIE) return; enet_peer_reset_queues (peer); command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT; command.header.channelID = 0xFF; command.disconnect.data = ENET_HOST_TO_NET_32 (data); if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE; else command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED; enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) { enet_peer_on_disconnect (peer); peer -> state = ENET_PEER_STATE_DISCONNECTING; } else { enet_host_flush (peer -> host); enet_peer_reset (peer); } }
/** Force an immediate disconnection from a peer. @param peer peer to disconnect @param data data describing the disconnection @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not guarenteed to receive the disconnect notification, and is reset immediately upon return from this function. */ void enet_peer_disconnect_now (ENetPeer * peer, enet_uint32 data) { ENetProtocol command; if (peer -> state == ENET_PEER_STATE_DISCONNECTED) return; if (peer -> state != ENET_PEER_STATE_ZOMBIE && peer -> state != ENET_PEER_STATE_DISCONNECTING) { enet_peer_reset_queues (peer); command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED; command.header.channelID = 0xFF; command.disconnect.data = ENET_HOST_TO_NET_32 (data); enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); enet_host_flush (peer -> host); } enet_peer_reset (peer); }
/** Forcefully disconnects a peer. @param peer peer to forcefully disconnect @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout on its connection to the local host. */ void enet_peer_reset (ENetPeer * peer) { if (peer -> ownSocket != ENET_SOCKET_NULL) { enet_socket_destroy (peer -> ownSocket); peer -> ownSocket = ENET_SOCKET_NULL; } size_t i; ENetHost * host = peer -> host; for (i = 0; i < host -> peerCount - host -> idlePeers; ++ i) if (host -> busyPeersList [i] == peer -> incomingPeerID) { host -> idlePeersList [host -> idlePeers ++] = peer -> incomingPeerID; host -> busyPeersList [i] = host -> busyPeersList [host -> peerCount - host -> idlePeers]; break; } if (peer -> host -> connectingPeerTimeout && peer -> connectingPeers) { enet_free(peer -> connectingPeers); peer -> connectingPeers = NULL; peer -> host -> connectsWindow -= ENET_PROTOCOL_TOTAL_SESSIONS * (peer -> connectingPeersTimeMask + 1); } enet_peer_on_disconnect (peer); peer -> outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID; peer -> connectID = 0; peer -> state = ENET_PEER_STATE_DISCONNECTED; peer -> incomingBandwidth = 0; peer -> outgoingBandwidth = 0; peer -> incomingBandwidthThrottleEpoch = 0; peer -> outgoingBandwidthThrottleEpoch = 0; peer -> incomingDataTotal = 0; peer -> outgoingDataTotal = 0; peer -> lastSendTime = 0; peer -> lastReceiveTime = 0; peer -> nextTimeout = 0; peer -> earliestTimeout = 0; peer -> packetLossEpoch = 0; peer -> packetsSent = 0; peer -> packetsLost = 0; peer -> packetLoss = 0; peer -> packetLossVariance = 0; peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE; peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE; peer -> packetThrottleCounter = 0; peer -> packetThrottleEpoch = 0; peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION; peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION; peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL; peer -> pingInterval = ENET_PEER_PING_INTERVAL; peer -> timeoutLimit = ENET_PEER_TIMEOUT_LIMIT; peer -> timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM; peer -> timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM; peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> lastRoundTripTimeVariance = 0; peer -> highestRoundTripTimeVariance = 0; peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> roundTripTimeVariance = 0; peer -> mtu = peer -> host -> mtu; peer -> reliableDataInTransit = 0; peer -> outgoingReliableSequenceNumber = 0; peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE; peer -> incomingUnsequencedGroup = 0; peer -> outgoingUnsequencedGroup = 0; peer -> eventData = 0; peer -> totalWaitingData = 0; memset (& peer -> localAddress, 0, sizeof (ENetAddress)); memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow)); enet_peer_reset_queues (peer); }