Пример #1
0
    public static void Main()
    {
        try
        {
            db = new Database("127.0.0.1", "johndog", "root", "");
            StartServer();
            // use local m/c IP address, and
            // use the same in the client

            /* Initializes the Listener */


            /* Start Listeneting at the specified port */

            JohnDog.SayNoNewLine("Connection Manager", "The server is running at port 8000.");
            JohnDog.Say("Connection Manager", "The local IP address is: " +
                        server.LocalEndpoint);
            JohnDog.Say("Connection Manger", "Waiting for a connection...");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.StackTrace);
            Console.ReadKey();
        }
    }
Пример #2
0
    private static void HandleVersionCheck(object joe)
    {
        Thread.Sleep(25);
        int joeNo = (int)joe;

        JohnDog.Say("Client Manager", "Client is requesting version [ID: " + joeNo + "] - sent: " + "VERSION " + version);
    }
Пример #3
0
    public static void Message(string username, string username1, string message)
    {
        UTF8Encoding asen = new UTF8Encoding();

        if (db.CheckAccountInUse(username) && db.CheckAccountInUse(username1))
        {
            if (username1 == username)
            {
                clientNames[username].Send(asen.GetBytes("MESSAGE:SELF"));
            }
            clientNames[username].Send(asen.GetBytes("MESSAGESENT " + username1 + " " + message));
            clientNames[username1].Send(asen.GetBytes("MESSAGE " + username + " " + message));
        }
        else
        {
            JohnDog.Say("Message Manager", "The target username is offline.");
            clientNames[username].Send(asen.GetBytes("FAILURE:MSG"));
            return;
        }
    }
