Пример #1
0
        public void SyncClient(byte[] data)
        {
            NetworkID network;
            NodeID    node;
            string    name   = data.ReadString(2);
            var       client = new NetworkerClient(this.receivedID, name);

            NetworkTransport.GetConnectionInfo(this.socketID, this.receivedID, out client.ip, out client.port, out network, out node, out this.errorCode);
            this.clients.Add(client);
            foreach (var history in this.eventHistory)
            {
                Networker.SendMessage(history, this.receivedID);
            }
            Networker.SendEventToClient("AddClient", this.receivedID.ToBytes().Append(name));
            Log.Show("[Server] Client " + this.receivedID + " identified as " + name + ".");
        }
Пример #2
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");
                }
            }
        }