Branch: refs/heads/net-split
  Home:   https://github.com/mailru/tarantool
  Commit: b55c0f5726d830e6346f137a8a13f5e2cf8aa4d7
      
https://github.com/mailru/tarantool/commit/b55c0f5726d830e6346f137a8a13f5e2cf8aa4d7
  Author: Aleksey Demakov <[email protected]>
  Date:   2012-05-04 (Fri, 04 May 2012)

  Changed paths:
    M include/sock.h
    M src/sock.m

  Log Message:
  -----------
  Add sock_read() and sock_write() functions.


diff --git a/include/sock.h b/include/sock.h
index 459aacd..88e44fd 100644
--- a/include/sock.h
+++ b/include/sock.h
@@ -38,4 +38,7 @@
 int sock_create_server(struct sockaddr_in *addr, int backlog);
 int sock_accept_client(int sockfd);
 
+size_t sock_read(int fd, void *buf, size_t count);
+size_t sock_write(int fd, void *buf, size_t count);
+
 #endif /* TARANTOOL_SOCK_H_INCLUDED */
diff --git a/src/sock.m b/src/sock.m
index 4792516..a195b0a 100644
--- a/src/sock.m
+++ b/src/sock.m
@@ -260,15 +260,66 @@ @implementation SocketError: SystemError
 {
        /* Accept a connection. */
        int fd = sock_accept(sockfd);
-
-       /* Set appropriate options. */
        @try {
+               /* Set appropriate options. */
                sock_set_server_accepted_options(fd);
+
+               return fd;
        }
        @catch (id e) {
                close(fd);
                @throw;
        }
+}
 
-       return fd;
+/**
+ * Read from a socket.
+ */
+size_t
+sock_read(int fd, void *buf, size_t count)
+{
+       size_t total = 0;
+       while (count > 0) {
+               ssize_t n = read(fd, buf, count);
+               if (n == 0) {
+                       break;
+               } else if (n < 0) {
+                       if (errno == EAGAIN || errno == EWOULDBLOCK) {
+                               break;
+                       } else if (errno == EINTR) {
+                               continue;
+                       }
+                       tnt_raise(SocketError, :"read");
+               }
+
+               buf += n;
+               count -= n;
+               total += n;
+       }
+       return total;
+}
+
+/**
+ * Write to a socket.
+ */
+size_t
+sock_write(int fd, void *buf, size_t count)
+{
+       size_t total = 0;
+       while (count > 0) {
+               ssize_t n = write(fd, buf, count);
+               if (n < 0) {
+                       if (errno == EAGAIN || errno == EWOULDBLOCK) {
+                               break;
+                       } else if (errno == EINTR) {
+                               continue;
+                       }
+                       tnt_raise(SocketError, :"read");
+               }
+
+               buf += n;
+               count -= n;
+               total += n;
+       }
+       return total;
 }


================================================================

_______________________________________________
Mailing list: https://launchpad.net/~tarantool-developers
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~tarantool-developers
More help   : https://help.launchpad.net/ListHelp

Reply via email to