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; } } } }
public Server(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, object p_distributer_pulse_object) { Se_ServerDelegateForDialogs server_delegates_for_dialogs = new Se_ServerDelegateForDialogs(new GetFriendsList(Se_GetUserfriendList), new GetUserStatus(Se_GetUserStatus) , new GetPublicChatIds(Se_GetPublicChatIds), new GetOfflineMessages(Se_GetAllUserOfflineMessages), new GetAllAgreementInvitation(Se_GetAllUserAgreementInvitation) , new AuthSend(Se_Authsend), new IsLoggedIn(Se_IsLoggedIn), new UnAuthSend(Se_UnAuthSend), new IsThereUnauthWorkerThread(Se_IsThereUnauthWorkerThread) , new Login(Se_Login), new ServerInformChatLeave(Se_ServerInformChatLeave), new OnlineAreFriends(Se_OnlineAreFriends), new CreatePrivateChat(Se_CreatePrivateChat) , new IsTherePrivateChat(Se_IsTherePrivateChat), new StartClientCreatedPrivateChatInform(Se_StartClientCreatedPrivateChatInform) , new JoinPublicChatRequest(Se_JoinPublicChatRequest), new IsUserInPublicChat(Se_IsUserInPublicChat), new GetPublicChatUsersList(Se_GetPublicChatUsersList) , new CreateFormalMessageRequest(Se_CreateFormalMessageRequest), new IstherUser(Se_IstherUser), new CreateOfflineMessage(Se_CreateOfflineMessage) , new SignUp(Se_Signup), new CreateAddAgreement(Se_CreateAddAgreement), new GetAgreementAnswer(Se_GetAgreementAnswer), new OfflineMessagesReadInform(Se_OfflineMessagesReadInform) , new GetPrivateChatInvitationAnswer(Se_GetPrivateChaInvitationAnswer), new GetAUserAgreementInvitation(Se_DB_GetAUserAgreementInvitation)); server_core = new ServerCore(p_all_users_logged_in, new StartClientFriendChangedStatusInformDialog(Se_StartClientFriendChangedStatusInform) , new StartSendToClinetFormalMessage(Se_StartSendToClinetFormalMessage), p_all_threads, p_send_to_distributer_construct, p_distributer_pulse_object); all_dialogs = new AllDialogs(server_delegates_for_dialogs, ref p_auth_dialogs, ref p_unauth_dialogs); all_chats = new AllChats(p_all_private_chats, p_all_public_chats, new ChatSend(Se_ChatSend), new StartEjectedChatUserInform(Se_StartEjectedChatUserInform), new IsLoggedIn(Se_IsLoggedIn), new StartClientSomeoneJoinedChatInform(Se_StartClientSomeoneJoinedChatInform), new StartClientSomeoneLeftChatInform(Se_StartClientSomeoneLeftChatInform)); all_agreements = new AllAgreements(ref p_all_add_agreements, new CreateFormalMessageRequest(Se_CreateFormalMessageRequest), new DB_IsThereUser(Se_IstherUser) , new DB_AddToFriends(Se_AddToFriends), new DB_AreFriends(Se_AreFriends), new DB_CreateAddAgreement(Se_DB_CreateAddAgreement), new DB_RemoveAgreement(Se_RemoveAgreement) , new IsLoggedIn(Se_IsLoggedIn), new StartClientFriendListChangedInformDialog(Se_StartClientFriendListChangedInformDialog), new ReloadFriendList(Se_ReloadFriendList) , new GetOnlineUserFriendList(Se_GetOnlineUserFriendList), new GetUserStatus(Se_GetUserStatus), new AddAgreementDone(Se_AddAgreementDone) , new GetAllAdAgreements(Se_GetAllAdAgreements), new StartClientInvitedAgreementInform(Se_StartClientInvitedAgreementInform)); thread_works = new ThreadWorks(new RegisterThread(Se_RegisterThread), new IsThereUnauthWorkerThread(Se_IsThereUnauthWorkerThread) , new RemoveThread(Se_RemoveThread), new RemoveAThreadDialogs(Se_RemoveAThreadDialogs)); offline_user_works = new OfflineUserWorks(new ChangeStatusToOffline(Se_ChangeStatusToOffline), new OfflineUser(Se_OfflineUser), new GetUserPresenceState(Se_GetUserPresenceState) , new GetOnlineUserThreadID(Se_GetOnlineUserThreadID), new UserLeaveAllChats(Se_UserLeavAllChats), new RemoveAThreadDialogs(Se_RemoveAThreadDialogs) , new RemoveAUserDialogs(Se_RemoveAUserDialogs), new RemoveThread(Se_RemoveThread)); authenicate_and_distribute = new AuthenticateAndDistribute(new GetAThreadUserName(Se_GetAThreadUserName), new IsThereUnauthWorkerThread(Se_IsThereUnauthWorkerThread) , new DialogReceiveMessage(Se_DialogReceiveMessage), new ChatReceiveMessage(Se_ChatReceiveMessage), new MakeMessagesOffline(Se_MakeMessagesOffline) , new UserOfflineRequest(Se_UserOfflineRequest), new ThreadisLoggedIn(Se_ThreadIsLoggedIn), new RegisterAThreadRequest(Se_RegisterAThreadRequest) , new ThreadRemoveWorks(Se_ThreadRemoveWorks)); }
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; }