コード例 #1
0
        public void Broadcast(object[] args, params EndPointId[] identityIds)
        {
            List <EndPointId> idsList;

            if (identityIds == null)
            {
                idsList = new List <EndPointId>();
            }
            else
            {
                idsList = identityIds.ToList();
            }
            string data = "";

            foreach (object arg in args)
            {
                data += arg + argSplitter.ToString();
            }
            data = data.Substring(0, data.Length - 1);
            TcpClient  client = null;
            EndPointId id     = EndPointId.InvalidIdentityId;

            try
            {
                byte[] b = Encoding.UTF8.GetBytes(data + packetSplitter.ToString());
                foreach (var idAndClient in clients)
                {
                    client = idAndClient.Value;
                    id     = idAndClient.Key;
                    if (!idsList.Contains(id))
                    {
                        client.GetStream().Write(b, 0, b.Length);
                        idsList.Add(id);
                    }
                }
            }
            catch
            {
                ConnectionLostRaise(id, client);
                Broadcast(args, idsList.ToArray()); // Keep send data to all other connected users.
            }
        }
コード例 #2
0
 protected virtual void InitIdentityLocally(NetworkIdentity identity, EndPointId ownerId, IdentityId id, bool spawnDuringSync, params object[] valuesByFields)
 {
     identity.NetworkBehavior = this;
     identity.LocalEndPoint   = localEndPointId;
     identity.OwnerId         = ownerId;
     identity.Id                = id;
     identity.hasAuthority      = ownerId == this.localEndPointId;
     identity.isServerAuthority = ownerId == serverEndPointId;
     if (valuesByFields != null && valuesByFields.Length != 0)
     {
         Dictionary <string, string> valuesByFieldsDict = valuesByFields.Select(v => v.ToString().Split('+')).ToDictionary(k => k[0], v => v[1]);
         SetObjectFieldsByValues(identity, valuesByFieldsDict);
         identity.hasFieldsBeenInitialized = true;
     }
     identity.AddToEntities();
     if (!spawnDuringSync)
     {
         CallIdentityEvent(identity);
     }
 }
コード例 #3
0
        private void Synchronize(EndPointId endPointId, EndPoint endPoint)
        {
            lock (clients)
            {
                lock (NetworkIdentity.entities)
                {
                    Console.WriteLine("New player at: " + endPointId);
                    SpawnObjectPacket spawnPacket;
                    clients.Add(endPointId, endPoint);
                    foreach (NetworkIdentity i in NetworkIdentity.entities.Values)
                    {
                        if (i.IsDestroyed)
                        {
                            continue;
                        }

                        spawnPacket = new SpawnObjectPacket(false, GetNetworkClassTypeByName(i.GetType().FullName), i.Id, i.OwnerId, true); //Spawn all existing clients in the remote client
                        SendPacketToAUser(spawnPacket, NetworkInterfaceType.TCP, endPointId);
                    }

                    SyncObjectVars syncObjectVars;
                    foreach (var i in NetworkIdentity.entities.Values)
                    {
                        if (i.IsDestroyed)
                        {
                            continue;
                        }

                        Dictionary <string, string> valuesByFields = GetValuesByFieldsFromObject(i);
                        var args = valuesByFields.Select(k => k.Key + "+" + k.Value).ToArray();
                        syncObjectVars = new SyncObjectVars(false, i.Id, args); //Sync all existing clients vars
                        SendPacketToAUser(syncObjectVars, NetworkInterfaceType.TCP, endPointId);
                    }
                    //Console.WriteLine("Spawn all existing clients");

                    InitiateDircetInterfacePacket initiateDircetInterface = new InitiateDircetInterfacePacket(endPointId);//Initiate dircet interface with the client
                    SendPacketToAUser(initiateDircetInterface, NetworkInterfaceType.TCP, endPointId);
                    //Console.WriteLine("Initiating dircet interface with the client");
                }
            }
        }
