Explorar el Código

GEventLoop: Make the server fd/pid global, and only connect to server once.

Andreas Kling hace 6 años
padre
commit
eac3a6ad52
Se han modificado 2 ficheros con 34 adiciones y 24 borrados
  1. 29 21
      LibGUI/GEventLoop.cpp
  2. 5 3
      LibGUI/GEventLoop.h

+ 29 - 21
LibGUI/GEventLoop.cpp

@@ -20,18 +20,14 @@
 //#define GEVENTLOOP_DEBUG
 
 static HashMap<GShortcut, GAction*>* g_actions;
-static GEventLoop* s_mainGEventLoop;
+static GEventLoop* s_main_event_loop;
+int GEventLoop::s_event_fd = -1;
+pid_t GEventLoop::s_server_pid = -1;
 
-GEventLoop::GEventLoop()
+void GEventLoop::connect_to_server()
 {
-    if (!s_mainGEventLoop)
-        s_mainGEventLoop = this;
-
-    if (!g_actions)
-        g_actions = new HashMap<GShortcut, GAction*>;
-
-    m_event_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
-    if (m_event_fd < 0) {
+    s_event_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+    if (s_event_fd < 0) {
         perror("socket");
         ASSERT_NOT_REACHED();
     }
@@ -43,7 +39,7 @@ GEventLoop::GEventLoop()
     int retries = 1000;
     int rc = 0;
     while (retries) {
-        rc = connect(m_event_fd, (const sockaddr*)&address, sizeof(address));
+        rc = connect(s_event_fd, (const sockaddr*)&address, sizeof(address));
         if (rc == 0)
             break;
 #ifdef GEVENTLOOP_DEBUG
@@ -55,6 +51,18 @@ GEventLoop::GEventLoop()
     if (rc < 0) {
         ASSERT_NOT_REACHED();
     }
+}
+
+GEventLoop::GEventLoop()
+{
+    if (!s_main_event_loop) {
+        s_main_event_loop = this;
+        connect_to_server();
+    }
+
+    if (!g_actions)
+        g_actions = new HashMap<GShortcut, GAction*>;
+
 #ifdef GEVENTLOOP_DEBUG
     dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
 #endif
@@ -66,8 +74,8 @@ GEventLoop::~GEventLoop()
 
 GEventLoop& GEventLoop::main()
 {
-    ASSERT(s_mainGEventLoop);
-    return *s_mainGEventLoop;
+    ASSERT(s_main_event_loop);
+    return *s_main_event_loop;
 }
 
 void GEventLoop::quit(int code)
@@ -219,7 +227,7 @@ void GEventLoop::wait_for_event()
             max_fd = fd;
     };
 
-    add_fd_to_set(m_event_fd, rfds);
+    add_fd_to_set(s_event_fd, rfds);
     for (auto& notifier : m_notifiers) {
         if (notifier->event_mask() & GNotifier::Read)
             add_fd_to_set(notifier->fd(), rfds);
@@ -265,7 +273,7 @@ void GEventLoop::wait_for_event()
         }
     }
 
-    if (!FD_ISSET(m_event_fd, &rfds))
+    if (!FD_ISSET(s_event_fd, &rfds))
         return;
 
     bool success = drain_messages_from_server();
@@ -277,7 +285,7 @@ void GEventLoop::process_unprocessed_messages()
     auto unprocessed_events = move(m_unprocessed_messages);
     for (auto& event : unprocessed_events) {
         if (event.type == WSAPI_ServerMessage::Type::Greeting) {
-            m_server_pid = event.greeting.server_pid;
+            s_server_pid = event.greeting.server_pid;
             continue;
         }
 
@@ -341,7 +349,7 @@ bool GEventLoop::drain_messages_from_server()
 {
     for (;;) {
         WSAPI_ServerMessage message;
-        ssize_t nread = read(m_event_fd, &message, sizeof(WSAPI_ServerMessage));
+        ssize_t nread = read(s_event_fd, &message, sizeof(WSAPI_ServerMessage));
         if (nread < 0) {
             perror("read");
             quit(1);
@@ -416,7 +424,7 @@ void GEventLoop::unregister_notifier(Badge<GNotifier>, GNotifier& notifier)
 
 bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message)
 {
-    int nwritten = write(m_event_fd, &message, sizeof(WSAPI_ClientMessage));
+    int nwritten = write(s_event_fd, &message, sizeof(WSAPI_ClientMessage));
     return nwritten == sizeof(WSAPI_ClientMessage);
 }
 
@@ -425,10 +433,10 @@ bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_S
     for (;;) {
         fd_set rfds;
         FD_ZERO(&rfds);
-        FD_SET(m_event_fd, &rfds);
-        int rc = select(m_event_fd + 1, &rfds, nullptr, nullptr, nullptr);
+        FD_SET(s_event_fd, &rfds);
+        int rc = select(s_event_fd + 1, &rfds, nullptr, nullptr, nullptr);
         ASSERT(rc > 0);
-        ASSERT(FD_ISSET(m_event_fd, &rfds));
+        ASSERT(FD_ISSET(s_event_fd, &rfds));
         bool success = drain_messages_from_server();
         if (!success)
             return false;

+ 5 - 3
LibGUI/GEventLoop.h

@@ -39,7 +39,7 @@ public:
 
     WSAPI_ServerMessage sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type);
 
-    pid_t server_pid() const { return m_server_pid; }
+    pid_t server_pid() const { return s_server_pid; }
 
 private:
     void wait_for_event();
@@ -54,6 +54,7 @@ private:
     void handle_menu_event(const WSAPI_ServerMessage&);
     void handle_window_entered_or_left_event(const WSAPI_ServerMessage&, GWindow&);
     void get_next_timer_expiration(timeval&);
+    void connect_to_server();
 
     struct QueuedEvent {
         WeakPtr<GObject> receiver;
@@ -63,12 +64,13 @@ private:
 
     Vector<WSAPI_ServerMessage> m_unprocessed_messages;
 
-    int m_event_fd { -1 };
     bool m_running { false };
     bool m_exit_requested { false };
     int m_exit_code { 0 };
     int m_next_timer_id { 1 };
-    pid_t m_server_pid { 0 };
+
+    static pid_t s_server_pid;
+    static pid_t s_event_fd;
 
     struct EventLoopTimer {
         int timer_id { 0 };