connection: Write before reading connection data
authorBenjamin Franzke <benjaminfranzke@googlemail.com>
Fri, 14 Jan 2011 23:40:00 +0000 (00:40 +0100)
committerBenjamin Franzke <benjaminfranzke@googlemail.com>
Fri, 14 Jan 2011 23:40:00 +0000 (00:40 +0100)
wayland/connection.c

index dadf2c3..e8e8357 100644 (file)
@@ -251,68 +251,68 @@ wl_connection_data(struct wl_connection *connection, uint32_t mask)
        char cmsg[128];
        int len, count, clen;
 
-       if (mask & WL_CONNECTION_READABLE) {
-               wl_buffer_put_iov(&connection->in, iov, &count);
+       if (mask & WL_CONNECTION_WRITABLE) {
+               wl_buffer_get_iov(&connection->out, iov, &count);
+
+               build_cmsg(&connection->fds_out, cmsg, &clen);
 
                msg.msg_name = NULL;
                msg.msg_namelen = 0;
                msg.msg_iov = iov;
                msg.msg_iovlen = count;
                msg.msg_control = cmsg;
-               msg.msg_controllen = sizeof cmsg;
+               msg.msg_controllen = clen;
                msg.msg_flags = 0;
 
                do {
-                       len = recvmsg(connection->fd, &msg, 0);
+                       len = sendmsg(connection->fd, &msg, 0);
                } while (len < 0 && errno == EINTR);
 
                if (len < 0) {
                        fprintf(stderr,
-                               "read error from connection %p: %m (%d)\n",
-                               connection, errno);
-                       return -1;
-               } else if (len == 0) {
-                       /* FIXME: Handle this better? */
+                               "write error for connection %p, fd %d: %m\n",
+                               connection, connection->fd);
                        return -1;
                }
 
-               decode_cmsg(&connection->fds_in, &msg);
-
-               connection->in.head += len;
-       }       
+               close_fds(&connection->fds_out);
 
-       if (mask & WL_CONNECTION_WRITABLE) {
-               wl_buffer_get_iov(&connection->out, iov, &count);
+               connection->out.tail += len;
+               if (connection->out.tail == connection->out.head)
+                       connection->update(connection,
+                                          WL_CONNECTION_READABLE,
+                                          connection->data);
+       }
 
-               build_cmsg(&connection->fds_out, cmsg, &clen);
+       if (mask & WL_CONNECTION_READABLE) {
+               wl_buffer_put_iov(&connection->in, iov, &count);
 
                msg.msg_name = NULL;
                msg.msg_namelen = 0;
                msg.msg_iov = iov;
                msg.msg_iovlen = count;
                msg.msg_control = cmsg;
-               msg.msg_controllen = clen;
+               msg.msg_controllen = sizeof cmsg;
                msg.msg_flags = 0;
 
                do {
-                       len = sendmsg(connection->fd, &msg, 0);
+                       len = recvmsg(connection->fd, &msg, 0);
                } while (len < 0 && errno == EINTR);
 
                if (len < 0) {
                        fprintf(stderr,
-                               "write error for connection %p, fd %d: %m\n",
-                               connection, connection->fd);
+                               "read error from connection %p: %m (%d)\n",
+                               connection, errno);
+                       return -1;
+               } else if (len == 0) {
+                       /* FIXME: Handle this better? */
                        return -1;
                }
 
-               close_fds(&connection->fds_out);
+               decode_cmsg(&connection->fds_in, &msg);
 
-               connection->out.tail += len;
-               if (connection->out.tail == connection->out.head)
-                       connection->update(connection,
-                                          WL_CONNECTION_READABLE,
-                                          connection->data);
-       }
+               connection->in.head += len;
+       }       
 
        return connection->in.head - connection->in.tail;
 }