コード例 #4
0
 private void AcceptLobbyConnection()
 {
     while (true)
     {
         TcpClient client;
         long      ping;
         try
         {
             client = tcpLobbyListener.AcceptTcpClient();
             EndPointId identityId = EndPointId.FromSocket(((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(), ((IPEndPoint)client.Client.RemoteEndPoint).Port);
             lobbyClients.Add(identityId, client);
             Ping      p     = new Ping();
             PingReply pInfo = p.Send(((IPEndPoint)client.Client.RemoteEndPoint).Address);
             ping = pInfo.RoundtripTime;
             OnConnectionLobbyAcceptedEvent?.Invoke(identityId, ping);
         }
         catch
         {
         }
     }
 }
コード例 #5
0
        private protected virtual void ParsePacket(PacketId packetId, object[] args, EndPointId endPointId, SocketInfo socketInfo)
        {
            switch (packetId)
            {
            case PacketId.LobbyInfo:
                LobbyInfoPacket lobbyInfoPacket = new LobbyInfoPacket(args.ToList());
                ParseLobbyInfoPacket(lobbyInfoPacket, endPointId, socketInfo);
                break;

            case PacketId.BroadcastMethod:
                BroadcastPacket broadcastPacket = new BroadcastPacket(args.ToList());
                ParseBroadcastPacket(broadcastPacket, broadcastPacket.ShouldInvokeSynchronously, endPointId, socketInfo);
                break;

            case PacketId.Command:
                CommandPacket commandPacket = new CommandPacket(args.ToList());
                ParseCommandPacket(commandPacket, commandPacket.ShouldInvokeSynchronously, endPointId, socketInfo);
                break;

            case PacketId.SyncVar:
                SyncVarPacket syncVarPacket = new SyncVarPacket(args.ToList());
                ParseSyncVarPacket(syncVarPacket, endPointId, socketInfo);
                break;

            case PacketId.SpawnObject:
                SpawnObjectPacket spawnObjectPacket = new SpawnObjectPacket(args.ToList());
                ParseSpawnObjectPacket(spawnObjectPacket, endPointId, socketInfo);
                break;

            case PacketId.SyncObjectVars:
                SyncObjectVars syncObjectVars = new SyncObjectVars(args.ToList());
                ParseSyncObjectVars(syncObjectVars, endPointId, socketInfo);
                break;

            default:
                throw new Exception("Invalid packet recived, id: " + args[0]);
            }
        }
コード例 #6
0
 public bool Connect(out long pingMs, out EndPointId endPointId)
 {
     pingMs     = 0;
     endPointId = EndPointId.InvalidIdentityId;
     try
     {
         client = new TcpClient();
         client.Connect(ServerIp, ServerPort);
         stream      = client.GetStream();
         isConnected = true;
         Ping ping = new Ping();
         pingMs     = ping.Send(ServerIp).RoundtripTime;
         endPointId = EndPointId.FromSocket(((IPEndPoint)client.Client.LocalEndPoint).Address.ToString(), ((IPEndPoint)client.Client.LocalEndPoint).Port);
         new Thread(new ThreadStart(TryToRecieve)).Start();
     }
     catch
     {
         ConnectionLostRaise();
         return(false);
     }
     this.endPointId = endPointId;
     return(true);
 }
コード例 #7
0
 private void AcceptConnection()
 {
     while (true)
     {
         TcpClient client;
         long      ping;
         try
         {
             client = tcpListener.AcceptTcpClient();
             string    ip    = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
             int       port  = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
             Ping      p     = new Ping();
             PingReply pInfo = p.Send(((IPEndPoint)client.Client.RemoteEndPoint).Address);
             ping = pInfo.RoundtripTime;
             EndPointId identityId = EndPointId.FromSocket(ip, port);
             OnConnectionAcceptedEvent?.Invoke(identityId, ping);
             clients.Add(identityId, client);
             new Thread(new ThreadStart(() => TryToRecieve(identityId, client, ip, port))).Start();
         }
         catch
         {
         }
     }
 }
コード例 #8
0
 internal void SendToAUser(object[] args, NetworkInterfaceType networkInterface, EndPointId endPointId)
 {
     if (networkInterface == NetworkInterfaceType.TCP)
     {
         server.SendToAUser(args, endPointId);
     }
     else
     {
         directServer.Send(args, clients[endPointId].Ip, clients[endPointId].UdpPort);
     }
 }
コード例 #9
0
 internal void SendPacketToAUser(Packet packet, NetworkInterfaceType networkInterface, EndPointId endPointId)
 {
     SendToAUser(packet.Data.ToArray(), networkInterface, endPointId);
 }
コード例 #10
0
 private protected virtual void ParseSpawnObjectPacket(SpawnObjectPacket spawnObjectPacket, EndPointId endPointId, SocketInfo socketInfo)
 {
     ParseSpawnPacket(spawnObjectPacket, socketInfo);
 }
コード例 #11
0
 private void BroadcastPacket(Packet packet, EndPointId endPointId, SocketInfo socketInfo)
 {
     BroadcastPacket(packet, socketInfo.NetworkInterface, endPointId);
 }
コード例 #12
0
 private protected virtual void ParseSyncVarPacket(SyncVarPacket syncVarPacket, EndPointId endPointId, SocketInfo socketInfo)
 {
     if (TryGetNetworkIdentityByPacket(syncVarPacket, out NetworkIdentity identity))
     {
         NetworkIdentity.NetworkSyncVarInvoke(identity, syncVarPacket, syncVarPacket.ShouldInvokeSynchronously);
     }
     else if (socketInfo.NetworkInterface == NetworkInterfaceType.TCP)
     {
         PrintWarning("cannot get network identity from packet:");
         Print(syncVarPacket.Data.ToArray());
     }
 }
コード例 #13
0
 public InitiateDircetInterfacePacket(EndPointId clientId) : base(PacketId.InitiateDircetInterface, false)
 {
     ClientId = clientId;
     Data.Add(ClientId.Id);
 }
コード例 #14
0
 protected override void InitIdentityLocally(NetworkIdentity identity, EndPointId ownerID, IdentityId id, bool spawnDuringSync, params object[] valuesByFields)
 {
     identity.isInServer = false;
     base.InitIdentityLocally(identity, ownerID, id, spawnDuringSync, valuesByFields);
 }
コード例 #15
0
 public dynamic SpawnWithClientAuthority(Type type, EndPointId clientId)
 {
     return(SpawnIdentity(type, clientId));
 }
コード例 #16
0
 private void Server_connectionAcceptedEvent(EndPointId endPointId, long ping)
 {
     clientsBeforeSync.Add(endPointId);
 }
コード例 #17
0
 private void Server_OnClientDisconnectedEvent(EndPointId endPointId)
 {
     ClientDsiconnected(endPointId);
 }
コード例 #18
0
 private protected virtual void ParseBroadcastPacket(BroadcastPacket broadcastPacket, bool shouldInvokeSynchronously, EndPointId endPointId, SocketInfo socketInfo)
 {
     ParseMethodPacket(broadcastPacket, shouldInvokeSynchronously, socketInfo);
 }
コード例 #19
0
 private protected virtual void ParseCommandPacket(CommandPacket commandPacket, bool shouldInvokeSynchronously, EndPointId endPointId, SocketInfo socketInfo)
 {
     ParseMethodPacket(commandPacket, shouldInvokeSynchronously, socketInfo);
 }
コード例 #20
0
 public NetworkBehavior(int serverPort)
 {
     this.serverPort       = serverPort;
     this.serverEndPointId = EndPointId.FromLong(serverPort);
     hasSynchronized       = false;
 }
コード例 #21
0
 private void Server_OnConnectionLobbyAcceptedEvent(EndPointId endPointId, long ping)
 {
     OnConnectionLobbyAcceptedEvent?.Invoke(endPointId, ping);
 }
コード例 #22
0
 public void InvokeCommandMethodNetworkly(string methodName, EndPointId targetId, params object[] args)
 {
     InvokeCommandMethodNetworkly(methodName, NetworkInterfaceType.TCP, null, targetId, args);
 }
コード例 #23
0
 public dynamic SpawnWithClientAuthority(NetworkIdentity identity, EndPointId clientId)
 {
     return(SpawnIdentity(identity.GetType(), clientId, identity));
 }
コード例 #24
0
        internal SpawnPacket(PacketId packetID, bool shouldInvokeSynchronously, IdentityId id, Type instance, EndPointId ownerId, bool spawnDuringSync, params object[] spawnParams) : base(packetID, shouldInvokeSynchronously, id)
        {
            InstanceName    = instance.FullName;
            OwnerId         = ownerId;
            SpawnDuringSync = spawnDuringSync;
            SpawnParamCount = spawnParams != null?spawnParams.Count() : 0;

            SpawnParams = spawnParams;
            Data.Add(InstanceName);
            Data.Add(OwnerId.Id);
            Data.Add(SpawnDuringSync);
            Data.Add(SpawnParamCount);
            if (SpawnParams != null)
            {
                Data.AddRange(SpawnParams);
            }
        }
コード例 #25
0
 public ServerBehavior(int serverPort) : base(serverPort)
 {
     localEndPointId = EndPointId.FromLong(serverPort);
 }
コード例 #26
0
        private protected override void ParsePacket(PacketId packetId, object[] args, EndPointId endPointId, SocketInfo socketInfo)
        {
            switch (packetId)
            {
            case PacketId.DircetInterfaceInitiating:
                DircetInterfaceInitiatingPacket initiatingPacket = new DircetInterfaceInitiatingPacket(args.ToList());
                EndPoint   eP;
                EndPointId clientId = initiatingPacket.EndPointId;
                lock (clients)
                {
                    if (clients.TryGetValue(clientId, out eP))
                    {
                        eP.UdpPort        = socketInfo.Port;
                        clients[clientId] = eP;
                    }
                    else
                    {
                        PrintWarning("UDP NOT init for: " + clientId);
                    }
                }
                clientsBeforeSync.Remove(clientId);
                OnClientEventHandlerSynchronizedEvent?.Invoke(clientId);
                break;

            case PacketId.BeginSynchronization:
                Synchronize(endPointId, new EndPoint(socketInfo.Ip, socketInfo.Port));
                break;

            default:
                base.ParsePacket(packetId, args, endPointId, socketInfo);
                break;
            }
        }
コード例 #27
0
        private protected override void ParsePacket(PacketId packetId, object[] args, EndPointId endPointId, SocketInfo socketInfo)
        {
            switch (packetId)
            {
            case PacketId.InitiateDircetInterface:
                InitiateDircetInterfacePacket initiateDircetInterfacePacket = new InitiateDircetInterfacePacket(args.ToList());
                localEndPointId = initiateDircetInterfacePacket.ClientId;
                DircetInterfaceInitiatingPacket initiatingPacket = new DircetInterfaceInitiatingPacket(initiateDircetInterfacePacket.ClientId);
                Send(initiatingPacket, NetworkInterfaceType.UDP);
                hasSynchronized = true;
                break;

            default:
                base.ParsePacket(packetId, args, endPointId, socketInfo);
                break;
            }
        }
コード例 #28
0
 private protected override void ParseBroadcastPacket(BroadcastPacket broadcastPacket, bool shouldInvokeSynchronously, EndPointId endPointId, SocketInfo socketInfo)
 {
     BroadcastPacket(broadcastPacket, endPointId, socketInfo);
     base.ParseBroadcastPacket(broadcastPacket, shouldInvokeSynchronously, endPointId, socketInfo);
 }
コード例 #29
0
 public InitiateDircetInterfacePacket(List <object> args) : base(PacketId.InitiateDircetInterface, args)
 {
     ClientId = EndPointId.FromLong(Convert.ToInt64(args[0]));
     args.RemoveAt(0);
 }
コード例 #30
0
 private protected override void ParseSyncVarPacket(SyncVarPacket syncVarPacket, EndPointId endPointId, SocketInfo socketInfo)
 {
     BroadcastPacket(syncVarPacket, endPointId, socketInfo);
     base.ParseSyncVarPacket(syncVarPacket, endPointId, socketInfo);
 }