Exemplo n.º 1
0
        public void Run()
        {
            Server server = new Server(12000);

            Thread clock = new Thread(ServerUtilities.Clock);

            clock.Start();

            Thread listeningForConnections = new Thread(server.Run);

            listeningForConnections.Start();

            ServerAuction bid = new ServerAuction("Teddy", 10.0, 30, "Fluffy");

            ServerUtilities.AuctionList.Add(bid);

            string json = ServerUtilities.JsonSerialize(bid);

            Console.WriteLine(json);

            Thread auctionThread = new Thread(bid.RunActiveAuction);

            auctionThread.Start();

            Console.ReadLine();
        }
Exemplo n.º 2
0
        private bool Execute()
        {
            string jsonData = ReciveFromClient();

            Debug.WriteLine(jsonData);
            if (jsonData == null)
            {
                return(false);
            }

            CommunicationData recivedData = ServerUtilities.Decode(jsonData);

            switch (recivedData.Action)
            {
            case "ExecuteCommand":
                switch (recivedData.Data.Trim().ToLower())
                {
                case "exit":
                    Close();
                    return(false);

                case "servertime":
                    int serverTime = ServerUtilities.Time;
                    SendToClient(new CommunicationData("OutPutMessage", "Server time: " + serverTime).Encode());
                    break;

                case "updateclientstest":
                    ServerUtilities.AuctionList[0].Notify();
                    break;

                default:
                    SendToClient(new CommunicationData("OutPutMessage", "Invalid command.").Encode());
                    break;
                }
                break;

            case "Bid":
                double x = 0;
                if (double.TryParse(recivedData.Data, out x))
                {
                    ServerUtilities.AuctionList[0].CheckNewBid(x, Name);
                }
                else
                {
                    SendToClient(new CommunicationData("OutPutMessage", "Invalid syntax").Encode());
                }
                break;

            case "SetClientName":
                Name = recivedData.Data;
                SendToClient(new CommunicationData("OutPutMessage", "You are signed in as - " + recivedData.Data + Environment.NewLine + "To exit type: exit").Encode());
                break;

            default:
                Console.WriteLine("Invalid action recived.");
                break;
            }
            return(true);
        }
Exemplo n.º 3
0
 private void SendToClient(string data)
 {
     try
     {
         Writer.WriteLine(data);
         Writer.Flush();
     }
     catch (Exception e)
     {
         ServerUtilities.RemoveClient(Id);
     }
 }
Exemplo n.º 4
0
        public void Close()
        {
            ServerUtilities.RemoveClient(Id);
            Writer.Close();
            Reader.Close();
            NetStream.Close();
            ClientSocket.Shutdown(SocketShutdown.Both);
            ClientSocket.Close();

            Monitor.Enter(Server._object);
            try
            {
                Server.CurrentClients--;
            }
            finally
            {
                Monitor.Exit(Server._object);
            }
        }
Exemplo n.º 5
0
 public string Encode()
 {
     return(ServerUtilities.JsonSerialize(this));
 }