MCPcopy
hub / github.com/probelabs/goreplay / Send

Method Send

tcp_client.go:98–193  ·  view source on GitHub ↗

Send sends data over created tcp connection

(data []byte)

Source from the content-addressed store, hash-verified

96
97// Send sends data over created tcp connection
98func (c *TCPClient) Send(data []byte) (response []byte, err error) {
99 // Don't exit on panic
100 defer func() {
101 if r := recover(); r != nil {
102 Debug(1, "[TCPClient]", r, string(data))
103
104 if _, ok := r.(error); !ok {
105 Debug(1, "[TCPClient] Failed to send request: ", string(data))
106 Debug(1, "PANIC: pkg:", r, debug.Stack())
107 }
108 }
109 }()
110
111 if c.conn == nil || !c.isAlive() {
112 Debug(1, "[TCPClient] Connecting:", c.baseURL)
113 if err = c.Connect(); err != nil {
114 Debug(1, "[TCPClient] Connection error:", err)
115 return
116 }
117 }
118
119 timeout := time.Now().Add(c.config.Timeout)
120
121 c.conn.SetWriteDeadline(timeout)
122
123 if c.config.Debug {
124 Debug(1, "[TCPClient] Sending:", string(data))
125 }
126
127 if _, err = c.conn.Write(data); err != nil {
128 Debug(1, "[TCPClient] Write error:", err, c.baseURL)
129 return
130 }
131
132 var readBytes, n int
133 var currentChunk []byte
134 timeout = time.Now().Add(c.config.Timeout)
135
136 for {
137 c.conn.SetReadDeadline(timeout)
138
139 if readBytes < len(c.respBuf) {
140 n, err = c.conn.Read(c.respBuf[readBytes:])
141 readBytes += n
142
143 if err != nil {
144 if err == io.EOF {
145 err = nil
146 }
147 break
148 }
149 } else {
150 if currentChunk == nil {
151 currentChunk = make([]byte, readChunkSize)
152 }
153
154 n, err = c.conn.Read(currentChunk)
155

Callers 1

sendRequestMethod · 0.45

Calls 5

isAliveMethod · 0.95
ConnectMethod · 0.95
WriteMethod · 0.80
DebugFunction · 0.70
ReadMethod · 0.45

Tested by

no test coverage detected