forked from 0x19/goesl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
84 lines (64 loc) · 1.59 KB
/
server.go
File metadata and controls
84 lines (64 loc) · 1.59 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2015 Nevio Vesic
// Please check out LICENSE file for more information about what you CAN and what you CANNOT do!
// Basically in short this is a free software for you to do whatever you want to do BUT copyright must be included!
// I didn't write all of this code so you could say it's yours.
// MIT License
package goesl
import (
"context"
"fmt"
"net"
)
type OutboundServer struct {
net.Listener
Addr string `json:"address"`
Proto string
Conns chan *SocketConnection
}
func (s *OutboundServer) Start() error {
Notice("Starting Freeswitch Outbound Server @ (address: %s) ...", s.Addr)
var err error
s.Listener, err = net.Listen(s.Proto, s.Addr)
if err != nil {
Error(ECouldNotStartListener, err)
return err
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
Warning("Waiting for incoming connections ...")
c, err := s.Accept()
if err != nil {
Error(EListenerConnection, err)
cancel()
break
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
conn := &SocketConnection{
Conn: c,
err: make(chan error),
m: make(chan *Message),
ctx: ctx,
cancel: cancel,
}
Notice("Got new connection from: %s", conn.OriginatorAddr())
go conn.Handle()
s.Conns <- conn
}
}()
<-ctx.Done()
s.Close()
return err
}
func NewOutboundServer(addr string) (*OutboundServer, error) {
if addr == "" {
return nil, fmt.Errorf(EInvalidServerAddr, addr)
}
server := OutboundServer{
Addr: addr,
Proto: "tcp",
Conns: make(chan *SocketConnection),
}
return &server, nil
}