コード例 #1
0
ファイル: Server.cs プロジェクト: TheDarkBadger/Game-Server
 public LoadedPlayer(ConnectedClient user, Character character, Vector2 startPos)
 {
     User            = user;
     LoadedCharacter = character;
     Position        = startPos;
 }
コード例 #2
0
ファイル: Server.cs プロジェクト: ryangoldsmith/Chat-Facility
 private void TcpSendDataToSpecificClient(ConnectedClient client, Packets.Packet packet)
 {
     client.TcpSend(packet);
 }
コード例 #3
0
        public void OnConnectRequest(IAsyncResult ar)
        {
            TcpListener listener = (TcpListener)ar.AsyncState;

            try
            {
                Socket ClientConnection = listener.EndAcceptSocket(ar);
                listener.BeginAcceptSocket(new AsyncCallback(OnConnectRequest), listener);
                try
                {
                    nBuffer buffer = new nBuffer();
                    buffer.ReceiveBuffer(ClientConnection);
                    if (buffer.GetID() == MsgType.ClientConnect)
                    {
                        ClientConnect   CC     = buffer.GetObject <ClientConnect>();
                        ConnectedClient client = new ConnectedClient();
                        client.socket        = ClientConnection;
                        client.CC            = new ClientConnect();
                        client.CC.sockettype = CC.sockettype;
                        client.CC.Ip         = CC.Ip;
                        client.CC.Salt       = CC.Salt;

                        // Only add to clientlist if NOT FileSocket or LogoSocket
                        client.Online = false;
                        for (int i = 0; i < ConnectedClients.Count; i++)
                        {
                            if (ConnectedClients[i].CC.Ip == CC.Ip) // Second connection from same Ip should be discarded
                            {
                                if (CC.sockettype == ConnectionType.Client && ConnectedClients[i].CC.sockettype == ConnectionType.Client)
                                {
                                    client.Online       = true;
                                    ConnectedClients[i] = client;
                                    Console.WriteLine("Client " + client.socket.RemoteEndPoint.ToString() + " of Type " + CC.sockettype.ToString() + ", Reconnected");
                                }
                            }
                        }
                        if (!client.Online)
                        {
                            ConnectedClients.Add(client);
                            client.Online = true;
                            Console.WriteLine("Client " + client.socket.RemoteEndPoint.ToString() + " of Type " + CC.sockettype.ToString() + " joined");
                        }
                        client.StartThread(recvMessage);
                    }

                    Console.WriteLine("Ready to receive next Incomming connection");
                }
                catch (Exception e)
                {
                    ClientConnection.Shutdown(SocketShutdown.Both);
                    ClientConnection.Close(1);
                    listener.BeginAcceptSocket(new AsyncCallback(OnConnectRequest), listener);

                    Console.WriteLine("Client " + ClientConnection.RemoteEndPoint + ", timed-out!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + "\r\n " + e.Message);
            }
        }
コード例 #4
0
ファイル: Server.cs プロジェクト: ryangoldsmith/Chat-Facility
        private void TcpProcessDataSentFromClient(Packets.Packet data, ConnectedClient client)
        {
            switch (data.m_PacketType)
            {
            case Packets.Packet.PacketType.Login:
                Packets.LoginPacket loginPacket = data as Packets.LoginPacket;
                client.Login(loginPacket.Nickname, loginPacket.Endpoint, loginPacket.PublicKey);
                PrintToConsoleAsLogMessage("[TCP] " + loginPacket.Nickname + " Joined the Server [" + loginPacket.Endpoint + "]");
                UpdateClientsOnlineBox();
                SendEncryptedChatPacket("[Server] " + connectedClients.Last().GetNickname() + " has joined the chat!");
                break;

            case Packets.Packet.PacketType.Nickname:
                Packets.NicknamePacket nicknamePacket = data as Packets.NicknamePacket;
                if (nicknamePacket.Name == client.GetNickname())
                {
                    TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] You Can't Change Your Name to the Same Name")));
                    return;
                }

                PrintToConsoleAsLogMessage("[TCP] [" + nicknamePacket.m_PacketType + "] from: " + client.GetNickname() + " data: " + nicknamePacket.Name);
                PrintToConsoleAsLogMessage("[TCP] [" + client.GetNickname() + "] Changed Name to " + nicknamePacket.Name);
                UpdateClientsOnlineBox();
                SendEncryptedChatPacket("[Server] " + client.GetNickname() + " Changed Name to " + nicknamePacket.Name);
                client.SetNickname(nicknamePacket.Name);
                break;

            case Packets.Packet.PacketType.ChatMessage:
                Packets.ChatMessagePacket messagePacket = data as Packets.ChatMessagePacket;
                string chatMessage = client.DecryptString(messagePacket.Message);
                SendEncryptedChatPacket("[" + client.GetNickname() + "] " + chatMessage);
                PrintToConsoleAsLogMessage("[TCP] [" + messagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Message: " + chatMessage);
                break;

            case Packets.Packet.PacketType.PrivateMessage:
                Packets.PrivateMessagePacket privateMessagePacket = data as Packets.PrivateMessagePacket;

                foreach (ConnectedClient target in connectedClients)
                {
                    if (target.GetNickname().ToLower() == privateMessagePacket.TargetUser.ToLower())
                    {
                        if (target == client)
                        {
                            PrintToConsoleAsLogMessage("[TCP] [Error] [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Tried to send a Private Message to Themself");
                            TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] You Can't Message Yourself")));
                            return;
                        }

                        string privateMessage = client.DecryptString(privateMessagePacket.Message);
                        PrintToConsoleAsLogMessage("[TCP] [" + privateMessagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] " +
                                                   "To [" + target.GetNickname() + " " + target.GetSocket().RemoteEndPoint + "] Message: " + privateMessage);

                        privateMessagePacket.Message = client.EncryptString("[To " + target.GetNickname() + "] " + privateMessage);
                        TcpSendDataToSpecificClient(client, privateMessagePacket);
                        privateMessagePacket.Message = target.EncryptString("[From " + client.GetNickname() + "] " + privateMessage);
                        TcpSendDataToSpecificClient(target, privateMessagePacket);
                        return;
                    }
                }

                PrintToConsoleAsLogMessage("[TCP] [Error] [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Tried to send a Private Message to a client that does not exist");
                TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] User not Found")));
                break;

            case Packets.Packet.PacketType.GameConnectionPacket:
                Packets.GameConnectionPacket gameConnectionPacket = data as Packets.GameConnectionPacket;

                if (gameConnectionPacket.GameToPlay == Packets.Packet.GameType.Pictionary)
                {
                    if (pictionaryLobby.Contains(client))
                    {
                        pictionaryLobby.Remove(client);
                        SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                        if (playingPictionary)
                        {
                            foreach (ConnectedClient c in pictionaryLobby)
                            {
                                TcpSendDataToSpecificClient(c, new Packets.PictionaryChatMessagePacket(c.EncryptString("[Server] " + c.GetNickname() + " Has Left The Game!")));
                            }
                        }
                        if (pictionaryLobby.Count == 0)
                        {
                            playingPictionary = false;
                        }
                    }
                    else
                    {
                        if (pictionaryLobby.Count > pictionaryLobbyMaxSize - 1 || playingPictionary)
                        {
                            TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Server] Server is Full")));
                        }
                        else
                        {
                            pictionaryLobby.Add(client);
                            SendEncryptedChatPacket("[Server] " + client.GetNickname() + " have joined the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                        }
                    }

                    if (pictionaryLobby.Count > pictionaryLobbyMaxSize - 1)
                    {
                        StartPictionaryRound();
                    }
                }
                break;

            case Packets.Packet.PacketType.Disconnect:
                PrintToConsoleAsLogMessage("[TCP] " + client.GetNickname() + " Left The Server. [" + client.GetSocket().RemoteEndPoint + "]");
                SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the chat!");
                if (pictionaryLobby.Contains(client))
                {
                    SendEncryptedPictionartChatPacket("[Server] " + client.GetNickname() + " has Left the Game!");
                    SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                    pictionaryLobby.Remove(client);
                }
                threads.RemoveAt(connectedClients.IndexOf(client));
                connectedClients.Remove(client);

                client.CloseConnection();
                UpdateClientsOnlineBox();
                break;

            case Packets.Packet.PacketType.PictionaryChatMessage:
                Packets.PictionaryChatMessagePacket pictionaryChatMessagePacket = data as Packets.PictionaryChatMessagePacket;
                string pictionaryChatMessage = client.DecryptString(pictionaryChatMessagePacket.Message);
                SendEncryptedPictionartChatPacket("[" + client.GetNickname() + "] " + pictionaryChatMessage);
                PrintToConsoleAsLogMessage("[TCP] [" + pictionaryChatMessagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Message: " + pictionaryChatMessage);

                if (pictionaryChatMessage.ToLower() == pictionaryCurrentWordBeingDrawn)
                {
                    pictionaryScores[pictionaryLobby.IndexOf(client)]++;
                    SendEncryptedPictionartChatPacket("[Server] " + client.GetNickname() + " Guessed The Word!");
                    SendEncryptedPictionartChatPacket("[Server] New Round!");

                    foreach (ConnectedClient c in pictionaryLobby)
                    {
                        TcpSendDataToSpecificClient(c, new Packets.PictionaryClearCanvasPacket());
                    }
                    StartPictionaryRound();
                }
                break;
            }
        }
コード例 #5
0
        //every client runs on its own thread.
        //File send and receives also runs on its own thread. this is to keep GUI responsive.
        private void recvMessage(object s)
        {
            ConnectedClient client       = (ConnectedClient)s;
            bool            bRecvMessage = true;

            while (bRecvMessage)
            {
                try
                {
                    nBuffer buffer = new nBuffer();
                    int     ReadBytes;
                    if ((ReadBytes = buffer.ReceiveCryptoBuffer(client.socket, client.CC.Salt)) > 0)
                    {
                        #region RecvClientSettings
                        if (buffer.GetID() == MsgType.ClientSettings)
                        {
                            Console.WriteLine(client.socket.RemoteEndPoint.ToString() + ": Received Client settings");
                            ClientSettings CSettings;

                            CSettings    = buffer.GetObject <ClientSettings>();
                            CSettings.Ip = client.CC.Ip;

                            bool InGroupList = false;

                            for (int i = 0; i < ConnectedClients.Count(); i++)
                            {
                                if (ConnectedClients[i].CC.Ip == client.CC.Ip)
                                {
                                    Console.WriteLine(ConnectedClients[i].Name + " Renamed to: " + CSettings.Name);
                                    ConnectedClients[i].Name = CSettings.Name;
                                }
                            }
                            client.Groups = CSettings.Groups;
                            string[] tmpGroups = CSettings.Groups.Split(';');
                            foreach (string group in tmpGroups)
                            {
                                if (group != "")
                                {
                                    InGroupList = false;
                                    for (int i = 0; i < ChatGroupsContainer.Instance.Count; i++)
                                    {
                                        if (group == ChatGroupsContainer.Instance.at(i).GroupName)
                                        {
                                            InGroupList = true; // group is already in chatrgrouplist,
                                        }
                                    }
                                    if (!InGroupList)
                                    {
                                        ChatGroup cGroup = new ChatGroup();
                                        cGroup.GroupName = group;
                                        ChatGroupsContainer.Instance.Add(cGroup);
                                        AddGroupToDataGridview(cGroup);
                                        Console.WriteLine("Group " + group + " Added to List");
                                    }
                                    // check if user is in group, else add it, and check if name updated;
                                    for (int i = 0; i < ChatGroupsContainer.Instance.Count; i++)
                                    {
                                        bool UserInList = false;
                                        if (ChatGroupsContainer.Instance.at(i).GroupName == group)
                                        {
                                            for (int j = 0; j < ChatGroupsContainer.Instance.at(i).Members.Count; j++)
                                            {
                                                if (ChatGroupsContainer.Instance.at(i).Members[j].Ip == CSettings.Ip) // Member is in list
                                                {
                                                    UserInList = true;
                                                    ChatGroupsContainer.Instance.at(i).Members[j].Name = CSettings.Name; // update name.
                                                }
                                            }
                                            if (!UserInList)
                                            {
                                                ChatGroupsContainer.Instance.at(i).Members.Add(CSettings);
                                                Console.WriteLine(CSettings.Name + " Added to: " + ChatGroupsContainer.Instance.at(i).GroupName);
                                            }
                                        }
                                    }
                                }
                            }

                            // check all groups if it has the user, if it does, check if the user has specified the group, else delete it from that group
                            for (int j = 0; j < ChatGroupsContainer.Instance.Count; j++)
                            {
                                bool UserInGroup        = false;
                                bool UserSpecifiedGroup = false;
                                int  IndexOfUser        = -1;
                                for (int i = 0; i < ChatGroupsContainer.Instance.WholeList[j].Members.Count; i++)
                                {
                                    if (ChatGroupsContainer.Instance.at(j).Members[i].Ip == CSettings.Ip) // client is in the group
                                    {
                                        UserInGroup = true;
                                        foreach (string Cgroups in tmpGroups) // check if groupname is also specified by teh client;
                                        {
                                            if (Cgroups == ChatGroupsContainer.Instance.at(j).GroupName)
                                            {
                                                UserSpecifiedGroup = true;
                                            }
                                        }
                                        if (!UserSpecifiedGroup)
                                        {
                                            IndexOfUser = i;
                                        }
                                    }
                                }
                                if (UserInGroup && !UserSpecifiedGroup) // user is in chatgroup but not specified in tmpgroups.
                                {
                                    ChatGroupsContainer.Instance.at(j).Members.RemoveAt(IndexOfUser);
                                }
                            }
                            UpdateClientList();
                        }
                        #endregion

                        #region RecvTextMessage

                        if (buffer.GetID() == MsgType.TextMessage)
                        {
                            Console.WriteLine(client.socket.RemoteEndPoint.ToString() + ": Received TextMessage");
                            TextMessage TM = buffer.GetObject <TextMessage>();
                            PassMessage(TM);
                            if (LogChatMessages.Checked)
                            {
                                MessageLogger.LOG(TM, ChatGroupsContainer.Instance.WholeList);
                            }
                        }
                        #endregion

                        #region FileRecv/Send
                        if (buffer.GetID() == MsgType.SendFile) // Server Receives file
                        {
                            PrepareFileSend PFS = buffer.GetObject <PrepareFileSend>();
                            Console.WriteLine(client.socket.RemoteEndPoint.ToString() + ": Send File -> " + PFS.FileName + PFS.Ext);
                            buffer.innerbuffer.Clear();
                            buffer.ReceiveCryptoFileBuffer(client.socket, client.CC.Salt);
                            ServerFile SF = buffer.GetObject <ServerFile>(0);
                            // Encrypt current password into file;
                            byte[] Passwordbytes = Crypt.Base64Encode(BinaryFilesPassword);
                            byte[] binaryfile    = new byte[Passwordbytes.Length + SF.BinaryFile.Length];
                            Buffer.BlockCopy(Passwordbytes, 0, binaryfile, 0, Passwordbytes.Length);
                            Buffer.BlockCopy(SF.BinaryFile, 0, binaryfile, Passwordbytes.Length, SF.BinaryFile.Length); // copy binary file;

                            binaryfile = Crypt.Encrypt(binaryfile, BinaryFilesKey, BinaryFilesPassword);                // encrypt file and store it.

                            string     Path = Utils.AssemblyDirectory + "\\" + PFS.FileName + PFS.Ext + ".scbd";
                            FileStream fs   = null;
                            if (File.Exists(Path))
                            {
                                fs = new FileStream(Path, FileMode.Truncate, FileAccess.ReadWrite); // overwrite file
                            }
                            else
                            {
                                fs = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.ReadWrite); // create new;
                            }
                            if (fs.CanWrite)
                            {
                                fs.Write(binaryfile, 0, binaryfile.Length);
                            }
                            fs.Close();
                            fs.Dispose();
                            SF.length  = binaryfile.Length;
                            SF.Ext     = PFS.Ext;
                            binaryfile = null;

                            // now we need to notify clients that there is a file for them.
                            TextMessage TM = new TextMessage();
                            TM.To      = PFS.to;
                            TM.From    = PFS.from;
                            TM.ID      = ChatBufferMessageType.Link;
                            TM.Message = PFS.FileName + PFS.Ext;
                            PassMessage(TM);

                            buffer.innerbuffer.Clear();
                            buffer.AddID(MsgType.FileSendComplete);
                            FileSendComplete FSC = new FileSendComplete();
                            FSC.complete = true;
                            buffer.AddObject(FSC);
                            buffer.SendCryptoBuffer(client.socket, client.CC.Salt);
                        }

                        #endregion

                        #region File Request Awsering
                        if (buffer.GetID() == MsgType.RequestFile) // Client requests file from server
                        {
                            bool        NoFile = true;
                            RequestFile RF     = buffer.GetObject <RequestFile>();
                            Console.WriteLine(client.socket.RemoteEndPoint.ToString() + ": Request File -> " + RF.Name);
                            buffer.innerbuffer.Clear();

                            byte[] binaryfile = new byte[] { };
                            string Path       = Utils.AssemblyDirectory + "\\" + RF.Name + ".scbd";
                            if (File.Exists(Path))
                            {
                                binaryfile = Crypt.Decrypt(File.ReadAllBytes(Path), BinaryFilesKey, BinaryFilesPassword);
                                byte[] passwordbytes          = Crypt.Base64Encode(BinaryFilesPassword); // get bytes of password
                                byte[] binarypasswordfromfile = new byte[passwordbytes.Length];          // allocate bytes
                                try
                                {
                                    // copy length of of current password from binrayfile on disk;
                                    Buffer.BlockCopy(binaryfile, 0, binarypasswordfromfile, 0, binarypasswordfromfile.Length);
                                    string DecodedBinaryPassword = Crypt.Base64Decode(binarypasswordfromfile); // decode password
                                    if (DecodedBinaryPassword == BinaryFilesPassword)                          // password from file is the same as current password;
                                    {
                                        Buffer.BlockCopy(binaryfile, passwordbytes.Length, binaryfile, 0, binaryfile.Length - passwordbytes.Length);
                                    }
                                    else // password was longer/smaller or same length but different chars.
                                    {
                                        Console.WriteLine("Encrypted password does not match current password, file could not be send to user : "******"Failed to Decrypt file from disk");
                                    Console.WriteLine(ex.Message + "/n/r " + ex.StackTrace);
                                }
                                NoFile = false;

                                PrepareFileSend PFS = new PrepareFileSend();
                                PFS.filesize = binaryfile.Length;
                                PFS.FileName = RF.Name;
                                PFS.ACK      = 0;
                                buffer.AddID(MsgType.SendFile);
                                buffer.AddObject(PFS);
                                buffer.SendCryptoBuffer(client.socket, client.CC.Salt);
                                buffer.innerbuffer.Clear();
                                buffer.CryptoSize = 0;

                                buffer.ReceiveCryptoBuffer(client.socket, client.CC.Salt);
                                if (buffer.GetID() == MsgType.SendOK)
                                {
                                    buffer.innerbuffer.Clear();
                                    ServerFile serverfile = new ServerFile();
                                    string[]   splitname  = RF.Name.Split('.');
                                    serverfile.Name       = splitname[0]; // name
                                    serverfile.Ext        = splitname[1]; // extension
                                    serverfile.BinaryFile = binaryfile;   // decrypt file to send it.
                                    binaryfile            = null;
                                    buffer.AddObject(serverfile);
                                    Console.WriteLine("Server : " + buffer.SendCryptoBuffer(client.socket, client.CC.Salt) + " bytess  of " + RF.Name + " send!");
                                    buffer.innerbuffer.Clear();
                                }
                            }
                            if (NoFile)
                            {
                                PrepareFileSend PFS = new PrepareFileSend();
                                PFS.filesize = -1;
                                PFS.FileName = RF.Name;
                                PFS.ACK      = 0;
                                buffer.AddID(MsgType.Nofile);
                                buffer.AddObject(PFS);
                                buffer.SendCryptoBuffer(client.socket, client.CC.Salt);
                                buffer.innerbuffer.Clear();
                                Thread.Sleep(10);
                            }
                        }
                        #endregion
                        #region Reguest Logo
                        if (buffer.GetID() == MsgType.RequestLogo)
                        {
                            if (Logo == null)
                            {
                                buffer.innerbuffer.Clear();
                                buffer.AddID(MsgType.NoLogo);
                                buffer.SendCryptoBuffer(client.socket, client.CC.Salt);
                            }
                            else
                            {
                                buffer.innerbuffer.Clear();
                                buffer.AddID(MsgType.Logo);

                                buffer.AddObject(Logo);
                                buffer.SendCryptoBuffer(client.socket, client.CC.Salt);
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        for (int i = 0; i < ConnectedClients.Count(); i++)
                        {
                            if (ConnectedClients[i].socket == client.socket)
                            {
                                if (client.CC.Ip == ConnectedClients[i].CC.Ip && client.CC.sockettype != ConnectionType.Client) // or delete if FileSocket.
                                {
                                    ConnectedClients[i].socket.Shutdown(SocketShutdown.Both);
                                    ConnectedClients[i].socket.Close(1);
                                    ConnectedClients.RemoveAt(i);
                                    Console.WriteLine("Socket of Type {0} Disconnected!", client.CC.sockettype);
                                    bRecvMessage = false;
                                    return;
                                }

                                client.socket.Shutdown(SocketShutdown.Both);
                                client.socket.Close(1);
                                client.socket = null;
                                client.Online = false;
                                bRecvMessage  = false;

                                Console.WriteLine("Client {0} of Type {1} Disconnected!", client.CC.Ip, client.CC.sockettype);
                                UpdateClientList();
                                return;
                            }
                        }
                        //  if (ConnectedClients[index].CC.sockettype != ConnectionType.File) // only update clientlist if socket is not a filesocket.
                        //       UpdateClientList();
                    }
                }
                catch (Exception e)
                {
                    for (int i = 0; i < ConnectedClients.Count(); i++)
                    {
                        if (client.socket == ConnectedClients[i].socket)
                        {
                            Console.WriteLine(ConnectedClients[i].CC.Ip + "   " + e.Message + "\r\n Disconnected");
                            if (i < ConnectedClients.Count())
                            {
                                /*
                                 * ConnectedClients[i].Online = false;
                                 * ConnectedClients[i].socket = null;
                                 * ConnectedClients[i].IsRunning = false;
                                 */
                                client.socket.Shutdown(SocketShutdown.Both);
                                client.socket.Close(1);
                                client.socket = null;
                                client.Online = false;
                                bRecvMessage  = false;
                            }
                        }
                    }
                }
            }
        }