|  | @@ -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)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +	*/
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  |  }
 |