Skip to content

Commit 6a93875

Browse files
author
Emmanuel Schmidbauer
committed
add SendEvent; use mutex to prevent conflicting events
1 parent eb969c5 commit 6a93875

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

connection.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net"
1414
"strconv"
1515
"strings"
16+
"sync"
1617
"time"
1718
)
1819

@@ -21,6 +22,7 @@ type SocketConnection struct {
2122
net.Conn
2223
err chan error
2324
m chan *Message
25+
mtx sync.Mutex
2426
}
2527

2628
// Dial - Will establish timedout dial against specified address. In this case, it will be freeswitch server
@@ -34,6 +36,9 @@ func (c *SocketConnection) Send(cmd string) error {
3436
if strings.Contains(cmd, "\r\n") {
3537
fmt.Errorf(EInvalidCommandProvided, cmd)
3638
}
39+
// lock mutex
40+
c.mtx.Lock()
41+
defer c.mtx.Unlock()
3742

3843
fmt.Fprintf(c, "%s\r\n\r\n", cmd)
3944

@@ -52,6 +57,25 @@ func (c *SocketConnection) SendMany(cmds []string) error {
5257
return nil
5358
}
5459

60+
// SendEvent - Will loop against passed event headers
61+
func (c *SocketConnection) SendEvent(eventHeaders []string) error {
62+
if len(eventHeaders) <= 0 {
63+
fmt.Errorf(ECouldNotSendEvent, len(eventHeaders))
64+
return nil
65+
}
66+
// lock mutex to prevent event headers from conflicting
67+
c.mtx.Lock()
68+
defer c.mtx.Unlock()
69+
70+
fmt.Fprint(c, "sendevent ")
71+
for _, eventHeader := range eventHeaders {
72+
fmt.Fprintf(c, "%s\r\n", eventHeader)
73+
}
74+
fmt.Fprint(c, "\r\n")
75+
76+
return nil
77+
}
78+
5579
// Execute - Helper fuck to execute commands with its args and sync/async mode
5680
func (c *SocketConnection) Execute(command, args string, sync bool) (m *Message, err error) {
5781
return c.SendMsg(map[string]string{

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ var (
2020
EUnexpectedAuthHeader = "Expected auth/request content type. Got %s"
2121
EInvalidPassword = "Could not authenticate against freeswitch with provided password: %s"
2222
ECouldNotCreateMessage = "Error while creating new message: %s"
23+
ECouldNotSendEvent = "Must send at least one event header, detected `%d` header"
2324
)

0 commit comments

Comments
 (0)