コード例 #1
0
 /// <summary>
 /// Sends a packet to all of the connections.
 /// </summary>
 /// <param name="packet">The packet to send.</param>
 public void SendPacketToAll(NetOutboundPacket packet)
 {
     foreach (NetConnection conn in Connections.Values)
     {
         conn.SendPacket(packet.Clone());
     }
 }
コード例 #2
0
        public virtual bool Connect(IPEndPoint serverAddress, string password, out NetDenialReason?denialReason)
        {
            if (IsConnected)
            {
                throw new NetException("Client is already connected to a server!");
            }
            if (awaitingConnectionResponse)
            {
                throw new NetException("Cannot connect to server, client is already in the middle of connecting!");
            }

            denialReason = null;
            awaitingConnectionResponse = true;

            NetOutboundPacket request = new NetOutboundPacket(NetDeliveryMethod.Unreliable,
                                                              NetPacketType.ConnectionRequest);

            request.Write(password != null);
            if (password != null)
            {
                request.Write(password);
            }

            int tries = 0;

            while (awaitingConnectionResponse && tries < config.MaxConnectionAttempts)
            {
                tries++;
                SendInternalPacket(request.Clone(), serverAddress);

                // Block thread while awaiting connection
                int timeoutAt = NetTime.Now + config.ConnectionAttemptTimeout;
                while (awaitingConnectionResponse && NetTime.Now < timeoutAt)
                {
                    Thread.Sleep(1);
                }
            }

            if (awaitingConnectionResponse)
            {
                awaitingConnectionResponse = false;
                denialReason = NetDenialReason.ConnectionTimedOut;
                return(false);
            }
            else
            {
                if (!lastConnectionApproved)
                {
                    denialReason = lastDenialReason;
                }

                return(lastConnectionApproved);
            }
        }
コード例 #3
0
        NetOutboundPacket[] SplitPacketIntoPartials(NetOutboundPacket packet)
        {
            packet.position = 0;

            // Calculate the number of packets to split it into,
            // and each of their sizes
            int    numPackets       = (int)Math.Ceiling((double)(packet.Length) / (MTU - 12));
            int    newPacketSize    = MTU - 12;
            int    packetChunkIndex = 0;
            ushort id = nextPartialId++;

            NetOutboundPacket[] partialPackets = new NetOutboundPacket[numPackets];

            for (byte i = 0; i < numPackets; i++)
            {
                int packetSize = Math.Min(newPacketSize, packet.Length - packetChunkIndex);

                // Create the new partial
                NetOutboundPacket partial = packet.Clone(true);
                partial.isPartial   = true;
                partial.Encrypt     = false;
                partial.Compression = NetPacketCompression.None;
                partial.Clear(6 + packetSize);

                // Write the partial header
                partial.Write(id);
                partial.Write(i);
                partial.Write((byte)numPackets);
                partial.Write((ushort)packetSize);

                // Write the data allocated to this partial
                partial.WriteBytes(packet.ReadBytes(packetChunkIndex, packetSize));

                // Add it to the list of partials
                partialPackets[i] = partial;
                packetChunkIndex += packetSize;
            }

            if (NetLogger.LogPartials)
            {
                NetLogger.LogVerbose("[Partial] Split Packet into {0} parts; Size: {1}b, Original Size: {2}b",
                                     numPackets, newPacketSize, packet.Length);
            }
            return(partialPackets);
        }