Add quick & dirty prototype for streamer side

This commit is contained in:
Kristóf Tóth 2020-05-04 21:51:41 +02:00
parent a895ee9761
commit ddadf31ff3
4 changed files with 131 additions and 51 deletions

51
connection/connect.go Normal file
View File

@ -0,0 +1,51 @@
package connection
import (
"net"
"fmt"
)
func NewConnect(address string) (*Connection, error) {
conn, err := connectTCP(address)
if err != nil {
return nil, err
}
return &Connection{
conn: conn,
BufSize: defaultBufSize,
stopFlag: false,
}, nil
}
func connectTCP(address string) (*net.TCPConn, error) {
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil, err
}
return conn, nil
}
func (c *Connection) SendChan() (chan<- []byte) {
c.dataChan = make(chan []byte)
go c.sendLoop()
return c.dataChan
}
func (c *Connection) sendLoop() {
for !c.stopFlag {
data, open := <- c.dataChan
if !open {
c.stopFlag = true
}
_, err := c.conn.Write(data)
if err != nil {
fmt.Println(err)
c.stopFlag = true
}
}
}

22
connection/connection.go Normal file
View File

@ -0,0 +1,22 @@
package connection
import (
"net"
)
const defaultBufSize = 1 * 1024
// Connection represents a recv-only network connection.
type Connection struct {
BufSize int
conn *net.TCPConn
dataChan chan []byte
stopFlag bool
}
// Close closes the connection
func (c *Connection) Close() {
c.stopFlag = true
c.conn.Close()
}

View File

@ -2,19 +2,8 @@ package connection
import (
"net"
"io"
)
const defaultBufSize = 1 * 1024
// Connection represents a recv-only network connection.
type Connection struct {
BufSize int
conn *net.TCPConn
recvChan chan []byte
stopFlag bool
}
// NewListen starts listening for a single connection on the given address.
// This method blocks until this is done.
@ -53,15 +42,15 @@ func listenAndAcceptTCP(address string) (*net.TCPConn, error) {
// Use this to read bytes from the network connection.
// The data read is owned by the reader, it is thread-safe to modify.
func (c *Connection) RecvChan() <-chan []byte {
c.recvChan = make(chan []byte)
c.dataChan = make(chan []byte)
go c.recvLoop()
return c.recvChan
return c.dataChan
}
func (c *Connection) recvLoop() {
buf := make([]byte, c.BufSize)
for !c.stopFlag {
n, err := io.ReadFull(c.conn, buf)
n, err := c.conn.Read(buf)
if err != nil {
c.stopFlag = true
}
@ -70,14 +59,8 @@ func (c *Connection) recvLoop() {
// must be copied for thread safety
sendBuf := make([]byte, n)
copy(sendBuf, buf)
c.recvChan <- sendBuf
c.dataChan <- sendBuf
}
}
close(c.recvChan)
}
// Close closes the connection
func (c *Connection) Close() {
c.stopFlag = true
c.conn.Close()
close(c.dataChan)
}

82
main.go
View File

@ -10,33 +10,6 @@ import (
)
func socketExample() {
fmt.Print("Listening for connection on port 8080... ")
conn, err := connection.NewListen(":8080")
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Println("Connection accepted!")
recvChan := conn.RecvChan()
go func() {
time.Sleep(10 * time.Second)
conn.Close()
}()
for {
buf, open := <-recvChan
if !open {
break
}
fmt.Printf("Bytes read: %d\n", len(buf))
fmt.Printf("Buf: %v\n", buf)
fmt.Printf("%v\n", string(buf))
}
fmt.Println("Exiting.")
}
func pulsectlExample() {
pulsectl := audio.NewPulsectl()
err := pulsectl.LoadPipeSourceModule()
@ -63,6 +36,57 @@ func micStreamExample() {
}
}
func main() {
socketExample()
func usage() {
fmt.Println("Usage: remote-mic <command>")
os.Exit(1)
}
func listen() {
fmt.Print("Listening for connection on port 8080... ")
conn, err := connection.NewListen(":8080")
if err != nil {
panic(err)
}
fmt.Println("Connection accepted!")
recvChan := conn.RecvChan()
for {
buf, open := <-recvChan
if !open {
break
}
fmt.Printf("Bytes read: %d\n", len(buf))
fmt.Printf("Buf: %v\n", buf)
fmt.Printf("%v\n", string(buf))
}
}
func stream() {
conn2, err := connection.NewConnect("localhost:8080")
if err != nil {
fmt.Println(err)
panic(err)
}
c := conn2.SendChan()
c <- []byte("cicasajtok")
time.Sleep(20 * time.Second)
fmt.Println("Exiting.")
}
func main() {
if len(os.Args) < 2 {
usage()
}
cmd := os.Args[1]
switch cmd {
case "listen":
listen()
case "stream":
stream()
default:
usage()
}
}