Пример #4
0
    private static void HandleClientComm(object client)
    {
        int    bNo               = 0;
        string username          = "";
        bool   isCheckingVersion = false;
        Thread versionCheck      = new Thread(new ParameterizedThreadStart(HandleVersionCheck));
        bool   john              = true;
        int    joe               = clientNo;
        Socket s = (Socket)client;

        JohnDog.Say("Client Manager", "Client received [ID: " + joe + "] - " + clients[joe].RemoteEndPoint);
        while (john)
        {
            UTF8Encoding asen = new UTF8Encoding();
            byte[]       b    = new byte[100];
            int          k    = 0;
            try { k = s.Receive(b); }
            catch
            {
                john = false;
                JohnDog.Say("Client Manager", "Client disconnected [ID: " + joe + "] - " + clients[joe].RemoteEndPoint);
                clientNames.Remove(username);
                try { db.UnlockAccount(username); }
                catch { }
                return;
            }
            string action = "";
            string thing  = "";
            for (int i = 0; i < k; i++)
            {
                // Convert received data to string after printing
                thing += Convert.ToChar(b[i]);
                if (thing == "LOGIN REQUEST")
                {
                    action = "LOGIN REQUEST";
                }
                else if (thing.ToUpper() == "REDEEM")
                {
                    action = "REDEEM";
                }
                else if (thing == "REGISTER REQUEST")
                {
                    action = "REGISTER REQUEST";
                }
                else if (thing == "VERSION CHECK")
                {
                    action = "VERSION CHECK";
                }
                else if (thing.ToUpper() == "MESSAGE")
                {
                    action = "MESSAGE";
                }
                else if (thing.ToUpper() == "INVENTORY ")
                {
                    action = "INVENTORY";
                }
            }
            JohnDog.Say("Command Manager", "Command: " + action.ToUpper());
            latestcommand = thing;
            if (action == "LOGIN REQUEST")
            {
                string[] cmds = ConvertToCMDs(thing);
                if (db.Login(cmds[2], cmds[3]) && db.CheckAccountInUse(cmds[2]))
                {
                    JohnDog.Say("Database", "Account in use!");
                    s.Send(asen.GetBytes("FAILURE:INUSE"));
                }
                else if (db.Login(cmds[2], cmds[3]))
                {
                    clientNames.Add(cmds[2], s);
                    username = cmds[2];
                    db.LockAccount(cmds[2]);
                    s.Send(asen.GetBytes("SUCCESS:LOGIN"));
                }
                else
                {
                    s.Send(asen.GetBytes("FAILURE:LOGIN"));
                }
            }
            else if (action == "REGISTER REQUEST")
            {
                string[] cmds = ConvertToCMDs(thing);
                if (db.Register(cmds[2], cmds[3]))
                {
                    JohnDog.Say("Database", "Client registered user " + cmds[2] + " [ID: " + joe + "] - " + clients[joe].RemoteEndPoint);
                    s.Send(asen.GetBytes("SUCCESS:REGISTER"));
                }
                else
                {
                    s.Send(asen.GetBytes("FAILURE:REGISTER"));
                }
            }
            else if (action == "MESSAGE")
            {
                string[] cmds     = ConvertToCMDs(thing);
                string   Messager = String.Empty;
                for (int i = 2; i < cmds.Length; i++)
                {
                    Messager += " " + cmds[i];
                }
                Message(username, cmds[1], Messager);
            }
            else if (action == "VERSION CHECK")
            {
                HandleVersionCheck(joe);
                s.Send(asen.GetBytes("VERSION IS " + version));
            }
            else if (action.ToUpper() == "REDEEM")
            {
                string[] cmds     = ConvertToCMDs(thing);
                string   contents = String.Empty;
                try
                {
                    if (db.CheckCodeExists(cmds[1]))
                    {
                        db.SetCodeRedeemed(cmds[1], username);
                        JohnDog.Say("Database", "Successful redeem");
                        contents = db.GetGiftCodeContents(cmds[1]);
                        s.Send(asen.GetBytes("REDEEM:SUCCESS " + cmds[1] + " " + contents));
                    }
                    else
                    {
                        JohnDog.Say("Database", "Failed redeem");
                        s.Send(asen.GetBytes("REDEEM:FAILURE " + cmds[1]));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex, Color.Red);
                    s.Send(asen.GetBytes("REDEEM:NOCODE"));
                }
            }
            else if (action.ToUpper() == "INVENTORY")
            {
                string[] cmds = ConvertToCMDs(thing);
                if (cmds[1] == "LIST")
                {
                    string equips = db.GetEquipment(username);
                    string inv    = db.GetInventory(username);
                    if (equips == null)
                    {
                        JohnDog.Say("Inventory List", "equips null??");
                    }
                    if (inv == null)
                    {
                        JohnDog.Say("Inventory List", "inv null??");
                    }
                    JohnDog.Say("Inventory List", "Sending - INVENTORY:LIST " + equips + " " + inv);
                    s.Send(asen.GetBytes("INVENTORY:LIST " + equips + " " + inv));
                }
                else if (cmds[1] == "DROP")
                {
                    JohnDog.Say("Debug Manager", "Drop function accessed.");

                    string[] o = ConvertToCMDs(db.GetEquipment(username));
                    string[] p = ConvertToCMDs(db.GetInventory(username));
                    string[] a = ConvertToCMDs(db.GetEquipment(username) + " " + db.GetInventory(username));

                    // Inv+Equips List
                    List <int> c = new List <int>();

                    // Equips List
                    List <int> d = new List <int>();

                    // Inv List
                    List <int> e = new List <int>();

                    for (int i = 0; i < a.Length; i++)
                    {
                        c.Add(Convert.ToInt32(a[i]));
                    }

                    for (int i = 0; i < o.Length; i++)
                    {
                        d.Add(Convert.ToInt32(o[i]));
                    }

                    for (int i = 0; i < p.Length; i++)
                    {
                        e.Add(Convert.ToInt32(p[i]));
                    }

                    int DropInt;
                    if (Int32.TryParse(cmds[2], out DropInt))
                    {
                        if (c[DropInt - 1] != -1)
                        {
                            c[DropInt - 1] = -1;
                            if (DropInt - 1 <= 3)
                            {
                                d[DropInt - 1] = -1;
                                string equipmentnew = String.Join(" ", d.ToArray());
                                JohnDog.Say("Debug Manager", "Sending equipment: " + equipmentnew);
                                db.SetEquipment(username, equipmentnew);
                                s.Send(asen.GetBytes("DROPSUCCESS"));
                            }
                            if (DropInt - 1 <= 11)
                            {
                                e[DropInt - 1] = -1;
                                string inventorynew = String.Join(" ", e.ToArray());
                                JohnDog.Say("Debug Manager", "Sending inventory: " + inventorynew);
                                db.SetEquipment(username, inventorynew);
                                s.Send(asen.GetBytes("DROPSUCCESS"));
                            }
                        }
                        else
                        {
                            s.Send(asen.GetBytes("DROPFAIL:C2"));
                        }
                    }
                    else
                    {
                        JohnDog.Say("Error", "Dropfail:C1");
                        s.Send(asen.GetBytes("DROPFAIL:C1"));
                    }
                }
                else if (cmds[1] == "EQUIP")
                {
                }
            }
            if (isCheckingVersion)
            {
                s.Send(asen.GetBytes("VERSION " + version));
            }
            if (latestcommand == "")
            {
                if (bNo >= 5)
                {
                    JohnDog.Say("Client Manager", "Client sent too many empty bytes [ID: " + joe + "] - " + clients[joe].RemoteEndPoint + ". Client disconnected.");
                    john = false;
                    clientNames.Remove(username);
                    try { db.UnlockAccount(username); }
                    catch { }
                }
                else
                {
                    bNo++;
                    JohnDog.Say("Client Manager", "Client sent empty byte [ID: " + joe + "] - " + clients[joe].RemoteEndPoint);
                }
            }
            else
            {
                JohnDog.Say("Client Manager", "Client command received [ID: " + joe + "] - " + latestcommand);
            }
        }
    }