diff --git a/conn_test.go b/conn_test.go new file mode 100644 index 00000000..c90f0bd3 --- /dev/null +++ b/conn_test.go @@ -0,0 +1,26 @@ +package pqx + +import ( + // "encoding/binary" + "net" + "testing" +) + +func TestXxx(t *testing.T) { + conn, err := net.Dial("tcp", "localhost:5432") + if err != nil { + // handle error + } + + msg := newStartupMsg() + msg.options["user"] = "jack" + + msg.WriteTo(conn) + + + buf := make([]byte, 128) + + num, _ := conn.Read(buf) + println(string(buf[0:1])) + println(num) +} diff --git a/messages.go b/messages.go new file mode 100644 index 00000000..ececc772 --- /dev/null +++ b/messages.go @@ -0,0 +1,35 @@ +package pqx + +import ( + "encoding/binary" + "io" +) + +const ( + protocolVersionNumber = 196608 // 3.0 +) + +type startupMsg struct { + options map[string] string +} + +func newStartupMsg() *startupMsg { + return &startupMsg{map[string] string{}} +} + +func (self *startupMsg) WriteTo(w io.Writer) (n int64, err error) { + buf := make([]byte, 8, 128) + binary.BigEndian.PutUint32(buf[4:8], uint32(protocolVersionNumber)) + for key, value := range self.options { + buf = append(buf, key...) + buf = append(buf, 0) + buf = append(buf, value...) + buf = append(buf, 0) + } + buf = append(buf, ("\000")...) + binary.BigEndian.PutUint32(buf[0:4], uint32(len(buf))) + + var n32 int + n32, err = w.Write(buf) + return int64(n32), err +} \ No newline at end of file