private void Update() { var sessions = GetEntities <Session>(); var onReceiveEvents = GetEntities <OnReceiveEvent, SocketClientConnection>().Select(e => e.Item1); if (onReceiveEvents.Count() == 0) { onReceiveEvents = GetEntities <OnReceiveEvent, Client>().Select(e => e.Item1); } foreach (var onReceiveEvent in onReceiveEvents) { var gameObject = onReceiveEvent.gameObject; var message = onReceiveEvent.message; var args = message.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (int.TryParse(args[0], out int sessionId)) { message = message.Substring(message.IndexOf(args[0]) + args[0].Length + 1); var session = sessions.Where(s => s.Item1.id == sessionId).FirstOrDefault(); if (session != null) { ECSEvent.Create <OnReceiveEvent>(session.Item1, message); } } } }
private void Update() { foreach (var entity in GetEntities <ClientInputRate, SplitScreenInput, ClientJumpQueue>()) { var lastSent = entity.Item1.lastSent; var jumpQueue = entity.Item3.jumpPresses; var nextSend = lastSent + entity.Item1.sendRateInSeconds; if (Time.time < nextSend) { continue; } var input = entity.Item2; var sessionEntity = GetEntity <Session>(); if (sessionEntity == null) { continue; } lastSent = Time.time; var jump = jumpQueue.Count > 0 ? jumpQueue.Dequeue() : false; var message = $"{sessionEntity.Item1.id} Input {input.horizontal} {input.vertical} {jump}"; ECSEvent.Create <OnSendEvent>(sessionEntity.Item1, message); } }
private void Update() { var servers = GetEntities <LocalServer>().Select(e => e.Item1); var clients = GetAllEntities <LocalClient>().Select(e => e.Item1); foreach (var server in servers) { var newClients = clients.Except(server.clients); foreach (var newClient in newClients) { server.clients.Add(newClient); newClient.server = server; ECSEvent.Create <OnLocalClientAccepted>(server); ECSEvent.Create <OnLocalClientAccepted>(newClient); } var disconnectedClients = server.clients.Except(clients); for (int i = server.clients.Count - 1; i >= 0; i--) { var disconnectedClient = server.clients[i]; if (disconnectedClients.Contains(disconnectedClient)) { server.clients.RemoveAt(i); ECSEvent.Create <OnLocalClientDisconnected>(server); ECSEvent.Create <OnLocalClientDisconnected>(disconnectedClient); } } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, SocketClientConnection>()) { var args = entity.Item1.message.Split(' '); if (args.Length != 2) { continue; } var command = args[0].ToLower(); if (command == "registernewsession") { var client = entity.Item2; var newSessionEntity = gameObject.scene.NewGameObject(); var session = newSessionEntity.AddComponent <Session>(); session.id = Session.GetNextId(); session.build = args[1]; session.nickname = "Guest" + session.id; session.connection = client; session.lastReceived = Time.time; session.name = "Session" + session.id; var message = $"AddSession {session.id} {session.build} {session.nickname}"; ECSEvent.Create <OnSendEvent>(client.gameObject, message); } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; if (args.Length != 2) { continue; } var command = args[1].ToLower(); var server = entity.Item2; if (command == "help" || command == "?") { ECSEvent.Create <OnSendEvent>(server, @"---------- Commands: ---------- Connect {{IP{{:PORT}}}} Disconnect Exit/Quit Restart Test Chat {{ChatMessage}} Whisper {{ClientIndex}} {{ChatMessage}} Help/?"); } } }
private void Update() { foreach (var entity in GetEntities <RoundTripTime, Session>()) { var rtt = entity.Item1; var session = entity.Item2; if (Time.time < rtt.nextUpdateTime) { continue; } rtt.nextUpdateTime = Time.time + rtt.updateRate; foreach (var clientTick in GetEntities <ClientTick>()) { var tick = clientTick.Item1.lastReceivedTick; rtt.AddNewTime(tick, Time.time); ECSEvent.Create <OnSendEvent>(session, $"Ping {tick}"); } foreach (var serverTick in GetEntities <ServerTick>()) { var tick = serverTick.Item1.tick; rtt.AddNewTime(tick, Time.time); ECSEvent.Create <OnSendEvent>(session, $"Ping {tick}"); } } foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; var session = entity.Item2; if (args.Length < 1) { continue; } var command = args[0].ToLower(); if (command == "ping") { var tick = Convert.ToInt32(args[1]); ECSEvent.Create <OnSendEvent>(session, $"Pong {tick}"); } if (command == "pong") { var tick = Convert.ToInt32(args[1]); foreach (var rttEntity in GetEntities <RoundTripTime>()) { var rtt = rttEntity.Item1; var rttSendTime = rtt.sendTimes.Where(st => st.tick == tick).FirstOrDefault(); if (rttSendTime != null) { rtt.RTT = Time.time - rttSendTime.time; } rtt.RemoveTimesOnOrBeforeTick(tick); } } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; if (args.Length < 2) { continue; } var command = args[0].ToLower(); var client = entity.Item2; if (command == "destroy") { var entityId = Convert.ToInt32(args[1]); foreach (var toBeDestroyed in GetEntities <EntityId>()) { if (toBeDestroyed.Item1.entityId == entityId) { toBeDestroyed.Item1.gameObject.AddComponent <EntityDestroyed>(); } } var message = $"Entity Id {entityId} has been destroyed."; ECSEvent.Create <OnSendEvent>(client.gameObject, message); } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; if (args.Length < 3) { continue; } var command = args[1].ToLower(); var session = entity.Item2; if (command == "chat") { var chatMessage = entity.Item1.message; chatMessage = chatMessage.Substring(chatMessage.ToLower().IndexOf("chat") + 4).Trim(); if (session && !string.IsNullOrEmpty(session.nickname)) { chatMessage = $"{session.nickname}: {chatMessage}"; } var client = entity.Item2; foreach (var otherSession in GetEntities <Session>().Where(s => s.Item1 != session).Select(s => s.Item1)) { ECSEvent.Create <OnSendEvent>(otherSession, chatMessage); } } } }
private void Update() { foreach (var entity in GetEntities <ClientOnOpenEvent, Client>()) { var client = entity.Item2; if (!client) { continue; } var message = "RegisterNewSession"; #if UNITY_EDITOR message += " Editor"; #elif UNITY_WEBGL message += " WebGL"; #elif UNITY_STANDALONE_WIN message += " Windows"; #elif UNITY_STANDALONE_OSX message += " OSX"; #elif UNITY_STANDALONE_LINUX message += " Linux"; #endif ECSEvent.Create <OnSendEvent>(client.gameObject, message); } }
private void Update() { foreach (var entity in GetEntities <OnSendEvent, LocalClient>()) { var message = entity.Item1.message; var client = entity.Item2; var server = client.server; ECSEvent.Create <OnReceiveEvent>(server, message); } }
private void OnMessage(Object server, Message <Socket> clientMessage) { var connection = clients.Where(c => c.socket == clientMessage.client).FirstOrDefault(); if (connection == null) { return; } ECSEvent.Create <OnReceiveEvent>(connection, clientMessage.data); }
private void Update() { foreach (var inputEntity in GetEntities <InputFieldSubmitEvent>()) { var message = inputEntity.Item1.text; foreach (var sessionEntity in GetEntities <Session>()) { var session = sessionEntity.Item1; ECSEvent.Create <OnSendEvent>(session.gameObject, message); } } }
private void Update() { foreach (var entity in GetEntities <OnSendEvent, LocalServer>()) { var message = entity.Item1.message; var server = entity.Item2; foreach (var client in server.clients) { ECSEvent.Create <OnReceiveEvent>(client, message); } } }
private void Update() { foreach (var entity in GetEntities <InputFieldSubmitEvent>()) { var message = entity.Item1.text; foreach (var server in GetEntities <Server>()) { var sender = server.Item1.gameObject; ECSEvent.Create <OnSendEvent>(sender, message); } } }
public void CreateSubmitEvent() { var text = inputField.text; if (text.Trim() != "") { ECSEvent.Create <InputFieldSubmitEvent>(gameObject, text); } inputField.text = ""; inputField.ActivateInputField(); }
private void OnClose(Object server, Socket client) { var connection = clients.Where(c => c.socket == client).FirstOrDefault(); if (connection == null) { return; } clients.Remove(connection); ECSEvent.Create <ServerOnCloseEvent>(gameObject, connection); Entity.Destroy(connection.gameObject); }
private void Update() { foreach (var entity in GetEntities <InputFieldSubmitEvent>()) { var message = entity.Item1.text; var peerToPeerClient = GetEntity <LocalPeerClient>(); if (peerToPeerClient != null) { var sender = peerToPeerClient.Item1.gameObject; ECSEvent.Create <OnSendEvent>(sender, message); } } }
private void OnOpen(Object server, Socket client) { var connectionGameObject = gameObject.scene.NewGameObject(); connectionGameObject.name = "Client Connection " + client.GetHashCode(); var connection = connectionGameObject.AddComponent <SocketClientConnection>(); connection.socket = client; connection.host = ((IPEndPoint)client.RemoteEndPoint).Address.ToString(); connection.port = ((IPEndPoint)client.RemoteEndPoint).Port; connection.protocol = client.ProtocolType.ToString().ToUpper(); clients.Add(connection); ECSEvent.Create <ServerOnOpenEvent>(gameObject, connection); }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; if (args.Length != 2) { continue; } var command = args[0].ToLower(); if (command == "create") { var prefabList = GetEntity <PrefabList>(); if (prefabList == null) { continue; } if (args.Length < 2) { continue; } var prefabName = args[1]; var prefab = prefabList.Item1.prefabList.prefabs .Where(p => p.name.ToLower() == prefabName.ToLower()) .FirstOrDefault(); if (!prefab) { continue; } var session = entity.Item2; var instance = session.gameObject.scene.Instantiate(prefab); instance.transform.position = new Vector3(0, 5, 0); instance.layer = LayerMask.NameToLayer("Server"); if (prefabName.ToLower().Contains("player")) { var tcpInput = instance.AddComponent <SessionInput>(); tcpInput.sessionId = session.id; } var message = $"{prefab.name} created with EntityId {EntityId.nextEntityId}"; ECSEvent.Create <OnSendEvent>(session, message); } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.message.Split(new[] { " " }, StringSplitOptions.None); if (args.Length < 2) { continue; } var session = entity.Item2; var command = args[1].ToLower(); if (command == "test") { ECSEvent.Create <OnSendEvent>(session, "Server reply to Test Command"); } } }
private void Update() { foreach (var entity in GetAllEntities <OnSendEvent, LocalPeerClient>()) { var message = entity.Item1.message; var peer = entity.Item2; foreach (var otherEntity in GetEntities <LocalPeerClient>()) { var otherPeer = otherEntity.Item1; if (peer == otherPeer) { continue; } ECSEvent.Create <OnReceiveEvent>(otherPeer, message); } } }
private void Update() { foreach (var entity in GetEntities <OnSendEvent, Session>()) { var sendEvent = entity.Item1; var session = entity.Item2; var message = $"{session.id} {sendEvent.message}"; if (session.connection != null) { ECSEvent.Create <OnSendEvent>(session.connection.gameObject, message); } else { var client = GetEntity <Client>(); if (client != null) { ECSEvent.Create <OnSendEvent>(client.Item1.gameObject, message); } } } }
private void Update() { foreach (var entity in GetEntities <OnReceiveEvent, Session>()) { var args = entity.Item1.Args; if (args.Length < 3) { continue; } var session = entity.Item2; var sessionId = session.id; var command = args[1].ToLower(); if (command == "whisper") { var reciepient = args[2]; var chatMessage = entity.Item1.message; chatMessage = chatMessage.Substring(chatMessage.ToLower().IndexOf("whisper") + 7); chatMessage = chatMessage.Substring(chatMessage.IndexOf(reciepient) + reciepient.Length); chatMessage = chatMessage.Trim(); if (session && !string.IsNullOrEmpty(session.nickname)) { chatMessage = $"{session.nickname} (whisper): {chatMessage}"; } var reciepientSession = GetEntities <Session>() .Where(s => s.Item1.id.ToString() == reciepient || s.Item1.nickname == reciepient) .Select(s => s.Item1).FirstOrDefault(); if (reciepientSession != null) { ECSEvent.Create <OnSendEvent>(reciepientSession, chatMessage); } } } }
private void Update() { foreach (var entity in GetEntities <ClientInputRate, SplitScreenInput, ClientTick>()) { foreach (var sessionEntity in GetEntities <Session>()) { var lastSent = entity.Item1.lastSent; var nextSend = lastSent + entity.Item1.sendRateInSeconds; var input = entity.Item2; var clientTick = entity.Item3; var session = sessionEntity.Item1; if (Time.time < nextSend) { continue; } entity.Item1.lastSent = lastSent = Time.time; var message = $"Input {clientTick.tick} {input.horizontal} {input.vertical} {input.jumpPressed}"; ECSEvent.Create <OnSendEvent>(session, message); } } }
private void Update() { var clientPrediction = GetEntity <ClientPrediction>(); foreach (var entity in GetEntities <SplitScreenInput, ClientTick, InputBuffer>()) { var input = entity.Item1; var clientTick = entity.Item2.tick; var inputBuffer = entity.Item3; var jump = input.jumpPressed; if (clientPrediction != null && clientPrediction.Item1.jumpPresses.Count > 0) { jump = clientPrediction.Item1.jumpPresses.Dequeue(); } if (inputBuffer.inputs.Count > 0) { while (inputBuffer.inputs.Last().tick + 1 < clientTick) { inputBuffer.AddNewInput(inputBuffer.inputs.Last().tick + 1, input.horizontal, input.vertical, false); } inputBuffer.AddNewInput(clientTick, input.horizontal, input.vertical, jump); } else { inputBuffer.AddNewInput(clientTick, input.horizontal, input.vertical, jump); } } foreach (var entity in GetEntities <ClientInputRate, ClientTick, InputBuffer>()) { foreach (var sessionEntity in GetEntities <Session>()) { var lastSent = entity.Item1.lastSent; var nextSend = lastSent + entity.Item1.sendRateInSeconds; var clientTick = entity.Item2; var inputBuffer = entity.Item3; var session = sessionEntity.Item1; if (Time.time < nextSend) { continue; } entity.Item1.lastSent = lastSent = Time.time; inputBuffer.RemoveTimesOnOrBeforeTick(clientTick.lastReceivedTick); if (inputBuffer.inputs.Count > 0) { var message = "Input"; foreach (var input in inputBuffer.inputs) { message += $" {input.tick} {input.horizontal} {input.vertical} {input.jumpPressed}"; } ECSEvent.Create <OnSendEvent>(session, message); } } } }
private void OnError(Object server, Exception exception) { ECSEvent.Create <OnErrorEvent>(gameObject, exception); }
private void OnMessage(Object server, Message message) { ECSEvent.Create <OnReceiveEvent>(gameObject, message.data); }
private void OnClose(Object client) { ECSEvent.Create <ClientOnCloseEvent>(gameObject); }
private void OnOpen(Object client) { ECSEvent.Create <ClientOnOpenEvent>(gameObject); }
private void Update() { var message = ""; foreach (var entity in GetEntities <EntityId, PrefabName>()) { var entityId = entity.Item1.entityId; var prefabName = entity.Item2.prefabName; message += $" {entityId} {prefabName}"; var movementNetworkSync = entity.Item1.GetComponent <MovementNetworkSync>(); if (movementNetworkSync) { if (movementNetworkSync.syncPosition) { var position = movementNetworkSync.transform.position; message += $" {position.x} {position.y} {position.z}"; } if (movementNetworkSync.syncRotation) { var rotation = movementNetworkSync.transform.rotation; message += $" {rotation.x} {rotation.y} {rotation.z} {rotation.w}"; } if (movementNetworkSync.syncScale) { var scale = movementNetworkSync.transform.localScale; message += $" {scale.x} {scale.y} {scale.z}"; } if (movementNetworkSync.syncVelocity) { var velocity = Vector3.zero; if (movementNetworkSync.GetComponent <Rigidbody>()) { velocity = movementNetworkSync.GetComponent <Rigidbody>().velocity; } message += $" {velocity.x} {velocity.y} {velocity.z}"; } if (movementNetworkSync.syncAngularVelocity) { var angularVelocity = Vector3.zero; if (movementNetworkSync.GetComponent <Rigidbody>()) { angularVelocity = movementNetworkSync.GetComponent <Rigidbody>().angularVelocity; } message += $" {angularVelocity.x} {angularVelocity.y} {angularVelocity.z}"; } } } foreach (var entity in GetEntities <ServerUpdateRate, ServerTick>()) { var serverRate = entity.Item1; var serverTick = entity.Item2.tick; var nextSendTime = serverRate.lastUpdateSent + serverRate.updateRateInSeconds; if (Time.time < nextSendTime) { continue; } serverRate.lastUpdateSent = Time.time; foreach (var sessionEntity in GetEntities <Session>()) { var session = sessionEntity.Item1; ECSEvent.Create <OnSendEvent>(session, $"Update {serverTick} {message}"); } } }