Browse Source

Use writeChannel for writing.

Steve Thielemann 3 years ago
parent
commit
52113c43f0
2 changed files with 39 additions and 0 deletions
  1. 4 0
      door/door_linux.go
  2. 35 0
      door/write_linux.go

+ 4 - 0
door/door_linux.go

@@ -267,6 +267,10 @@ func (d *Door) Init(doorname string) {
 	log.Printf("Loading dropfile %s\n", dropfile)
 
 	log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
+
+	writerChannel = make(chan string)
+	go Writer(d.Config.Comm_handle)
+
 	d.detect()
 	if Unicode {
 		BOXES = BOXES_UNICODE

+ 35 - 0
door/write_linux.go

@@ -5,11 +5,44 @@ import (
 	"syscall"
 )
 
+var writerChannel chan string
+
+func Writer(handle int) {
+	for output := range writerChannel {
+		buffer := []byte(output)
+		n, err := syscall.Write(handle, buffer)
+		if (err != nil) || (n != len(buffer)) {
+			close(writerChannel)
+			break
+		}
+	}
+}
+
+/*
+reading from a closed channel is easy to detect.
+
+res, ok := <-channel
+ok == false
+
+writing to a closed channel is a panic.
+
+*/
+
 // Write string to client.
 func (d *Door) Write(output string) {
 	if d.Disconnected {
 		return
 	}
+
+	defer func() {
+		if r := recover(); r != nil {
+			fmt.println("Write error/HANGUP.")
+			d.Disconnected = true
+	}()
+
+	writerChannel <- output
+
+	/*
 	buffer := []byte(output)
 	n, err := syscall.Write(d.WRITEFD, buffer)
 	if err != nil {
@@ -20,4 +53,6 @@ func (d *Door) Write(output string) {
 	if n != len(buffer) {
 		fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
 	}
+	*/
+	
 }