123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package door
- import (
- "errors"
- "fmt"
- "net"
- "os"
- "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 {
- // DeadlineExceedError
- // Ignore DeadlineExceeded error (it is expected here)
- if !errors.Is(err, os.ErrDeadlineExceeded) {
- t.Logf("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(CursorPos{1, 1}) +
- CursorReply(CursorPos{3, 1}) +
- CursorReply(CursorPos{width, height})
- }
- // CP437 response with screen width/height. (80x25)
- func CP437WidthHeight(width, height int) string {
- return CursorReply(CursorPos{3, 1}) +
- CursorReply(CursorPos{4, 1}) +
- CursorReply(CursorPos{width, height})
- }
- // Cursor Position Reply
- func CursorReply(c CursorPos) string {
- return fmt.Sprintf("\x1b[%d;%dR", c.Y, c.X)
- }
- // Mouse Button X,Y Reply
- func MouseReply(m Mouse) string {
- return fmt.Sprintf("\x1b[M%c%c%c", m.Button+' '-1, m.X+'!'-1, m.Y+'!'-1)
- }
|