PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/connection.go

http://github.com/simonz05/exp-godis
Go | 63 lines | 43 code | 15 blank | 5 comment | 6 complexity | 33775cf69c77461721c7f4346c847801 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package godis
  2. import (
  3. "net"
  4. )
  5. var ConnSum = 0
  6. type Connection interface {
  7. Write(args ...interface{}) error
  8. Read() (*Reply, error)
  9. Close() error
  10. Sock() net.Conn
  11. }
  12. type Conn struct {
  13. rbuf *reader
  14. c net.Conn
  15. }
  16. // New connection
  17. func NewConn(addr, proto string) (*Conn, error) {
  18. c, err := net.Dial(proto, addr)
  19. if err != nil {
  20. return nil, err
  21. }
  22. ConnSum++
  23. return &Conn{newReader(c), c}, nil
  24. }
  25. // read and parse a reply from socket
  26. func (c *Conn) Read() (*Reply, error) {
  27. reply := Parse(c.rbuf)
  28. if reply.Err != nil {
  29. return nil, reply.Err
  30. }
  31. return reply, nil
  32. }
  33. // write args to socket
  34. func (c *Conn) Write(args ...interface{}) error {
  35. _, e := c.c.Write(format(args...))
  36. if e != nil {
  37. return e
  38. }
  39. return nil
  40. }
  41. // close socket connection
  42. func (c *Conn) Close() error {
  43. return c.c.Close()
  44. }
  45. // returns the net.Conn for the struct
  46. func (c *Conn) Sock() net.Conn {
  47. return c.c
  48. }