Ejemplo n.º 1
0
 public void Start()
 {
     Console.AddKeyword("networkDisconnect", this.Disconnect);
     Console.AddKeyword("networkConnect", this.Join);
     Console.AddKeyword("networkHost", this.Host);
     Console.AddKeyword("networkHostSimulated", this.HostSimulated);
     Console.AddKeyword("networkListen", this.Listen);
     Console.AddKeyword("networkChat", this.Chat);
     Console.AddKeyword("networkShowClients", this.ShowClients);
     Console.AddKeyword("networkShowEvents", this.ShowEvents);
     Console.AddKeyword("networkShowHistory", this.ShowHistory);
     Console.AddShortcut("networkDisconnect", "disconnect", "leave");
     Console.AddShortcut("networkConnect", "connect", "join");
     Console.AddShortcut("networkChat", "chat", "msg", "query");
     Console.AddShortcut("networkHostSimulated", "hostSimulated", "startServerSimulated");
     Console.AddShortcut("networkHost", "host", "startServer");
     Console.AddShortcut("networkListen", "listen");
     Console.AddShortcut("networkShowClients", "showClients", "clients");
     Console.AddShortcut("networkShowEvents", "showEvents", "events");
     Console.AddShortcut("networkShowHistory", "showHistory", "history");
     Console.AddShortcut("networkClientName", "clientName", "name");
     Console.AddCvar("networkServerIP", this, "serverIP", "Server IP");
     Console.AddCvar("networkServerPort", this, "serverPort", "Server Port");
     Console.AddCvar("networkClientName", this, "clientName", "Client Name");
     Networker.AddEvent("SyncData", this.SyncData);
     Networker.AddEvent("SyncClient", this.SyncClient);
     Networker.AddEvent("AddClient", this.AddClient);
     Networker.AddEvent("RemoveClient", this.RemoveClient);
 }
Ejemplo n.º 2
0
 public static void SendDataToServer(string name, byte[] data)
 {
     if (!Networker.Get().PrepareEvent(name, ref data))
     {
         return;
     }
     Networker.Get().syncBufferToServer.AddNew(name).AddRange(data);
 }
Ejemplo n.º 3
0
 public void OnEnable()
 {
     Networker.singleton = this;
     if (Proxy.IsPlaying())
     {
         NetworkTransport.Init();
     }
     Events.Add("On Update", this.Update);
     Events.Add("On Start", this.Start);
 }
Ejemplo n.º 4
0
        public static void SendMessage(string name, byte[] data, int channel, int clientID, int excludeID, bool useHistory = true)
        {
            if (!Networker.Get().PrepareEvent(name, ref data))
            {
                return;
            }
            var message = new NetworkerMessage(name, data, channel, clientID, excludeID);

            if (useHistory && clientID == -1)
            {
                Networker.Get().eventHistory.Add(message);
            }
            Networker.SendMessage(message);
        }
Ejemplo n.º 5
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 + ".");
        }
Ejemplo n.º 6
0
        public static void SendMessage(NetworkerMessage message, int?targetID = null)
        {
            var network  = Networker.Get();
            int clientID = targetID.IsNull() ? message.clientID : (int)targetID;

            if (clientID == -1)
            {
                foreach (var client in network.clients)
                {
                    if (message.excludeID == client.id)
                    {
                        continue;
                    }
                    NetworkTransport.Send(network.socketID, client.id, message.channel, message.data, 1024, out network.errorCode);
                }
                return;
            }
            NetworkTransport.Send(network.socketID, clientID, message.channel, message.data, 1024, out network.errorCode);
        }
Ejemplo n.º 7
0
 public static void SendDataToClients(string name, byte[] data, int clientID = -1, int excludeID = -1)
 {
     if (!Networker.Get().PrepareEvent(name, ref data))
     {
         return;
     }
     if (clientID == -1)
     {
         foreach (var client in Networker.Get().clients)
         {
             if (excludeID == client.id)
             {
                 continue;
             }
             Networker.Get().syncBufferToClients.AddNew(client.id).AddNew(name).AddRange(data);
         }
         return;
     }
     Networker.Get().syncBufferToClients.AddNew(clientID).AddNew(name).AddRange(data);
 }
Ejemplo n.º 8
0
 //==============
 // Unity
 //==============
 public static Networker Get()
 {
     Networker.singleton = Networker.singleton ?? Singleton.Get <Networker>();
     return(Networker.singleton);
 }
Ejemplo n.º 9
0
 public static void SendEventToClient(string name, byte[] data, int clientID = -1, int excludeID = -1)
 {
     Networker.SendMessage(name, data, Networker.Get().eventChannel, clientID, excludeID);
 }
Ejemplo n.º 10
0
 public static void SendEventToServer(string name, byte[] data)
 {
     Networker.SendMessage(name, data, Networker.Get().eventChannel, Networker.Get().connectionID, -1);
 }
Ejemplo n.º 11
0
 public static void AddEvent(string name, Action <byte[]> method)
 {
     Networker.Get().events.Add(new NetworkerEvent(name, method));
 }
Ejemplo n.º 12
0
 //==============
 // Global
 //==============
 public static int GetActiveID()
 {
     return(Networker.Get().receivedID);
 }
Ejemplo n.º 13
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");
                }
            }
        }