private int HandleVerifyConnect(Event evnt, Peer peer, Protocol.VerifyConnect command) { if (peer.State != PeerState.CONNECTING) { return(0); } uint channelCount = command.ChannelCount; if (channelCount < MINIMUM_CHANNEL_COUNT || channelCount > MAXIMUM_CHANNEL_COUNT || command.PacketThrottleInterval != peer.PacketThrottleInterval || command.PacketThrottleAcceleration != peer.PacketThrottleAcceleration || command.PacketThrottleDeceleration != peer.PacketThrottleDeceleration) { DispatchState(peer, PeerState.ZOMBIE); return(-1); } RemoveSentReliableCommand(peer, 1, 0xFF); if (channelCount < peer.ChannelCount) { peer.ChannelCount = channelCount; } peer.OutgoingPeerID = command.OutgoingPeerID; ushort mtu = Utils.Clamp(command.MTU, MINIMUM_MTU, MAXIMUM_MTU); if (mtu < peer.MTU) { peer.MTU = mtu; } uint windowSize = Utils.Clamp(command.WindowSize, MINIMUM_WINDOW_SIZE, MAXIMUM_WINDOW_SIZE); if (windowSize < peer.WindowSize) { peer.WindowSize = windowSize; } peer.IncomingBandwidth = command.IncomingBandwidth; peer.OutgoingBandwidth = command.OutgoingBandwidth; NotifyConnect(peer, evnt); return(0); }
private int HandleConnect(Address receivedAddress, ref Peer result, Protocol.Connect command) { uint channelCount = command.ChannelCount; if (channelCount < MINIMUM_CHANNEL_COUNT || channelCount > MAXIMUM_CHANNEL_COUNT) { return(-1); } foreach (var peer in Peers) { if (peer.State != PeerState.DISCONNECTED && peer.Address.Host == receivedAddress.Host && peer.Address.Port == receivedAddress.Port && peer.SessionID == command.SessionID) { return(-1); } } var currentPeer = Array.Find(Peers, (peer) => peer.State == PeerState.DISCONNECTED); if (currentPeer == null) { return(-1); } if (channelCount > ChannelLimit) { channelCount = ChannelLimit; } currentPeer.ResetChannels(); currentPeer.ChannelCount = channelCount; currentPeer.State = PeerState.ACKNOWLEDGING_CONNECT; currentPeer.SessionID = command.SessionID; currentPeer.Address = receivedAddress; currentPeer.OutgoingPeerID = command.OutgoingPeerID; currentPeer.IncomingBandwidth = command.IncomingBandwidth; currentPeer.OutgoingBandwidth = command.OutgoingBandwidth; currentPeer.PacketThrottleInterval = command.PacketThrottleInterval; currentPeer.PacketThrottleAcceleration = command.PacketThrottleAcceleration; currentPeer.PacketThrottleDeceleration = command.PacketThrottleDeceleration; currentPeer.MTU = Utils.Clamp(command.MTU, MINIMUM_MTU, MAXIMUM_MTU); if (OutgoingBandwidth == 0 && currentPeer.IncomingBandwidth == 0) { currentPeer.WindowSize = MAXIMUM_WINDOW_SIZE; } else if (OutgoingBandwidth == 0 || currentPeer.IncomingBandwidth == 0) { currentPeer.WindowSize = (Math.Max(OutgoingBandwidth, currentPeer.IncomingBandwidth) / Peer.WINDOW_SIZE_SCALE) * MINIMUM_WINDOW_SIZE; } else { currentPeer.WindowSize = (Math.Min(OutgoingBandwidth, currentPeer.IncomingBandwidth) / Peer.WINDOW_SIZE_SCALE) * MINIMUM_WINDOW_SIZE; } currentPeer.WindowSize = Utils.Clamp(currentPeer.WindowSize, MINIMUM_WINDOW_SIZE, MAXIMUM_WINDOW_SIZE); uint windowSize = IncomingBandwidth == 0 ? MAXIMUM_WINDOW_SIZE : (IncomingBandwidth / Peer.WINDOW_SIZE_SCALE) * MINIMUM_WINDOW_SIZE; if (windowSize > command.WindowSize) { windowSize = command.WindowSize; } windowSize = Utils.Clamp(windowSize, MINIMUM_WINDOW_SIZE, MAXIMUM_WINDOW_SIZE); var verifyCommand = new Protocol.VerifyConnect { Flags = ProtocolFlag.ACKNOWLEDGE, ChannelID = 0xFF, OutgoingPeerID = currentPeer.IncomingPeerID, MTU = currentPeer.MTU, WindowSize = windowSize, ChannelCount = channelCount, IncomingBandwidth = IncomingBandwidth, OutgoingBandwidth = OutgoingBandwidth, PacketThrottleInterval = currentPeer.PacketThrottleInterval, PacketThrottleAcceleration = currentPeer.PacketThrottleAcceleration, PacketThrottleDeceleration = currentPeer.PacketThrottleDeceleration, }; currentPeer.QueueOutgoingCommand(verifyCommand, null, 0, 0); result = currentPeer; return(0); }