public static void WorkerMainThread(ServerWorkerData p_worker_data, int p_port_num, int p_thread_id, object p_all_treads_data_lock, IPAddress p_server_ip_address) { ServerWorkerData worker_data = p_worker_data; int port_num = p_port_num; int thread_id = p_thread_id; ServerWorkerStatus status = ServerWorkerStatus.WaithForAclinet; System.Timers.Timer worker_timer_object = new System.Timers.Timer(); worker_timer_object.Interval = 12500; worker_timer_object.AutoReset = false; worker_timer_object.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) => worker_timer_Elapsed(sender, e, worker_data.send_to_worker_construct , worker_data.worker_pulse_object)); WorkerTimer timer = new WorkerTimer(worker_timer_object); Socket client_socket = null; IPEndPoint server_endpoint = new IPEndPoint(p_server_ip_address, port_num); TcpListener server_listener = new TcpListener(server_endpoint); try { System.Timers.Timer listener_timer = new System.Timers.Timer(10000); listener_timer.AutoReset = false; bool timer_elapsed = false; listener_timer.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) => listener_timer_Elapsed(sender, e, out timer_elapsed)); server_listener.Start(1); listener_timer.Start(); while (true) { Thread.Sleep(500); if (server_listener.Pending()) { client_socket = server_listener.AcceptSocket(); break; } if (timer_elapsed) { try { client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); } catch { } lock (p_all_treads_data_lock) { worker_data.remove_worker(p_thread_id); } Thread.CurrentThread.Abort(); return; } } } catch (Exception) { try { client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); } catch { } lock (p_all_treads_data_lock) { worker_data.remove_worker(p_thread_id); } Thread.CurrentThread.Abort(); return; } int last_message_sent_id = 0; bool cancelled = false; BinaryFormatter formatter = new BinaryFormatter(); formatter.Context = new StreamingContext(StreamingContextStates.All); SerializationBinder serialization_binder = formatter.Binder; MemoryStream stream = new MemoryStream(); byte[] data_buffer = new byte[1]; status = ServerWorkerStatus.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 client_socket , ref timer, worker_data.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.worker_pulse_object, receive_thread_construct, client_socket, timer)); receiver_thread.Start(); send_thread.Start(); timer.StartAndReset(); status = ServerWorkerStatus.Normal; while (true) { lock (worker_data.worker_pulse_object) { if (!worker_data.cancel_construct && !receive_thread_construct.receive_thread_queue_flag && !worker_data.send_to_worker_construct.send_to_worker_queue_flag) { Monitor.Wait(worker_data.worker_pulse_object); } if (worker_data.cancel_construct) { timer.PrimerStop(); 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(); lock (worker_data.server_pulse_object) { worker_data.receive_from_worker_construct.server_receive_quque.Enqueue(new MessageFromServerWorkerQueueObject(p_thread_id, new MessageFromServerWorker(TypeOfMessageFromServerWorker.ofllienRequestMessage, null))); worker_data.receive_from_worker_construct.server_receive_queue_flag = true; Monitor.Pulse(worker_data.server_pulse_object); cancelled = true; break; } } else { stream = new MemoryStream(byte_received_data); DataForSend data_received = null; try { data_received = (DataForSend)formatter.Deserialize(stream); } catch (Exception) { stream = new MemoryStream(); continue; } stream = new MemoryStream(); if (data_received.Get_data_for_send_type == TypeOfDataForSend.QuickAnswer && status == ServerWorkerStatus.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 = ServerWorkerStatus.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) { UserMessageToServer user_message = null; try { user_message = ((UserMessageToServer)data_received.Get_message_object); } catch { continue; } FinalMessageForServer final_message_for_server = null; try { final_message_for_server = user_message.Get_user_message; } catch { continue; } MessageFromServerWorkerQueueObject server_object = new MessageFromServerWorkerQueueObject(p_thread_id, new MessageFromServerWorker(TypeOfMessageFromServerWorker.FinalMessageForServer, final_message_for_server)); lock (worker_data.server_pulse_object) { worker_data.receive_from_worker_construct.server_receive_quque.Enqueue(server_object); worker_data.receive_from_worker_construct.server_receive_queue_flag = true; Monitor.Pulse(worker_data.server_pulse_object); } 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 == ServerWorkerStatus.Normal) { if (worker_data.send_to_worker_construct.send_to_worker_quque.Count > 0) { MessageToServerWorker object_to_send = worker_data.send_to_worker_construct.send_to_worker_quque.Dequeue(); if (worker_data.send_to_worker_construct.send_to_worker_quque.Count == 0) { worker_data.send_to_worker_construct.send_to_worker_queue_flag = false; } if (object_to_send.Get_type_of_message_to_serverworker == TypeOfMessageToServerWorker.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 = ServerWorkerStatus.WaitForAReceipt; stream = new MemoryStream(); } else if (object_to_send.Get_type_of_message_to_serverworker == TypeOfMessageToServerWorker.FinalMessageToClient) { FinalMessageForClient final_message = (FinalMessageForClient)object_to_send.Get_message_to_server_worker_object; UserMessageToClient user_message_to_send = new UserMessageToClient(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 = ServerWorkerStatus.WaitForAReceipt; stream = new MemoryStream(); } } else { worker_data.send_to_worker_construct.send_to_worker_queue_flag = false; } } } } } if (cancelled) { timer.PrimerStop(); try { receiver_thread.Abort(); } catch { } try { client_socket.Shutdown(SocketShutdown.Both); client_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(); lock (p_all_treads_data_lock) { worker_data.remove_worker(p_thread_id); } Thread.CurrentThread.Abort(); return; } }
public void Receive(MessageFromServerWorkerQueueObject p_global_message_to_server_from_queue) { if (thread_is_logged_in(p_global_message_to_server_from_queue.Get_thread_id)) { string user_name = get_a_thread_user_name(p_global_message_to_server_from_queue.Get_thread_id); if (user_name == null) { return; } if (p_global_message_to_server_from_queue.Get_message_from_worker.Get_type_of_message_from_server_worker == TypeOfMessageFromServerWorker.FinalMessageForServer) { FinalMessageForServer user_message = null; try { user_message = (FinalMessageForServer)p_global_message_to_server_from_queue.Get_message_from_worker.Get_message_from_server_worker_object; } catch (Exception) { return; } if (user_message.Get_message_type == TypeOfMessage.Chat) { ChatMessageForServer user_chat_message = null; try { user_chat_message = ((ChatMessageForServer)user_message.Get_message_object); chat_receive_message(new AuthServerChatMessage(user_name, user_chat_message)); return; } catch (Exception) { return; } } else if (user_message.Get_message_type == TypeOfMessage.Dialog) { DialogMessageForServer user_dialog_message = null; try { user_dialog_message = (DialogMessageForServer)user_message.Get_message_object; dialog_receive_message(new AuthServerDialogMessage(user_dialog_message, user_name)); return; } catch (Exception) { return; } } } else if (p_global_message_to_server_from_queue.Get_message_from_worker.Get_type_of_message_from_server_worker == TypeOfMessageFromServerWorker.MakeOfflineMessage) { List <FinalMessageForServer> make_offline_messages = new List <FinalMessageForServer>(); try { make_offline_messages = (List <FinalMessageForServer>)p_global_message_to_server_from_queue.Get_message_from_worker.Get_message_from_server_worker_object; } catch (Exception) { return; } make_messages_offline(user_name, make_offline_messages); return; } else if (p_global_message_to_server_from_queue.Get_message_from_worker.Get_type_of_message_from_server_worker == TypeOfMessageFromServerWorker.ofllienRequestMessage) { offline_user(user_name); return; } } else if (!thread_is_logged_in(p_global_message_to_server_from_queue.Get_thread_id)) { if (!is_there_unauth_worker_thread(p_global_message_to_server_from_queue.Get_thread_id)) { register_a_thread_request(p_global_message_to_server_from_queue.Get_thread_id); } if (p_global_message_to_server_from_queue.Get_message_from_worker.Get_type_of_message_from_server_worker == TypeOfMessageFromServerWorker.FinalMessageForServer) { FinalMessageForServer user_message = null; try { user_message = ((FinalMessageForServer)p_global_message_to_server_from_queue.Get_message_from_worker.Get_message_from_server_worker_object); } catch (Exception) { return; } if (user_message.Get_message_type == TypeOfMessage.Dialog) { DialogMessageForServer user_dialog_message = null; try { user_dialog_message = (DialogMessageForServer)user_message.Get_message_object; } catch (Exception) { return; } dialog_receive_message(new UnAuthServerDialogMessage(user_dialog_message, p_global_message_to_server_from_queue.Get_thread_id)); return; } } if (p_global_message_to_server_from_queue.Get_message_from_worker.Get_type_of_message_from_server_worker == TypeOfMessageFromServerWorker.ofllienRequestMessage) { thread_remove_works(p_global_message_to_server_from_queue.Get_thread_id); } } }