Debian Codegraph @

net.go

raw symbols SCIP
1 package mssql
2
3 import (
4 "fmt"
5 "net"
6 "time"
7 )
8
9 type timeoutConn struct {
10 c net.Conn
11 timeout time.Duration
12 buf *tdsBuffer
13 packetPending bool
14 continueRead bool
15 }
16
17 func NewTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
18 return &timeoutConn{
19 c: conn,
20 timeout: timeout,
21 }
22 }
23
24 func (c *timeoutConn) Read(b []byte) (n int, err error) {
25 if c.buf != nil {
26 if c.packetPending {
27 c.packetPending = false
28 err = c.buf.FinishPacket()
29 if err != nil {
30 err = fmt.Errorf("Cannot send handshake packet: %s", err.Error())
31 return
32 }
33 c.continueRead = false
34 }
35 if !c.continueRead {
36 var packet packetType
37 packet, err = c.buf.BeginRead()
38 if err != nil {
39 err = fmt.Errorf("Cannot read handshake packet: %s", err.Error())
40 return
41 }
42 if packet != packPrelogin {
43 err = fmt.Errorf("unexpected packet %d, expecting prelogin", packet)
44 return
45 }
46 c.continueRead = true
47 }
48 n, err = c.buf.Read(b)
49 return
50 }
51 err = c.c.SetDeadline(time.Now().Add(c.timeout))
52 if err != nil {
53 return
54 }
55 return c.c.Read(b)
56 }
57
58 func (c *timeoutConn) Write(b []byte) (n int, err error) {
59 if c.buf != nil {
60 if !c.packetPending {
61 c.buf.BeginPacket(packPrelogin)
62 c.packetPending = true
63 }
64 n, err = c.buf.Write(b)
65 if err != nil {
66 return
67 }
68 return
69 }
70 err = c.c.SetDeadline(time.Now().Add(c.timeout))
71 if err != nil {
72 return
73 }
74 return c.c.Write(b)
75 }
76
77 func (c timeoutConn) Close() error {
78 return c.c.Close()
79 }
80
81 func (c timeoutConn) LocalAddr() net.Addr {
82 return c.c.LocalAddr()
83 }
84
85 func (c timeoutConn) RemoteAddr() net.Addr {
86 return c.c.RemoteAddr()
87 }
88
89 func (c timeoutConn) SetDeadline(t time.Time) error {
90 panic("Not implemented")
91 }
92
93 func (c timeoutConn) SetReadDeadline(t time.Time) error {
94 panic("Not implemented")
95 }
96
97 func (c timeoutConn) SetWriteDeadline(t time.Time) error {
98 panic("Not implemented")
99 }