예제 #1
0
        void MessageExecuteThread()
        {
            byte[]        buffer;
            NetworkStream stream = Sockets.client.GetStream();

            while (Sockets.client.Connected)
            {
                if (thLock)
                {
                    continue;
                }

                if (isPlaying)
                {
                    buffer = System.Text.Encoding.ASCII.GetBytes(string.Format("{0};{1}", Convert.ToInt32(MessageTypes.ListRequestClient), nickname));
                    stream.Write(buffer, 0, buffer.Length);
                    isPlaying = false;
                }

                try
                {
                    buffer = new byte[MainActivity.MAX_LENGTH];
                    stream.Read(buffer, 0, buffer.Length);
                    string[] data = MessageParser.Split(buffer);
                    if (data.Length == 1)
                    {
                        Sockets.client.Close();
                        RunOnUiThread(delegate {
                            ad = new AlertDialog.Builder(this).Create();
                            ad.SetCancelable(false); // This blocks the 'BACK' button
                            ad.SetMessage("Nie mo¿na po³¹czyæ siê z serwerem. SprawdŸ po³¹czenie z internetem.");
                            ad.SetButton("OK", delegate
                            {
                                Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                            });
                            ad.Show();
                        });
                    }
                    switch (MessageParser.ToMessageType(data[0]))
                    {
                    case MessageTypes.SendPlayers:
                        ParsePlayerList(data[1]);
                        lv.Post(delegate { lv.Adapter = new PlayerListAdapter(this, this.data); });
                        break;

                    case MessageTypes.InviteClient:
                        if (data[1] == nickname)
                        {
                            break;
                        }
                        RunOnUiThread(delegate
                        {
                            ad = new AlertDialog.Builder(this).Create();
                            ad.SetCancelable(false);     // This blocks the 'BACK' button
                            ad.SetMessage(string.Format("Masz zaproszenie do gry od {0}.", data[1]));
                            ad.SetButton("Odrzuæ", delegate
                            {
                                buffer = System.Text.Encoding.ASCII.GetBytes(string.Format("{0};{1}", Convert.ToInt32(MessageTypes.Decline), data[1]));
                                stream.Write(buffer, 0, buffer.Length);
                                czyOtwartyAlert = false;
                            });
                            ad.SetButton2("Akceptuj", delegate
                            {
                                buffer = System.Text.Encoding.ASCII.GetBytes(string.Format("{0};{1}", Convert.ToInt32(MessageTypes.Accept), data[1]));
                                stream.Write(buffer, 0, buffer.Length);
                                czyOtwartyAlert = false;
                            });
                            ad.Show();
                        });
                        czyOtwartyAlert = true;
                        while (czyOtwartyAlert)
                        {
                            continue;
                        }
                        Thread.Sleep(100);
                        break;

                    case MessageTypes.StartClient:
                        RunOnUiThread(delegate
                        {
                            Intent intent = new Intent(this, typeof(GameActivity));
                            intent.PutExtra("Nickname", nickname);
                            intent.PutExtra("Data", data[1]);
                            StartActivity(intent);
                        });
                        thLock    = true;
                        isPlaying = true;
                        break;

                    case MessageTypes.PlayerIsBusy:
                        RunOnUiThread(delegate {
                            ad = new AlertDialog.Builder(this).Create();
                            ad.SetCancelable(false);                                     // This blocks the 'BACK' button
                            ad.SetMessage(string.Format("Gracz {0} jest zajêty.", data[1]));
                            ad.SetButton("Zamknij", delegate {});
                            ad.Show();
                        });
                        break;
                    }
                }
                catch (System.Threading.ThreadAbortException e)
                {
                    return;
                }
                catch (Exception e)
                {
                    Sockets.client.Close();
                    RunOnUiThread(delegate {
                        ad = new AlertDialog.Builder(this).Create();
                        ad.SetCancelable(false); // This blocks the 'BACK' button
                        ad.SetMessage("Nie mo¿na po³¹czyæ siê z serwerem. SprawdŸ po³¹czenie z internetem.");
                        ad.SetButton("OK", delegate
                        {
                            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                        });
                        ad.Show();
                    });
                }
            }
        }