Exemplo n.º 1
0
        /// <summary>
        /// Create a new netcode.io server.
        /// </summary>
        /// <param name="bindAddress">The address to bind to.</param>
        /// <param name="protocolId">The protocol ID.  This should be unique to your game or application.</param>
        /// <param name="privateKey">The symmetric private key used between your clients and the dedicated servers.</param>
        /// <param name="time">The starting time for the server as a double value.  Normally this will be 0 in the constructor.</param>
        public Server(string bindAddress, UInt64 protocolId, byte[] privateKey, double time)
        {
            NetcodeLibrary.Init();

            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }

            if (privateKey.Length != netcodeNATIVE.NETCODE_KEY_BYTES)
            {
                throw new ArgumentException(
                          $"The private symmetric key must be {netcodeNATIVE.NETCODE_KEY_BYTES} bytes long.",
                          nameof(privateKey));
            }

            var unmanagedPrivateKey = Marshal.AllocHGlobal(privateKey.Length);

            Marshal.Copy(privateKey, 0, unmanagedPrivateKey, privateKey.Length);
            _server = netcodeNATIVE.netcode_server_create(
                bindAddress,
                protocolId,
                unmanagedPrivateKey,
                time);
            Marshal.FreeHGlobal(unmanagedPrivateKey);

            if (_server == null)
            {
                throw new InvalidOperationException("Unable to create native netcode.io server");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start the netcode.io server.
        /// </summary>
        /// <param name="maxClients">The maximum number of clients.  Must be greater than 0 and less than or equal to <see cref="NetcodeLibrary.GetMaxClients()"/>.</param>
        public void Start(int maxClients)
        {
            AssertNotDisposed();

            if (maxClients <= 0 || maxClients > NetcodeLibrary.GetMaxClients())
            {
                throw new ArgumentOutOfRangeException(
                          nameof(maxClients),
                          $"Must be greater than 0 and less than or equal to {NetcodeLibrary.GetMaxClients()}");
                ;
            }

            netcodeNATIVE.netcode_server_start(_server, maxClients);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends a packet to a specific client.
        /// </summary>
        /// <param name="clientIndex">The client index.</param>
        /// <param name="packetData">The packet data.  Must be no larger than <see cref="NetcodeLibrary.GetMaxPacketSize()"/></param>
        public void SendPacket(int clientIndex, byte[] packetData)
        {
            AssertNotDisposed();

            if (packetData == null)
            {
                throw new ArgumentNullException(nameof(packetData));
            }

            var maxPacketSize = NetcodeLibrary.GetMaxPacketSize();

            if (packetData.Length > maxPacketSize)
            {
                throw new ArgumentException($"Packet data can not be longer than {maxPacketSize} bytes.", nameof(packetData));
            }

            var unmanagedPointer = Marshal.AllocHGlobal(packetData.Length);

            Marshal.Copy(packetData, 0, unmanagedPointer, packetData.Length);
            netcodeNATIVE.netcode_server_send_packet(_server, clientIndex, unmanagedPointer, packetData.Length);
            Marshal.FreeHGlobal(unmanagedPointer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a new netcode.io client.
        /// </summary>
        /// <param name="bindAddress">The address to bind to.</param>
        /// <param name="time">The starting time for the server as a double value.  Normally this will be 0 in the constructor.</param>
        public Client(string bindAddress, double time)
        {
            NetcodeLibrary.Init();

            _client = netcodeNATIVE.netcode_client_create(bindAddress, time);
        }