Beispiel #1
0
        public bool Update()
        {
            try
            {
                ServiceSocket.Listen(256);  // Block on Listen() Until a New Client Connects.
            }

            catch (SocketException e)
            {
                Console.WriteLine("Listen Failure: " + e.Message);

                return(false);
            }

            Socket NewClient;

            try
            {
                NewClient = ServiceSocket.Accept();

                IPEndPoint ClientEndPoint = ((IPEndPoint)NewClient.RemoteEndPoint);
                Console.WriteLine("Client Connected At: " + ClientEndPoint.Address + ':' + ClientEndPoint.Port);

                Clients.Add(NewClient);
            }

            catch (SocketException e)
            {
                Console.WriteLine("Accept Failure: " + e.Message);

                return(false);
            }

            SocketTrackedAsyncArgs AsyncArgs = new SocketTrackedAsyncArgs();

            AsyncArgs.Client = NewClient;
            byte[] Buffer = new byte[1024];
            AsyncArgs.SetBuffer(Buffer, 0, Buffer.Length);
            AsyncArgs.Completed += Process;
            NewClient.ReceiveAsync(AsyncArgs);

            return(true);
        }
Beispiel #2
0
        private void Process(object Sender, SocketAsyncEventArgs Args)
        {
            SocketTrackedAsyncArgs TrackedArgs    = (SocketTrackedAsyncArgs)Args;
            IPEndPoint             ClientEndPoint = ((IPEndPoint)TrackedArgs.Client.RemoteEndPoint);

            string ReceivedString = Encoding.ASCII.GetString(Args.Buffer);

            ReceivedString = ReceivedString.Replace("\0", String.Empty);

            // Don't Process Disconnect Messages.
            if (ReceivedString == "")
            {
                return;
            }

            if (ReceivedString == "GETTOP10SCORES")
            {
                List <string> Entries = GetHighestScores(10);

                byte[] EntriesBuffer = new byte[256];

                int Offset = 0;

                for (int Iter = 0; Iter < Entries.Count; ++Iter)
                {
                    byte[] EntryBuffer = Encoding.ASCII.GetBytes(Entries[Iter]);

                    System.Buffer.BlockCopy(EntryBuffer, 0, EntriesBuffer, Offset, EntryBuffer.Length);

                    Offset += EntryBuffer.Length;
                }

                Console.WriteLine("Client Requested Highscores At: " + ClientEndPoint.Address + ':' + ClientEndPoint.Port);

                TrackedArgs.Client.Send(EntriesBuffer);
            }

            else if (ReceivedString.Substring(0, 5) != "NAME:")
            {
                Console.WriteLine("Received Malformed Data");
            }

            else
            {
                string NameSearch = ReceivedString.Substring(5);
                int    Position   = 0;

                for (int Iter = 0; Iter < NameSearch.Length; ++Iter)
                {
                    if (NameSearch[Iter] == '|')
                    {
                        Position = Iter;
                    }
                }

                if (Position == 0)
                {
                    Console.WriteLine("Received Malformed Data");
                }

                else
                {
                    string Name = NameSearch.Substring(0, Position);

                    int Score = Convert.ToInt32(NameSearch.Substring(Position + 7));
                    if (Score != 0)
                    {
                        Console.WriteLine("Client Submitted Score At: " + ClientEndPoint.Address + ':' + ClientEndPoint.Port);

                        string Entry = Name + '|' + Score + "\n";

                        bool AquiredLock = new bool();
                        Lock.Enter(ref AquiredLock);

                        long EOF = ScoreDatabase.Length;
                        ScoreDatabase.Seek(EOF, SeekOrigin.Begin);
                        ScoreWriter.WriteLine(Entry);
                        ScoreWriter.Flush();
                        //ScoreDatabase.Flush();

                        Lock.Exit();

                        NewData = true;
                    }
                }
            }

            // Reset Receiving on this Socket.
            SocketTrackedAsyncArgs AsyncArgs = new SocketTrackedAsyncArgs();

            AsyncArgs.Client = TrackedArgs.Client;
            byte[] Buffer = new byte[1024];
            AsyncArgs.SetBuffer(Buffer, 0, Buffer.Length);
            AsyncArgs.Completed += Process;
            TrackedArgs.Client.ReceiveAsync(AsyncArgs);
        }