コード例 #1
0
        // Use for sending from server to client
        public void Send(byte[] data, int length, RemoteUdpPeer remoteUdpClient)
        {
            if (!IsServer)
            {
                return;
            }

            // Create a new byte array segment with the byte array data we want to send
            sendSegments.Enqueue(new DnlUdpSendSegment(data, remoteUdpClient));

            // Ensure we aren't already trying to send data
            if (Interlocked.CompareExchange(ref isSending, 1, 0) == 0)
            {
                BeginSend();
            }
        }
コード例 #2
0
        // This function determines whether or not an existing connection has timed out
        public static bool HasConnectionTimedOut(RemoteUdpPeer remoteClient, int timeOut)
        {
            if (remoteClient == null)
            {
                throw new ArgumentNullException("RemoteUdpClient", "RemoteUdpClient cannot be null");
            }

            bool     timedOut       = false;
            TimeSpan elapsedSpan    = new TimeSpan(DateTime.Now.Ticks - remoteClient.TimeSinceLastReceivedDatagram.Ticks);
            int      elapsedSeconds = elapsedSpan.Seconds;

            // Connected timed out
            if (elapsedSeconds > timeOut)
            {
                timedOut = true;
            }
            return(timedOut);
        }
コード例 #3
0
        // This client adds a new client and returns the new client id
        public static void Add(RemoteUdpPeer remoteClient, int networkId)
        {
            if (remoteClient == null)
            {
                throw new ArgumentNullException("RemoteUdpClient", "RemoteUdpClient cannot be null.");
            }

            // Ensure valid network id
            if (networkId > 0 && networkId < MaxNumberOfConnections)
            {
                throw new ArgumentOutOfRangeException("NetworkID", "NetworkID must be greater than 0.");
            }

            lock (connectionLock)
            {
                RemoteClients[networkId] = remoteClient;
                CurrentlyConnectedClients++;
            }
        }
コード例 #4
0
        // This is used for sending from server to client
        public void Send(DnlUdpPeer server, RemoteUdpPeer receiver)
        {
            if (server == null)
            {
                throw new NullReferenceException("DsnTcpClient cannot be null. Ensure a valid connection has been initialized.");
            }
            if (receiver == null)
            {
                throw new NullReferenceException("RemoteUdpClient cannot be null. Ensure a valid remote client has been initialized.");
            }

            if (!OperationCodes.SendPacket.ContainsKey(GetType()) || OperationCodes.SendPacket.Count < 0)
            {
                throw new Exception("This instance type is unrecognized in DsnOperationCodes. Register it on both client and server side (if applicable) for this packet to be sent.");
            }

            MemoryStream stream = null;
            BinaryWriter writer = null;

            lock (WriteLock)
            {
                if (Buffer == null)
                {
                    stream = new MemoryStream();
                    using (writer = new BinaryWriter(stream))
                    {
                        if (OperationCodes.SendPacket.Count > 0)
                        {
                            writer.Write(server.ProtocolId);
                            writer.Write(OperationCodes.SendPacket[GetType()]);
                        }
                        Write(writer);
                    }
                    Buffer = new byte[stream.GetBuffer().Length];
                    Buffer = stream.ToArray();
                }
            }
            server.Send(Buffer, Buffer.Length, receiver);
        }
コード例 #5
0
        // This process method is for processing packets from remote clients
        public void Process(DnlUdpPeer client, RemoteUdpPeer sender, byte[] buffer)
        {
            if (client == null)
            {
                throw new ArgumentNullException("Client");
            }
            if (buffer == null)
            {
                throw new NullReferenceException("No data to process. Ensure a valid byte array has been provided.");
            }

            Client = client ?? throw new NullReferenceException("DsnTcpClient cannot be null. Ensure a valid connection has been initialized.");

            MemoryStream stream = new MemoryStream(buffer);

            using (Reader = new BinaryReader(stream))
            {
                Read();
            }

            // Now try to process the packet based on developers needs
            Process(sender);
        }
コード例 #6
0
 public abstract void Process(RemoteUdpPeer remoteClient);