Initial prototyping hackery.
This commit is contained in:
51
socketops/socketops.go
Normal file
51
socketops/socketops.go
Normal file
@ -0,0 +1,51 @@
|
||||
package socketops
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
const bufSize = 1024
|
||||
|
||||
|
||||
func AcceptConn(connstr string) (net.Conn, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", connstr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sock, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := sock.AcceptTCP()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn.SetNoDelay(true)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func HandleConn(conn net.Conn, out chan<- []byte) {
|
||||
buf := make([]byte, bufSize)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
close(out)
|
||||
break
|
||||
}
|
||||
if n > 0 {
|
||||
out <- buf[:n]
|
||||
fmt.Printf("Bytes read: %d\n", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleData(data <-chan []byte) {
|
||||
for {
|
||||
buf, open := <-data
|
||||
if !open {
|
||||
break
|
||||
}
|
||||
fmt.Printf("%v\n", string(buf))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user