예제 #1
0
        private void dropClient(UserThread client)
        {
            client.Stop();
            var disconnectPacket = new Packet(NetConstants.PacketTypes.UserLeft, client.GetBytes());

            sendPacketToAllClients(disconnectPacket);
        }
예제 #2
0
 private void sendPacketToClient(Packet p, UserThread client)
 {
     if (client.StopCalled)
     {
         return;
     }
     try
     {
         var stream = client.TcpClient.GetStream();
         stream.Write(p.Bytes, 0, p.TotalSize);
     }
     catch
     {
         dropClient(client);
     }
 }
예제 #3
0
        private void handleIncomingPacket(UserThread sender, Packet packet)
        {
            int newOffset;

            if (packet.PacketType == NetConstants.PacketTypes.UserRegister)
            {
                var registerString = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset);
                if (registerString == NetConstants.UserRegisterString && !sender.IsRegistered)
                {
                    var name = PacketHelper.GetStringFromBytes(packet.DataBytes, newOffset, out newOffset);
                    sender.Username     = name;
                    sender.IsRegistered = true;

                    var joinPacket = PacketHelper.CreateUserJoinedPacket(sender);
                    sendPacketToAllClientsExcept(joinPacket, sender.UserGuid, true);

                    var clientsPacket = PacketHelper.CreateUsersAlreadyHerePacket(AllRegisteredClients);
                    //Send info of all clients already in lobby to new client
                    sendPacketToClient(clientsPacket, sender);
                }
            }
            if (packet.PacketType == NetConstants.PacketTypes.UserLeft)
            {
                var leftPacket = PacketHelper.CreateUserLeftPacket(sender);
                sendPacketToAllClientsExcept(leftPacket, sender.UserGuid, true);
            }

            if (packet.PacketType == NetConstants.PacketTypes.MemoryData && sender.IsRegistered)
            {
                //Add the clients Guid in front of the data
                var newData   = PacketHelper.ConcatBytes(sender.UserGuid.ToByteArray(), packet.DataBytes);
                var newPacket = new Packet(NetConstants.PacketTypes.MemoryData, newData);
                sendPacketToAllClientsExcept(newPacket, sender.UserGuid, true);
            }

            if (packet.PacketType == NetConstants.PacketTypes.UserChat)
            {
                var text       = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset);
                var chatPacket = PacketHelper.CreateServerToUserChatMessage(sender, text);
                sendPacketToAllClients(chatPacket, true);
            }
        }
예제 #4
0
        private void clientListenLoop(UserThread client)
        {
            var stream = client.TcpClient.GetStream();
            var size   = client.TcpClient.ReceiveBufferSize;

            try
            {
                while (!client.StopCalled)
                {
                    byte[] buffer = new byte[size];
                    stream.Read(buffer, 0, size);
                    handleIncomingPacket(client, new Packet(buffer));
                }
            }
            catch (IOException)
            {
                //Stream was closed, most likely due to the server shutting down
                client.Stop();
            }
        }
예제 #5
0
 private void acceptIncomingConnections(TcpListener tcp)
 {
     while (!_stopCalled)
     {
         try
         {
             var client         = tcp.AcceptTcpClient();
             var clientThread   = new UserThread(client);
             var registerPacket = PacketHelper.CreateServerRegistrationPacket(clientThread.UserGuid);
             sendPacketToClient(registerPacket, clientThread);
             _clients.Add(clientThread);
             clientThread.Start(clientListenLoop, clientStopped);
         }
         catch (SocketException)
         {
             break;
         }
     }
     sendShutdownToAll();
 }
예제 #6
0
        public static Packet CreateServerToUserChatMessage(UserThread sender, string text)
        {
            var data = ConcatBytes(sender.GetBytes(), GetStringBytes(text));

            return(new Packet(NetConstants.PacketTypes.ServerToUserChat, data));
        }
예제 #7
0
 public static Packet CreateUserLeftPacket(UserThread user)
 {
     return(new Packet(NetConstants.PacketTypes.UserLeft, user.GetBytes()));
 }
예제 #8
0
 private void clientStopped(UserThread client)
 {
     client.TcpClient.Close();
 }