Skip to content

Commit 6699575

Browse files
committed
replace Fprintf with io.WriteString in order to expose write errors
1 parent 6a93875 commit 6699575

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

client.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package goesl
99
import (
1010
"bufio"
1111
"fmt"
12+
"io"
1213
"time"
1314
)
1415

@@ -41,30 +42,31 @@ func (c *Client) EstablishConnection() error {
4142
func (c *Client) Authenticate() error {
4243

4344
m, err := newMessage(bufio.NewReaderSize(c, ReadBufferSize), false)
44-
4545
if err != nil {
4646
Error(ECouldNotCreateMessage, err)
4747
return err
4848
}
4949

5050
cmr, err := m.tr.ReadMIMEHeader()
51-
52-
Debug("A: %v %v ", cmr, err)
53-
5451
if err != nil && err.Error() != "EOF" {
5552
Error(ECouldNotReadMIMEHeaders, err)
5653
return err
5754
}
5855

56+
Debug("A: %v\n", cmr)
57+
5958
if cmr.Get("Content-Type") != "auth/request" {
6059
Error(EUnexpectedAuthHeader, cmr.Get("Content-Type"))
6160
return fmt.Errorf(EUnexpectedAuthHeader, cmr.Get("Content-Type"))
6261
}
6362

64-
fmt.Fprintf(c, "auth %s\r\n\r\n", c.Passwd)
63+
s := "auth " + c.Passwd + "\r\n\r\n"
64+
_, err = io.WriteString(c, s)
65+
if err != nil {
66+
return err
67+
}
6568

6669
am, err := m.tr.ReadMIMEHeader()
67-
6870
if err != nil && err.Error() != "EOF" {
6971
Error(ECouldNotReadMIMEHeaders, err)
7072
return err
@@ -80,22 +82,24 @@ func (c *Client) Authenticate() error {
8082

8183
// NewClient - Will initiate new client that will establish connection and attempt to authenticate
8284
// against connected freeswitch server
83-
func NewClient(host string, port uint, passwd string, timeout int) (Client, error) {
85+
func NewClient(host string, port uint, passwd string, timeout int) (*Client, error) {
8486
client := Client{
8587
Proto: "tcp", // Let me know if you ever need this open up lol
8688
Addr: fmt.Sprintf("%s:%d", host, port),
8789
Passwd: passwd,
8890
Timeout: timeout,
8991
}
9092

91-
if err := client.EstablishConnection(); err != nil {
92-
return client, err
93+
err := client.EstablishConnection()
94+
if err != nil {
95+
return nil, err
9396
}
9497

95-
if err := client.Authenticate(); err != nil {
98+
err = client.Authenticate()
99+
if err != nil {
96100
client.Close()
97-
return client, err
101+
return nil, err
98102
}
99103

100-
return client, nil
104+
return &client, nil
101105
}

connection.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bufio"
1111
"bytes"
1212
"fmt"
13+
"io"
1314
"net"
1415
"strconv"
1516
"strings"
@@ -34,13 +35,22 @@ func (c *SocketConnection) Dial(network string, addr string, timeout time.Durati
3435
func (c *SocketConnection) Send(cmd string) error {
3536

3637
if strings.Contains(cmd, "\r\n") {
37-
fmt.Errorf(EInvalidCommandProvided, cmd)
38+
return fmt.Errorf(EInvalidCommandProvided, cmd)
3839
}
40+
3941
// lock mutex
4042
c.mtx.Lock()
4143
defer c.mtx.Unlock()
4244

43-
fmt.Fprintf(c, "%s\r\n\r\n", cmd)
45+
_, err := io.WriteString(c, cmd)
46+
if err != nil {
47+
return err
48+
}
49+
50+
_, err = io.WriteString(c, "\r\n\r\n")
51+
if err != nil {
52+
return err
53+
}
4454

4555
return nil
4656
}
@@ -60,18 +70,35 @@ func (c *SocketConnection) SendMany(cmds []string) error {
6070
// SendEvent - Will loop against passed event headers
6171
func (c *SocketConnection) SendEvent(eventHeaders []string) error {
6272
if len(eventHeaders) <= 0 {
63-
fmt.Errorf(ECouldNotSendEvent, len(eventHeaders))
64-
return nil
73+
return fmt.Errorf(ECouldNotSendEvent, len(eventHeaders))
6574
}
75+
6676
// lock mutex to prevent event headers from conflicting
6777
c.mtx.Lock()
6878
defer c.mtx.Unlock()
6979

70-
fmt.Fprint(c, "sendevent ")
80+
_, err := io.WriteString(c, "sendevent ")
81+
if err != nil {
82+
return err
83+
}
84+
7185
for _, eventHeader := range eventHeaders {
72-
fmt.Fprintf(c, "%s\r\n", eventHeader)
86+
_, err := io.WriteString(c, eventHeader)
87+
if err != nil {
88+
return err
89+
}
90+
91+
_, err = io.WriteString(c, "\r\n")
92+
if err != nil {
93+
return err
94+
}
95+
96+
}
97+
98+
_, err = io.WriteString(c, "\r\n")
99+
if err != nil {
100+
return err
73101
}
74-
fmt.Fprint(c, "\r\n")
75102

76103
return nil
77104
}
@@ -131,9 +158,14 @@ func (c *SocketConnection) SendMsg(msg map[string]string, uuid, data string) (m
131158
b.WriteString(data)
132159
}
133160

134-
if _, err := b.WriteTo(c); err != nil {
161+
// lock mutex
162+
c.mtx.Lock()
163+
_, err = b.WriteTo(c)
164+
if err != nil {
165+
c.mtx.Unlock()
135166
return nil, err
136167
}
168+
c.mtx.Unlock()
137169

138170
select {
139171
case err := <-c.err:

helpers.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
package goesl
88

9-
import "fmt"
10-
119
// Set - Helper that you can use to execute SET application against active ESL session
1210
func (sc *SocketConnection) ExecuteSet(key string, value string, sync bool) (m *Message, err error) {
13-
return sc.Execute("set", fmt.Sprintf("%s=%s", key, value), sync)
11+
return sc.Execute("set", key+"="+value, sync)
1412
}
1513

1614
// ExecuteHangup - Helper desgned to help with executing Answer against active ESL session
@@ -29,12 +27,12 @@ func (sc *SocketConnection) ExecuteHangup(uuid string, args string, sync bool) (
2927

3028
// BgApi - Helper designed to attach api in front of the command so that you do not need to write it
3129
func (sc *SocketConnection) Api(command string) error {
32-
return sc.Send(fmt.Sprintf("api %s", command))
30+
return sc.Send("api " + command)
3331
}
3432

3533
// BgApi - Helper designed to attach bgapi in front of the command so that you do not need to write it
3634
func (sc *SocketConnection) BgApi(command string) error {
37-
return sc.Send(fmt.Sprintf("bgapi %s", command))
35+
return sc.Send("bgapi " + command)
3836
}
3937

4038
// Connect - Helper designed to help you handle connection. Each outbound server when handling needs to connect e.g. accept

0 commit comments

Comments
 (0)