Esempio n. 1
0
        // Send to all connected clients
        public static void RPC(DnlUdpPeer serverPeer, SendPacket packet, int senderNetworkId, RPCTarget peerTarget)
        {
            if (serverPeer == null)
            {
                throw new ArgumentNullException("DnlUdpClient", "Server must be an already initialized DnlUdpClient");
            }
            if (packet == null)
            {
                throw new ArgumentNullException("SendPacket", "Packet must be initialized in order to send to remote clients");
            }
            if (senderNetworkId > 0 && senderNetworkId < MaxNumberOfConnections)
            {
                throw new ArgumentNullException("SenderNetworkId", "Invalid networkid. Ensure sender has registered network id.");
            }

            lock (connectionLock)
            {
                for (int i = 0; i < MaxNumberOfConnections; i++)
                {
                    if (RemoteClients[i] != null && RemoteClients[i].IsConnected)
                    {
                        if (RemoteClients[i].NetworkID == senderNetworkId && peerTarget == RPCTarget.Others)
                        {
                            continue;
                        }
                        else
                        {
                            packet.Send(serverPeer, RemoteClients[i]);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        // This process method is for processing packets from remote clients
        public void Process(DnlUdpPeer client, byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("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();
        }
Esempio n. 3
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);
        }