public static void ThreadRunner(Dictionary <TypeOfDialog, Dictionary <int, Se_AuthDialog> > p_auth_dialogs , ref Dictionary <TypeOfDialog, Dictionary <int, Se_UnAuthDialog> > p_unauth_dialogs, Dictionary <int, PrivateChat> p_all_private_chats , Dictionary <int, PublicChat> p_all_public_chats, ref Dictionary <int, AddAgreement> p_all_add_agreements, Dictionary <int, UserData> p_all_users_logged_in, List <int> p_all_threads, SendToDistributerConstruct p_send_to_distributer_construct, ReceiveFromServerWorkerConstruct p_receive_from_worker_construct , object p_distributer_pulse_object, object p_server_thread_pulse_object) { Server server = new Server(p_auth_dialogs, ref p_unauth_dialogs, p_all_private_chats, p_all_public_chats, ref p_all_add_agreements, p_all_users_logged_in , p_all_threads, p_send_to_distributer_construct, p_distributer_pulse_object); while (true) { lock (p_server_thread_pulse_object) { if (!p_receive_from_worker_construct.server_receive_queue_flag) { Monitor.Wait(p_server_thread_pulse_object); } if (p_receive_from_worker_construct.server_receive_quque.Count > 0) { MessageFromServerWorkerQueueObject message_for_server = p_receive_from_worker_construct.server_receive_quque.Dequeue(); if (p_receive_from_worker_construct.server_receive_quque.Count == 0) { p_receive_from_worker_construct.server_receive_queue_flag = false; } server.Receive(message_for_server); } else { p_receive_from_worker_construct.server_receive_queue_flag = false; } } } }
static void Main(string[] args) { IPEndPoint server_udp_ip_endpoint = new IPEndPoint(IPAddress.Any, 0); int server_check_data = 0; IPAddress server_tcp_ip = IPAddress.Any; Console.WriteLine("Retrieve configurations"); try { LoadConfigs(out server_udp_ip_endpoint, out server_check_data, out server_tcp_ip); } catch { Console.WriteLine("AN Error occured in working with config"); Console.ReadLine(); return; } Console.WriteLine("loading data..."); Dictionary <TypeOfDialog, Dictionary <int, Se_AuthDialog> > all_auth_dialogs = CreateAllAuthDialogs(); Dictionary <TypeOfDialog, Dictionary <int, Se_UnAuthDialog> > all_unauth_dialogs = CreateAllUnAuthDialogs(); Dictionary <int, PrivateChat> all_private_chats = new Dictionary <int, PrivateChat>(); Dictionary <int, PublicChat> all_public_chats = new Dictionary <int, PublicChat>(); Dictionary <int, AddAgreement> all_add_agreements = new Dictionary <int, AddAgreement>(); Dictionary <int, UserData> all_users_logged_in = new Dictionary <int, UserData>(); List <int> all_threads = new List <int>(); Queue <MessageToDistributer> send_to_distributer_queue = new Queue <MessageToDistributer>(); SendToDistributerConstruct send_to_disributer_construct = new SendToDistributerConstruct(send_to_distributer_queue); Queue <MessageFromServerWorkerQueueObject> server_receive_quque = new Queue <MessageFromServerWorkerQueueObject>(); ReceiveFromServerWorkerConstruct receive_from_worker_construct = new ReceiveFromServerWorkerConstruct(server_receive_quque); object distributer_thread_pulse_object = new object(); object server_thread_pulse_object = new object(); Dictionary <int, KeyValuePair <Thread, ServerWorkerData> > all_workers_data = new Dictionary <int, KeyValuePair <Thread, ServerWorkerData> >(); object all_workers_data_lock = new object(); object producer_thread_pulse_object = new object(); Queue <int> workers_port_number_queue = new Queue <int>(); WorkersPortNumberConstruct workers_port_number_construct = new WorkersPortNumberConstruct(workers_port_number_queue); Thread server_thread = new Thread(() => ServerThread.ThreadRunner(all_auth_dialogs, ref all_unauth_dialogs, all_private_chats, all_public_chats , ref all_add_agreements, all_users_logged_in, all_threads, send_to_disributer_construct, receive_from_worker_construct, distributer_thread_pulse_object , server_thread_pulse_object)); Thread.Sleep(100); Thread worker_producer_thread = new Thread(() => OtherThreads.WorkerProducerThread(all_workers_data, all_workers_data_lock, producer_thread_pulse_object , receive_from_worker_construct, workers_port_number_construct, server_thread_pulse_object , server_tcp_ip)); Thread.Sleep(100); Thread distributer_thread = new Thread(() => OtherThreads.DistributerThread(send_to_disributer_construct, distributer_thread_pulse_object, all_workers_data, all_workers_data_lock)); Thread.Sleep(100); Thread udp_thread = new Thread(() => OtherThreads.UDPThread(workers_port_number_construct, producer_thread_pulse_object, server_check_data , server_udp_ip_endpoint, server_tcp_ip)); Console.WriteLine("starting server."); distributer_thread.Start(); server_thread.Start(); worker_producer_thread.Start(); udp_thread.Start(); try { Console.ReadLine(); } catch { try { server_thread.Abort(); } catch { } try { worker_producer_thread.Abort(); } catch { } try { distributer_thread.Abort(); } catch { } try { udp_thread.Abort(); } catch { } try { Environment.Exit(2); } catch { } } return; }