Beispiel #1
0
        private void OnClientConnected(MultiPlayerClientConnection newClient)
        {
            lock (connectionsLock)
            {
                var existingConnections = Connections.ToArray();
                Connections.Add(newClient);
                Info.Fire($"Client {newClient.ClientId} arrived");
                foreach (var existingClient in existingConnections)
                {
                    SendMessage(new NewUserMessage()
                    {
                        NewUserId = newClient.ClientId, Recipient = existingClient.ClientId
                    });
                    SendMessage(new NewUserMessage()
                    {
                        NewUserId = existingClient.ClientId, Recipient = newClient.ClientId
                    });
                }
            }

            newClient.OnDisposed(() =>
            {
                lock (connectionsLock)
                {
                    if (Connections.Remove(newClient))
                    {
                        Warning.Fire($"Client {newClient.ClientId} was disconnected");
                        TryBroadcast((conn) => new LeftMessage()
                        {
                            ClientWhoLeft = newClient.ClientId
                        });
                    }
                }
            });
        }
Beispiel #2
0
        public void SendMessageToClient(string message, MultiPlayerClientConnection client)
        {
            if (inProcClients.ContainsKey(client.ClientId) == false)
            {
                throw new IOException("Client not found: " + client.ClientId);
            }

            var inProcClient = inProcClients[client.ClientId];

            inProcClient.MessageReceived.Fire(message);
        }
Beispiel #3
0
 public void SendMessageToClient(string message, MultiPlayerClientConnection client)
 {
     lock (client)
     {
         var bytes       = Encoding.UTF8.GetBytes(message);
         var lengthBytes = BitConverter.GetBytes(bytes.Length);
         var sent        = (client as RemoteSocketConnection).RemoteSocket.Send(lengthBytes);
         if (sent != lengthBytes.Length)
         {
             throw new Exception("WTF");
         }
         sent = (client as RemoteSocketConnection).RemoteSocket.Send(bytes);
         if (sent != bytes.Length)
         {
             throw new Exception("WTF");
         }
     }
 }