예제 #1
0
 private static void ReceiveThread(WorkerReceiveThreadConstruct p_recieve_thread_construct, ref Socket p_client_socket
                                   , ref ClientWorkerTimer p_worker_timer, object p_worker_pulse_object)
 {
     byte[] data_received = new byte[1024];
     while (true)
     {
         data_received = ReceiveData(p_client_socket);
         lock (p_worker_pulse_object)
         {
             p_recieve_thread_construct.receive_thread_queue_flag = true;
             p_recieve_thread_construct.receive_thread_queue.Enqueue(data_received);
             p_worker_timer.StartAndReset();
             Monitor.Pulse(p_worker_pulse_object);
             if (data_received.Length == 0)
             {
                 Thread.CurrentThread.Abort();
                 return;
             }
         }
     }
 }
예제 #2
0
        private static void SendThread(Queue <KeyValuePair <TypeOfSend, byte[]> > p_send_thread_queue, ref bool p_send_thread_queue_flag
                                       , ref bool p_send_thread_cancel_flag, ref bool p_receipt_received_flag, object p_send_thread_pulse_object
                                       , object p_worker_pulse_object, WorkerReceiveThreadConstruct p_recieve_thread_construct, Socket p_client_socket
                                       , ClientWorkerTimer p_worker_timer)
        {
            SendWorkerThreadStatus status = SendWorkerThreadStatus.Normal;

            System.Timers.Timer send_timer = new System.Timers.Timer();
            send_timer.Interval  = 3000;
            send_timer.AutoReset = false;

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

            bool cancelled    = false;
            int  retry_counts = 0;

            byte[] last_message_sent = new byte[1];

            SendThreadResendConstruct resend_construct = new SendThreadResendConstruct();

            send_timer.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) => send_timer_Elapsed(sender, e, resend_construct, p_send_thread_pulse_object));

            while (true)
            {
                lock (p_send_thread_pulse_object)
                {
                    if (!p_send_thread_cancel_flag && !p_receipt_received_flag && !p_send_thread_queue_flag && !resend_construct.resend_flag)
                    {
                        Monitor.Wait(p_send_thread_pulse_object);
                    }
                    if (p_send_thread_cancel_flag)
                    {
                        status = SendWorkerThreadStatus.End;
                        send_timer.Stop();
                        cancelled = true;
                        break;
                    }
                    if (p_receipt_received_flag && status == SendWorkerThreadStatus.WaitingForAReceipt)
                    {
                        send_timer.Stop();
                        retry_counts            = 0;
                        status                  = SendWorkerThreadStatus.Normal;
                        p_receipt_received_flag = false;
                    }
                    if (resend_construct.resend_flag == true && status == SendWorkerThreadStatus.WaitingForAReceipt)
                    {
                        retry_counts++;
                        resend_construct.resend_flag = false;

                        if (retry_counts < 4)
                        {
                            send_timer.Stop();
                            send_timer.Start();

                            int sent_data_length = SendData(p_client_socket, last_message_sent);
                            if (sent_data_length > 0)
                            {
                                p_worker_timer.StartAndReset();
                            }
                        }
                        else
                        {
                            send_timer.Stop();
                            int mini_thread_id = HelperFunctions.GetGUID();
                            p_worker_timer.StartAndReset();
                            status = SendWorkerThreadStatus.Suspended;
                            Thread send_cancel_bytes_to_worker_thead = new Thread(() => SendCancelBytesToWorker(p_worker_pulse_object, p_recieve_thread_construct
                                                                                                                , all_mini_threads, all_mini_threads_lock, mini_thread_id));
                            lock (all_mini_threads_lock)
                            {
                                all_mini_threads.Add(mini_thread_id, send_cancel_bytes_to_worker_thead);
                            }
                            send_cancel_bytes_to_worker_thead.Start();
                        }
                    }
                    if (p_send_thread_queue_flag && (status == SendWorkerThreadStatus.Normal || status == SendWorkerThreadStatus.WaitingForAReceipt))
                    {
                        if (p_send_thread_queue.Count > 0)
                        {
                            if ((p_send_thread_queue.ElementAt(0).Key == TypeOfSend.WithReceipt && status == SendWorkerThreadStatus.Normal) ||
                                (p_send_thread_queue.ElementAt(0).Key == TypeOfSend.WithoutReceipt && (status == SendWorkerThreadStatus.Normal ||
                                                                                                       status == SendWorkerThreadStatus.WaitingForAReceipt)))
                            {
                                KeyValuePair <TypeOfSend, byte[]> send_object = p_send_thread_queue.Dequeue();
                                if (p_send_thread_queue.Count == 0)
                                {
                                    p_send_thread_queue_flag = false;
                                }

                                if (send_object.Key == TypeOfSend.WithReceipt && status == SendWorkerThreadStatus.Normal)
                                {
                                    status            = SendWorkerThreadStatus.WaitingForAReceipt;
                                    last_message_sent = send_object.Value;
                                    int sent_data_length = SendData(p_client_socket, send_object.Value);
                                    if (sent_data_length > 0)
                                    {
                                        p_worker_timer.StartAndReset();
                                    }
                                    send_timer.Start();
                                }
                                else if (send_object.Key == TypeOfSend.WithoutReceipt)
                                {
                                    SendData(p_client_socket, send_object.Value);
                                    p_worker_timer.StartAndReset();
                                }
                            }
                        }
                        else
                        {
                            p_send_thread_queue_flag = false;
                        }
                    }
                }
            }
            if (cancelled)
            {
                send_timer.Stop();
                Thread.CurrentThread.Abort();
            }
        }
예제 #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;
            }
        }