示例#1
0
        public void Listen()
        {
            var local = "".AsArray();

            this.Host(local);
            this.Join(local);
            Networker.mode = NetworkerMode.Listen;
        }
示例#2
0
        //==============
        // Commands
        //==============
        public void Host(string[] values)
        {
            NetworkID network;
            NodeID    node;

            Networker.mode = NetworkerMode.Server;
            this.ParseAddress(values, ref this.hostIP, ref this.hostPort);
            this.SetupHost();
            NetworkTransport.GetConnectionInfo(this.socketID, this.socketID, out this.hostIP, out this.hostPort, out network, out node, out this.errorCode);
            Log.Show("[Server] Server created at " + this.hostIP + ":" + this.hostPort + ".");
        }
示例#3
0
        public void HostSimulated(string[] values)
        {
            if (values.Length < 2)
            {
                return;
            }
            Networker.mode = NetworkerMode.Server;
            int minLatency = values[1].ToInt();
            int maxLatency = values.Length > 2 ? values[2].ToInt() : minLatency;

            this.SetupHost(minLatency, maxLatency);
            Log.Show("[Server] Simulated Lag Server created at " + this.hostIP + ":" + this.hostPort + ".");
        }
示例#4
0
        public void Disconnect()
        {
            if (!this.setup)
            {
                Log.Show("Not connected.");
                return;
            }
            NetworkTransport.Disconnect(0, this.connectionID, out this.errorCode);
            NetworkTransport.RemoveHost(this.socketID);

            /*NetworkTransport.Shutdown();
             * NetworkTransport.Init();*/
            Networker.mode = NetworkerMode.None;
            this.settings.Channels.Clear();
            this.clients.Clear();
            this.setup = false;
            Log.Show("Disconnected.");
        }
示例#5
0
        public void Update()
        {
            if (!this.setup)
            {
                return;
            }
            int hostID, channelID;
            int maxSize = 1024;

            byte[] buffer = new byte[1024];
            if (this.syncBufferToServer.Count > 0)
            {
                Networker.SendMessage("SyncData", this.syncBufferToServer.SelectMany(x => x.Value).ToArray(), this.syncChannel, this.connectionID, -1, false);
                this.syncBufferToServer.Clear();
            }
            if (this.syncBufferToClients.Count > 0)
            {
                foreach (var clientBuffer in this.syncBufferToClients)
                {
                    Networker.SendMessage("SyncData", clientBuffer.Value.SelectMany(x => x.Value).ToArray(), this.syncChannel, clientBuffer.Key, -1, false);
                }
                this.syncBufferToClients.Clear();
            }
            while (true)
            {
                var networkEvent = NetworkTransport.Receive(out hostID, out this.receivedID, out channelID, buffer, maxSize, out this.bufferSize, out this.errorCode);
                if (!this.CheckErrors())
                {
                    continue;
                }
                if (networkEvent == NetworkEventType.Nothing)
                {
                    break;
                }
                if (networkEvent == NetworkEventType.ConnectEvent)
                {
                    if (this.connectionID == this.receivedID)
                    {
                        Networker.mode = NetworkerMode.Client;
                        Log.Show("Connection successful.");
                        Networker.SendEventToServer("SyncClient", this.clientName.ToStringBytes());
                        return;
                    }
                    Log.Show("[Server] Client connected.");
                }
                else if (networkEvent == NetworkEventType.DataEvent)
                {
                    var eventID = buffer.ReadShort();
                    //Log.Show("Network event received from -- " + this.GetClientName(this.receivedID) + " -- " + this.events[eventID].name);
                    this.events[eventID].method(buffer);
                }
                else if (networkEvent == NetworkEventType.DisconnectEvent)
                {
                    if (this.connectionID == this.receivedID && this.setup)
                    {
                        this.Disconnect();
                        return;
                    }
                    Log.Show("[Server] Client disconnected -- " + this.GetClientName(this.receivedID));
                    this.clients.RemoveAll(x => x.id == this.receivedID);
                    this.eventHistory.RemoveAll(x => x.name == "AddClient" && x.data.ReadInt(2) == this.receivedID);
                    Networker.SendEventToClient("RemoveClient", this.receivedID.ToBytes());
                    this.eventHistory.RemoveAll(x => x.name == "RemoveClient");
                }
            }
        }