예제 #1
0
        /// <summary>
        /// sents message to specific client
        /// </summary>
        /// <param name="cl"></param>
        /// <param name="code"></param>
        /// <param name="message"></param>
        private void SendToClient(LClient cl, EMessageCode code, string message)
        {
            try
            {
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes((((char)code) + "|Admin|" + message + "|\n").ToCharArray());

                buffer = this.EncrptyDecrypt(buffer);

                cl.MySocket.Send(buffer, buffer.Length, 0);
            }
            catch (Exception e)
            {
                this.RemoveListBox(this.listBoxUsers, cl.LogData());
                cl.Shutdown();
                this.clientsDict.Remove(cl.UserName);
            }
        }
예제 #2
0
        /// <summary>
        /// recieves messages and processes
        /// </summary>
        /// <param name="client"></param>
        private void Recieve(Socket client)
        {
            bool isLooping = true;
            while (isLooping)
            {
                try
                {
                    Byte[] buffer = new Byte[2048];
                    client.Receive(buffer);

                    //decrypt file
                    buffer = this.EncrptyDecrypt(buffer);

                    string message = System.Text.Encoding.ASCII.GetString(buffer);

                    string[] tokens = message.Split(new Char[] { '|' });

                    //pulls code from recieved message
                    EMessageCode code = (EMessageCode)tokens[0][0];

                    switch (code)
                    {
                        case EMessageCode.None:
                            {
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.HandShake:
                            {
                                this.LogMessage(tokens);
                                //checks credentials
                                if(this.CheckCredentials(tokens[1], tokens[2]))
                                {
                                    //checks if the client name isn't already in use.
                                    if(!this.clientsDict.ContainsKey(tokens[1]))
                                    {
                                        //get endpoint for creating an LClient
                                        EndPoint ep = client.RemoteEndPoint;
                                        LClient c = new LClient(tokens[1],ep,clientService, client);

                                        //add client into dictionary
                                        this.clientsDict.Add(c.UserName, c);

                                        //message client a handshake
                                        this.SendToClient(c, EMessageCode.HandShake, "Very nice");

                                        //add the client data to the list box.
                                        this.AddListBox(this.listBoxUsers, c.LogData());

                                        //update log reader
                                        this.UpdateReader(c.UserName + " has Connected.");

                                        //pause for the client to setup before receiving next message.
                                        System.Threading.Thread.Sleep(250);

                                        //send messages to all connected clients that a new client is connected.
                                        //also sends a message to the client that connected with all connected clients.
                                        foreach(KeyValuePair<string,LClient> entry in this.clientsDict)
                                        {
                                            this.SendToClient(entry.Value, EMessageCode.UserConnected, c.UserName);
                                            this.SendToClient(c, EMessageCode.UsersOnline, entry.Value.UserName);
                                            System.Threading.Thread.Sleep(50);
                                        }
                                    }
                                    else
                                    {
                                        //mulitple users logged in with the name. rejects connection.
                                        string doubleTemp = "DOUBLE ACCESS:\nAttempting access: " + tokens[1] + "Current Logged in User: "******"Attempting Access: " + client.RemoteEndPoint.ToString();
                                        //log warning added
                                        this.UpdateReader(doubleTemp);
                                        byte[] bufferUser = System.Text.Encoding.ASCII.GetBytes((((byte)EMessageCode.InvalidUsername) + "|Admin|User name in use.").ToCharArray());
                                        client.Send(bufferUser, bufferUser.Length, 0);
                                    }
                                }
                                else
                                {
                                    //user tried to log in with invalid password. rejects connection obviously.
                                    string passTemp = "INVALID PASSWORD: \n" + "User: "******"IP: " + client.RemoteEndPoint.ToString();
                                    //log warning added
                                    this.UpdateReader(passTemp);
                                    byte[] bufferPass = System.Text.Encoding.ASCII.GetBytes((((byte)EMessageCode.InvalidPassword) + "|Admin|Invalid password.").ToCharArray());
                                    client.Send(bufferPass, bufferPass.Length, 0);
                                }
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.GreatSuccess:
                            {
                                this.LogMessage(tokens);
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.MessageAll:
                            {
                                this.LogMessage(tokens);
                                string m = "";

                                for(int i = 2; i < tokens.Length - 1; i++)
                                {
                                    m += tokens[i] + "|";
                                }

                                this.RelayToAllClients(EMessageCode.MessageAll, tokens[1], m);

                                isLooping = false;
                            }
                            break;
                        case EMessageCode.MessageWhisper:
                            {
                                this.LogMessage(tokens);
                                string m = "";

                                for (int i = 2; i < tokens.Length - 1; i++)
                                {
                                    m += tokens[i] + "|";
                                }

                                if (this.clientsDict.ContainsKey(tokens[2]))
                                {
                                    this.RelayToClient(this.clientsDict[tokens[2]], EMessageCode.MessageWhisper, tokens[1], m);
                                }
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.MessageFile:
                            {
                                this.LogMessage(tokens);
                                this.RelayFile(this.clientsDict[tokens[1]], this.clientsDict[tokens[2]], Convert.ToInt64(tokens[3]), tokens[4]);
                                //the below method was a method that would recieve files automatically from the client when sent
                                //this.RecieveFile(this.clientsDict[tokens[1]], this.clientsDict[tokens[2]], Convert.ToInt64(tokens[3]), tokens[4]);
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.UserDisconnect:
                            {
                                this.LogMessage(tokens);
                                this.RelayToAllClients(EMessageCode.UserDisconnect, tokens[1], "bye bye\n");
                                this.UpdateReader(tokens[1] + " has left the server.");
                                if (this.clientsDict.ContainsKey(tokens[1]))
                                {
                                    this.RemoveListBox(this.listBoxUsers, this.clientsDict[tokens[1]].LogData());
                                    this.clientsDict[tokens[1]].Shutdown();
                                    this.clientsDict.Remove(tokens[1]);
                                }
                                isLooping = false;
                            }
                            break;
                        case EMessageCode.UserConnected:
                            {
                                this.LogMessage(tokens);
                                this.UpdateReader(tokens[1] + " has joined the server.");
                                isLooping = false;
                            }
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    isLooping = false;
                }
            }
        }