예제 #1
0
 public void AuthModeReceive(FinalMessageForClient p_message)
 {
     if (p_message.Get_message_type == TypeOfMessage.Dialog)
     {
         DialogMessageForClient t_dialog_message = null;
         try
         {
             t_dialog_message = ((DialogMessageForClient)p_message.Get_message_object);
         }
         catch (Exception)
         {
             return;
         }
         all_dialogs.Receive(t_dialog_message);
     }
     else if (p_message.Get_message_type == TypeOfMessage.Chat)
     {
         ChatMessageForClient t_chat_message = null;
         try
         {
             t_chat_message = ((ChatMessageForClient)p_message.Get_message_object);
         }
         catch (Exception)
         {
             return;
         }
         all_chats.Receive(t_chat_message);
     }
 }
예제 #2
0
 public abstract void Receive(FinalMessageForClient p_message);
예제 #3
0
        public static void WorkerMainThread(ClientWorkerData p_worker_data, IPEndPoint p_server_ip_endpoint)
        {
            ClientWorkerData worker_data = p_worker_data;

            Dictionary <int, Thread> all_mini_threads = new Dictionary <int, Thread>();
            object all_mini_threads_lock = new object();

            ClientWorkerStatus status = ClientWorkerStatus.Connecting;

            System.Timers.Timer worker_quick_check_timer_object = new System.Timers.Timer();
            worker_quick_check_timer_object.Interval  = 13500;
            worker_quick_check_timer_object.AutoReset = false;

            worker_quick_check_timer_object.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) => worker_timer_Elapsed(sender
                                                                                                                                 , e, worker_data.send_to_worker_construct
                                                                                                                                 , worker_data.client_worker_pulse_object));
            ClientWorkerTimer timer = new ClientWorkerTimer(worker_quick_check_timer_object);

            Socket server_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            int    k             = 0;

            while (k < 4)
            {
                try
                {
                    server_socket.Connect(p_server_ip_endpoint);
                    k = 0;
                    break;
                }
                catch (Exception)
                {
                    k++;
                    Thread.Sleep(200);
                    continue;
                }
            }
            if (k > 3)
            {
                MessageFromWorker signal_reject_message = new MessageFromWorker(new ClientWorkerSignal(false), TypeOfMessageFromWorker.SignalMessage);
                AddNewTransferMiniThread(signal_reject_message, worker_data.receive_from_worker_construct, worker_data.message_received_inform
                                         , worker_data.main_form_invoke, all_mini_threads, all_mini_threads_lock);

                try
                {
                    server_socket.Shutdown(SocketShutdown.Both);
                    server_socket.Close();
                }
                catch
                {
                }
                Thread.CurrentThread.Abort();
                return;
            }

            MessageFromWorker signal_accept_message = new MessageFromWorker(new ClientWorkerSignal(true), TypeOfMessageFromWorker.SignalMessage);

            AddNewTransferMiniThread(signal_accept_message, worker_data.receive_from_worker_construct, worker_data.message_received_inform
                                     , worker_data.main_form_invoke, all_mini_threads, all_mini_threads_lock);

            int  last_message_sent_id = 0;
            bool cancelled            = false;
            bool user_cancelled       = false;

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Context        = new StreamingContext(StreamingContextStates.All);
            formatter.TypeFormat     = FormatterTypeStyle.TypesWhenNeeded;
            formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

            SerializationBinder serialization_binder = formatter.Binder;
            MemoryStream        stream = new MemoryStream();

            byte[] data_buffer = new byte[1];

            status = ClientWorkerStatus.Satrting;

            Queue <byte[]> temp_receive_thread_queue = new Queue <byte[]>();
            WorkerReceiveThreadConstruct receive_thread_construct = new WorkerReceiveThreadConstruct(temp_receive_thread_queue);

            Thread receiver_thread = new Thread(() => ReceiveThread(receive_thread_construct, ref server_socket
                                                                    , ref timer, worker_data.client_worker_pulse_object));

            Queue <KeyValuePair <TypeOfSend, byte[]> > send_thread_queue = new Queue <KeyValuePair <TypeOfSend, byte[]> >();
            bool   send_thread_queue_flag            = false;
            bool   send_thread_cancel_flag           = false;
            object send_thread_pulse_object          = new object();
            bool   send_thread_receipt_received_flag = false;

            Thread send_thread = new Thread(() => SendThread(send_thread_queue, ref send_thread_queue_flag, ref send_thread_cancel_flag
                                                             , ref send_thread_receipt_received_flag, send_thread_pulse_object, worker_data.client_worker_pulse_object, receive_thread_construct, server_socket, timer));

            receiver_thread.Start();
            send_thread.Start();
            timer.StartAndReset();

            status = ClientWorkerStatus.Normal;



            while (true)
            {
                lock (worker_data.client_worker_pulse_object)
                {
                    if (!worker_data.cancel_construct.cancel_construct_flag && !receive_thread_construct.receive_thread_queue_flag && !worker_data.send_to_worker_construct.send_to_worker_queue_flag)
                    {
                        Monitor.Wait(worker_data.client_worker_pulse_object);
                    }
                    if (worker_data.cancel_construct.cancel_construct_flag)
                    {
                        timer.PrimerStop();
                        user_cancelled = true;
                        cancelled      = true;
                        break;
                    }
                    if (receive_thread_construct.receive_thread_queue_flag)
                    {
                        if (receive_thread_construct.receive_thread_queue.Count > 0)
                        {
                            byte[] byte_received_data = receive_thread_construct.receive_thread_queue.Dequeue();
                            if (receive_thread_construct.receive_thread_queue.Count == 0)
                            {
                                receive_thread_construct.receive_thread_queue_flag = false;
                            }
                            if (byte_received_data.Length == 0)
                            {
                                timer.PrimerStop();

                                MessageFromWorker cancel_message_from_worker = new MessageFromWorker(null, TypeOfMessageFromWorker.OfflineInform);
                                AddNewTransferMiniThread(cancel_message_from_worker, worker_data.receive_from_worker_construct, worker_data.message_received_inform
                                                         , worker_data.main_form_invoke, all_mini_threads, all_mini_threads_lock);
                                cancelled = true;
                                break;
                            }
                            else
                            {
                                stream = new MemoryStream(byte_received_data);
                                DataForSend data_received = null;
                                try
                                {
                                    data_received = (DataForSend)formatter.Deserialize(stream);
                                }
                                catch
                                {
                                    stream = new MemoryStream();
                                    continue;
                                }
                                stream = new MemoryStream();
                                if (data_received.Get_data_for_send_type == TypeOfDataForSend.QuickAnswer && status == ClientWorkerStatus.WaitForAReceipt)
                                {
                                    QuickAnswer received_quick_answer = null;
                                    try
                                    {
                                        received_quick_answer = ((QuickAnswer)data_received.Get_message_object);
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                    if (received_quick_answer.Get_id == last_message_sent_id)
                                    {
                                        lock (send_thread_pulse_object)
                                        {
                                            send_thread_receipt_received_flag = true;
                                            Monitor.Pulse(send_thread_pulse_object);
                                            status = ClientWorkerStatus.Normal;
                                        }
                                    }
                                }
                                else if (data_received.Get_data_for_send_type == TypeOfDataForSend.QuickCheck)
                                {
                                    QuickCheck received_quick_check = null;
                                    try
                                    {
                                        received_quick_check = ((QuickCheck)data_received.Get_message_object);
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                    QuickAnswer quick_answer          = new QuickAnswer(received_quick_check.Get_id);
                                    DataForSend quick_answer_for_send = new DataForSend(TypeOfDataForSend.QuickAnswer, quick_answer);
                                    formatter.Serialize(stream, quick_answer_for_send);
                                    byte[] byte_quick_answer = stream.GetBuffer();
                                    stream = new MemoryStream();
                                    lock (send_thread_pulse_object)
                                    {
                                        send_thread_queue.Enqueue(new KeyValuePair <TypeOfSend, byte[]>(TypeOfSend.WithoutReceipt, byte_quick_answer));
                                        send_thread_queue_flag = true;
                                        Monitor.Pulse(send_thread_pulse_object);
                                    }
                                }
                                else if (data_received.Get_data_for_send_type == TypeOfDataForSend.UserMessage)
                                {
                                    UserMessageToClient user_message = null;
                                    try
                                    {
                                        user_message = ((UserMessageToClient)data_received.Get_message_object);
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }

                                    FinalMessageForClient client_object = null;
                                    try
                                    {
                                        client_object = new FinalMessageForClient(user_message.Get_user_message.Get_message_type, user_message.Get_user_message.Get_message_object);
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }

                                    MessageFromWorker final_message_received = new MessageFromWorker(client_object, TypeOfMessageFromWorker.FinalMessage);
                                    AddNewTransferMiniThread(final_message_received, worker_data.receive_from_worker_construct, worker_data.message_received_inform
                                                             , worker_data.main_form_invoke, all_mini_threads, all_mini_threads_lock);

                                    QuickAnswer user_message_answer  = new QuickAnswer(user_message.Get_id);
                                    DataForSend user_answer_for_send = new DataForSend(TypeOfDataForSend.QuickAnswer, user_message_answer);
                                    formatter.Serialize(stream, user_answer_for_send);
                                    lock (send_thread_pulse_object)
                                    {
                                        send_thread_queue.Enqueue(new KeyValuePair <TypeOfSend, byte[]>(TypeOfSend.WithoutReceipt, stream.GetBuffer()));
                                        send_thread_queue_flag = true;
                                        Monitor.Pulse(send_thread_pulse_object);
                                    }
                                    stream = new MemoryStream();
                                }
                            }
                        }
                        else
                        {
                            receive_thread_construct.receive_thread_queue_flag = false;
                        }
                    }
                    if (worker_data.send_to_worker_construct.send_to_worker_queue_flag)
                    {
                        if (status == ClientWorkerStatus.Normal)
                        {
                            if (worker_data.send_to_worker_construct.send_to_worker_queue.Count > 0)
                            {
                                MessageToWorker object_to_send = worker_data.send_to_worker_construct.send_to_worker_queue.Dequeue();
                                if (worker_data.send_to_worker_construct.send_to_worker_queue.Count == 0)
                                {
                                    worker_data.send_to_worker_construct.send_to_worker_queue_flag = false;
                                }
                                if (object_to_send.Get_type_of_message_to_worker_object == TypeOfMessageToWorker.QuickCheckRequest)
                                {
                                    QuickCheck  quick_check_to_send = new QuickCheck(HelperFunctions.GetGUID());
                                    DataForSend client_data         = new DataForSend(TypeOfDataForSend.QuickCheck, quick_check_to_send);
                                    formatter.Serialize(stream, client_data);
                                    byte[] byte_client_data = stream.GetBuffer();
                                    lock (send_thread_pulse_object)
                                    {
                                        send_thread_queue.Enqueue(new KeyValuePair <TypeOfSend, byte[]>(TypeOfSend.WithReceipt, byte_client_data));
                                        send_thread_queue_flag = true;
                                        Monitor.Pulse(send_thread_pulse_object);
                                    }
                                    last_message_sent_id = quick_check_to_send.Get_id;
                                    status = ClientWorkerStatus.WaitForAReceipt;
                                    stream = new MemoryStream();
                                }
                                else if (object_to_send.Get_type_of_message_to_worker_object == TypeOfMessageToWorker.FinalMessage)
                                {
                                    FinalMessageForServer final_message        = (FinalMessageForServer)object_to_send.Get_message_for_worker_object;
                                    UserMessageToServer   user_message_to_send = new UserMessageToServer(HelperFunctions.GetGUID(), final_message);
                                    DataForSend           client_data          = new DataForSend(TypeOfDataForSend.UserMessage, user_message_to_send);
                                    formatter.Serialize(stream, client_data);
                                    byte[] byte_client_data = stream.GetBuffer();
                                    lock (send_thread_pulse_object)
                                    {
                                        send_thread_queue.Enqueue(new KeyValuePair <TypeOfSend, byte[]>(TypeOfSend.WithReceipt, byte_client_data));
                                        send_thread_queue_flag = true;
                                        Monitor.Pulse(send_thread_pulse_object);
                                    }
                                    last_message_sent_id = user_message_to_send.Get_id;
                                    status = ClientWorkerStatus.WaitForAReceipt;
                                    stream = new MemoryStream();
                                }
                            }
                            else
                            {
                                worker_data.send_to_worker_construct.send_to_worker_queue_flag = false;
                            }
                        }
                    }
                }
            }
            if (cancelled)
            {
                timer.PrimerStop();

                if (!user_cancelled)
                {
                    MessageFromWorker offline_inform_message = new MessageFromWorker(null, TypeOfMessageFromWorker.OfflineInform);
                    AddNewTransferMiniThread(offline_inform_message, worker_data.receive_from_worker_construct, worker_data.message_received_inform
                                             , worker_data.main_form_invoke, all_mini_threads, all_mini_threads_lock);
                }

                try
                {
                    receiver_thread.Abort();
                }
                catch
                {
                }
                try
                {
                    server_socket.Shutdown(SocketShutdown.Both);
                    server_socket.Close();
                }
                catch
                {
                }
                receiver_thread.Join();

                lock (send_thread_pulse_object)
                {
                    send_thread_cancel_flag = true;
                    Monitor.Pulse(send_thread_pulse_object);
                }
                send_thread.Join();
                Thread.CurrentThread.Abort();
                return;
            }
        }
예제 #4
0
        //
        public void MessageReceivedInform()
        {
            bool flag = false;
            MessageFromWorker received_message = null;

            lock (receive_from_worker_construct.receive_from_worker_queue_lock)
            {
                if (receive_from_worker_construct.receive_from_worker_queue.Count > 0)
                {
                    received_message = receive_from_worker_construct.receive_from_worker_queue.Dequeue();
                    flag             = true;
                    if (receive_from_worker_construct.receive_from_worker_queue.Count == 0)
                    {
                        receive_from_worker_construct.receive_from_worker_queue_flag = false;
                    }
                }
                else
                {
                    receive_from_worker_construct.receive_from_worker_queue_flag = false;
                }
            }
            if (flag == true)
            {
                bool message_investigate = HelperFunctions.MessageFromWorkerInvestigate(received_message);
                if (message_investigate == false)
                {
                    MessageBox.Show("Invalid Message Object Rejected.");
                    return;
                }
                else if (message_investigate == true)
                {
                    if (received_message.Get_type_of_message_from_worker_object == TypeOfMessageFromWorker.SignalMessage && mode == TypeOfMode.TcpAllocation)
                    {
                        TcpModeReceive(((ClientWorkerSignal)received_message.Get_message_from_worker_object));
                        return;
                    }
                    else if (received_message.Get_type_of_message_from_worker_object == TypeOfMessageFromWorker.FinalMessage)
                    {
                        FinalMessageForClient final_message_received = (FinalMessageForClient)received_message.Get_message_from_worker_object;
                        if (mode == TypeOfMode.UnAuthMode && final_message_received.Get_message_type == TypeOfMessage.Dialog)
                        {
                            UnAuthModeReceive((DialogMessageForClient)final_message_received.Get_message_object);
                            return;
                        }
                        else if (mode == TypeOfMode.AuthMode)
                        {
                            AuthModeReceive(final_message_received);
                            return;
                        }
                    }
                    else if (received_message.Get_type_of_message_from_worker_object == TypeOfMessageFromWorker.OfflineInform)
                    {
                        is_there_worker = false;
                        OfflineApplication();
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            return;
        }