Jelajahi Sumber

Working windows.

Steve Thielemann 2 tahun lalu
induk
melakukan
8dd436dff6
4 mengubah file dengan 51 tambahan dan 1 penghapusan
  1. 43 0
      door/input_win.c
  2. 5 0
      door/input_win.h
  3. 1 1
      door/input_windows.go
  4. 2 0
      door/write_windows.go

+ 43 - 0
door/input_win.c

@@ -0,0 +1,43 @@
+#include "input_win.h"
+
+ void init_wsa(void) {
+    WSADATA wsa;
+ 	WSAStartup(WINSOCK_VERSION, &wsa); 
+ }
+
+ int windows_read(int fd, char *buffer, int len) {
+    struct timeval tv;
+    fd_set rfd;
+    
+    retry:
+    FD_ZERO(&rfd);
+    FD_SET(fd, &rfd);
+    tv.tv_sec = 0;
+    tv.tv_usec = 200;
+    int rs;
+    int ret;
+
+    rs = select(fd+1, &rfd, NULL, NULL, &tv);
+    if (errno == EINTR) {
+        goto retry;
+    }
+    if (rs == -1)  {
+        // error
+        return -1;
+    }
+
+    if (rs == 0 ) {
+        return 0;
+    }
+
+    if (FD_ISSET(fd, &rfd)) {
+        ret = recv(fd, buffer, len, 0);
+        if (ret == 0) {
+            // select, recv = 0: connection closed.
+            return -1;
+        }
+        return ret;
+    }
+
+    return -1;
+ }

+ 5 - 0
door/input_win.h

@@ -0,0 +1,5 @@
+#include <winsock2.h>
+#include <string.h>
+
+void init_wsa(void);
+int windows_read(int fd, char *buffer, int len);

+ 1 - 1
door/input_windows.go

@@ -62,7 +62,7 @@ func Reader(d *Door) {
 	for {
 		read := C.windows_read(C.int(d.Config.Comm_handle), (*C.char)(readerbuffer), C.int(READ_SIZE))
 		r = int(read)
-		log.Printf("windows_read: %d\n", r)
+		// log.Printf("windows_read: %d\n", r)
 
 		if r == -1 {
 			log.Println("window_read -1 (closed)")

+ 2 - 0
door/write_windows.go

@@ -71,6 +71,8 @@ func Writer(d *Door) {
 		err = syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
 		if err != nil {
 			fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
+		} else {
+			log.Printf("write %d bytes\n", DataWrite)
 		}
 
 		if (err != nil) || (l != DataWrite) {