package door import ( "fmt" "net" "testing" "time" ) // helper routines - to help with testing. // Using pipes causes failures. func Possible_setupSockets() (server net.Conn, client net.Conn) { // the better way to set up connections. return net.Pipe() } // setupSockets: return server and client (that's connected to server) func setupSockets() (server net.Conn, client net.Conn) { // establish network socket connection to set Comm_handle var err error var sock net.Listener sock, err = net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } // I only need address for making the connection. // Get address of listening socket address := sock.Addr().String() client, err = net.Dial("tcp", address) if err != nil { panic(err) } server, err = sock.Accept() if err != nil { panic(err) } sock.Close() return server, client } func clear_socket(socket net.Conn, t *testing.T) string { // Clear out the data that's pending in the socket. buffer := make([]byte, 1204) var r int var err error err = socket.SetReadDeadline(time.Now().Add(time.Millisecond * 20)) if err != nil { t.Error("socket.SetReadDeadLine:", err) } r, err = socket.Read(buffer) if err != nil { t.Errorf("socket.Read: %#v", err) } // t.Errorf("Buffer : %#v\n", buffer[:r]) err = socket.SetReadDeadline(time.Time{}) if err != nil { t.Error("socket.SetReadDeadLine:", err) } return string(buffer[:r]) } // Unicode detection response with given screen width/height. func UnicodeWidthHeight(width, height int) string { return CursorReply(1, 1) + CursorReply(3, 1) + CursorReply(width, height) } // CP437 response with screen width/height. (80x25) func CP437WidthHeight(width, height int) string { return CursorReply(3, 1) + CursorReply(4, 1) + CursorReply(width, height) } // Cursor Position Reply func CursorReply(x, y int) string { return fmt.Sprintf("\x1b[%d;%dR", y, x) } // Mouse Button X,Y Reply func MouseResponse(button, x, y int8) string { return fmt.Sprintf("\x1b[M%c%c%c", button+' '+1, x+'!'+1, y+'!'+1) }