forked from Philio/GoMySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
59 lines (55 loc) · 1.24 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// GoMySQL - A MySQL client library for Go
//
// Copyright 2010-2011 Phil Bayfield. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysql
import (
"io"
"net"
"os"
)
// Packet writer struct
type writer struct {
conn io.ReadWriteCloser
}
// Create a new reader
func newWriter(conn io.ReadWriteCloser) *writer {
return &writer{
conn: conn,
}
}
// Write packet to the server
func (w *writer) writePacket(p packetWritable) (err os.Error) {
// Deferred error processing
defer func() {
if err != nil {
// EOF errors
if err == os.EOF || err == io.ErrUnexpectedEOF {
err = &ClientError{CR_SERVER_LOST, CR_SERVER_LOST_STR}
}
// OpError
if _, ok := err.(*net.OpError); ok {
err = &ClientError{CR_SERVER_LOST, CR_SERVER_LOST_STR}
}
// Not ClientError, unknown error
if _, ok := err.(*ClientError); !ok {
err = &ClientError{CR_UNKNOWN_ERROR, CR_UNKNOWN_ERROR_STR}
}
}
}()
// Get data in binary format
pktData, err := p.write()
if err != nil {
return
}
// Write packet
nw, err := w.conn.Write(pktData)
if err != nil {
return
}
if nw != len(pktData) {
err = &ClientError{CR_DATA_TRUNCATED, CR_DATA_TRUNCATED_STR}
}
return
}