本文整理汇总了Golang中net.ListenUnixgram函数的典型用法代码示例。如果您正苦于以下问题:Golang ListenUnixgram函数的具体用法?Golang ListenUnixgram怎么用?Golang ListenUnixgram使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ListenUnixgram函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: unixgram_coll
func unixgram_coll(_, address string) (Listener, error) {
var err error
r := new(unixgram_receiver)
r.network = "unixgram"
r.address = address
r.end = make(chan struct{})
r.listener, err = net.ListenUnixgram("unixgram", &net.UnixAddr{address, "unixgram"})
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
return nil, err
}
default:
return nil, err
}
if _, r_err := os.Stat(address); r_err != nil {
return nil, err
}
os.Remove(address)
r.listener, err = net.ListenUnixgram("unixgram", &net.UnixAddr{address, "unixgram"})
}
return r, nil
}
示例2: Run
func (jrnl *JournalReader) Run(dest chan<- Message, errchan chan<- error) {
jrnl.end = make(chan bool, 1)
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{jrnl.Journald, "unixgram"})
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
errchan <- &InputError{jrnl.Driver, jrnl.Id, "listen " + jrnl.Journald, err}
return
}
default:
errchan <- &InputError{jrnl.Driver, jrnl.Id, "listen " + jrnl.Journald, err}
return
}
if _, r_err := os.Stat(jrnl.Journald); r_err != nil {
errchan <- &InputError{jrnl.Driver, jrnl.Id, "lstat " + jrnl.Journald, err}
return
}
os.Remove(jrnl.Journald)
conn, err = net.ListenUnixgram("unixgram", &net.UnixAddr{jrnl.Journald, "unixgram"})
}
defer conn.Close()
for {
select {
case <-jrnl.end:
return
default:
buffer := make([]byte, 65536)
_, _, err := conn.ReadFrom(buffer)
if err != nil {
errchan <- &InputError{jrnl.Driver, jrnl.Id, "ReadFrom " + jrnl.Journald, err}
return
}
line := string(bytes.TrimRight(buffer, "\t \n\r\000"))
if line == "" {
continue
}
pos := strings.Index(line, ">")
if pos > 0 && unicode.IsDigit(rune(line[pos+1])) {
dest <- packmsg(jrnl.Id, *message.ParseMessage_5424(line))
} else {
dest <- packmsg(jrnl.Id, *message.ParseMessage_3164(line))
}
}
}
}
示例3: Listen
// Listen starts gorutine that receives syslog messages on specified address.
// addr can be a path (for unix domain sockets) or host:port (for UDP).
func (s *Server) Listen(addr string) error {
var c net.PacketConn
if strings.IndexRune(addr, ':') != -1 {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
c, err = net.ListenUDP("udp", a)
if err != nil {
return err
}
} else {
a, err := net.ResolveUnixAddr("unixgram", addr)
if err != nil {
return err
}
c, err = net.ListenUnixgram("unixgram", a)
if err != nil {
return err
}
}
s.conns = append(s.conns, c)
go s.receiver(c)
return nil
}
示例4: Init
func (haProxy *HAProxy) Init() {
if len(*logstash) == 0 {
logger.Notice("No Logstash host:port set - not sending HAProxy logs.")
return
}
logger.Info(fmt.Sprintf("Connecting to haproxy log socket: %s", haProxy.LogSocket))
// Set the socket HAProxy can write logs to.
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{haProxy.LogSocket, "unixgram"})
if err != nil {
logger.Error("Error while connecting to haproxy log socket: ", err.Error())
return
}
logger.Info(fmt.Sprintf("Opened Unix socket at: %s. Creating Logstash sender.", haProxy.LogSocket))
logstash := Logstash{
Address: *logstash,
Reader: conn,
}
logstash.Pipe()
}
示例5: newProxy
func newProxy(src, dst string) (*unixProxy, error) {
os.Remove(src)
// start listening
local, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: src,
Net: "unixgram",
})
if err != nil {
return nil, err
}
remote, err := net.DialUnix("unixgram", nil, &net.UnixAddr{
Name: dst,
Net: "unixgram",
})
if err != nil {
return nil, err
}
return &unixProxy{
local: local,
remote: remote,
}, nil
}
示例6: main
func main() {
path := "/tmp/example.sock"
addr, err := net.ResolveUnixAddr("unixgram", path)
if err != nil {
fmt.Printf("ResolveUnixAddr err: %s\n", err)
return
}
conn, err := net.ListenUnixgram("unixgram", addr)
if err != nil {
fmt.Printf("ListenUnixgram err: %s\n", err)
return
}
// Ensure world writable access
os.Chmod(path, 0666)
data := make([]byte, 4096)
length, _, err := conn.ReadFrom(data)
if err != nil {
fmt.Printf("conn.ReadFrom error: %s\n", err)
return
}
fmt.Printf("Got: %d bytes\n", length)
}
示例7: setupSockets
// setupSockets sets up the sockets used to communicate between the client
// and the server. If successful it spins up another goroutine to handle
// all communication from that client.
func (s *Server) setupSockets(remoteID string) {
log.Infof("server: dialing client socket")
out, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), remoteID), udsType},
)
if err != nil {
log.Infof("problem dialing client's socket: %s", err)
return
}
log.Infof("server: preparing to listen on new socket")
uid := uuid.New()
p := path.Join(os.TempDir(), uid)
in, err := net.ListenUnixgram(udsType, &net.UnixAddr{p, udsType})
if err != nil {
out.Close()
log.Infof("could not listen on domain socket %q: %s", p, err)
return
}
log.Infof("server: sending a uid to the client")
if err := SetupEncode(uid, out); err != nil {
out.Close()
log.Infof("problem encoding UUIDv4 for setup: %s", err)
return
}
go s.serveConn(in, out)
}
示例8: setUnixSocket
func setUnixSocket(c *C) *net.UnixConn {
os.Remove("/tmp/vulcand_trace_test.sock")
unixAddr, err := net.ResolveUnixAddr("unixgram", "/tmp/vulcand_trace_test.sock")
c.Assert(err, IsNil)
conn, err := net.ListenUnixgram("unixgram", unixAddr)
c.Assert(err, IsNil)
return conn
}
示例9: DialServer
// DialServer dials a Unix Domain Socket where a server is listening and
// returns a client to the server.
func DialServer(uid string) (*Client, error) {
// Have the client start listening for responses from the server.
clientUID := uuid.New()
clientPath := path.Join(os.TempDir(), clientUID)
outConn, err := net.ListenUnixgram(udsType, &net.UnixAddr{clientPath, udsType})
if err != nil {
return nil, err
}
// Dial the server.
log.Infof("client: dialing the server")
setupConn, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), uid), udsType},
)
if err != nil {
return nil, err
}
log.Infof("client: sending uid to server")
if err := SetupEncode(clientUID, setupConn); err != nil {
return nil, err
}
setupConn.Close()
// Get the socket the server is going to listen on.
out := bufio.NewReader(outConn)
inUUID, err := SetupDecode(out)
if err != nil {
return nil, err
}
log.Infof("client: received server uid for conn")
// Dial the server.
in, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), inUUID), udsType},
)
if err != nil {
return nil, err
}
log.Infof("client: dialed server")
c := &Client{
inConn: in,
outConn: out,
reqCh: make(chan request, 50),
responses: make(map[uint64]chan response),
}
go c.send()
go c.receive()
return c, nil
}
示例10: BenchmarkSend
func BenchmarkSend(b *testing.B) {
conn, _ := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: "@dummy", Net: "unixgram"})
go io.Copy(ioutil.Discard, conn)
defer conn.Close()
h := &Handle{path: "@dummy"}
for i := 0; i < b.N; i++ {
h.Send("MESSAGE=hello world")
}
}
示例11: logListener
func logListener(socket string) {
log.Info("Starting log listener")
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{socket, "unixgram"})
if err != nil {
log.Fatal("listen error:", err)
}
go logPrinter(conn)
}
示例12: TestSdNotify
// TestSdNotify
func TestSdNotify(t *testing.T) {
testDir, e := ioutil.TempDir("/tmp/", "test-")
if e != nil {
panic(e)
}
defer os.RemoveAll(testDir)
notifySocket := testDir + "/notify-socket.sock"
laddr := net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}
_, e = net.ListenUnixgram("unixgram", &laddr)
if e != nil {
panic(e)
}
tests := []struct {
unsetEnv bool
envSocket string
wsent bool
werr bool
}{
// (true, nil) - notification supported, data has been sent
{false, notifySocket, true, false},
// (false, err) - notification supported, but failure happened
{true, testDir + "/missing.sock", false, true},
// (false, nil) - notification not supported
{true, "", false, false},
}
for i, tt := range tests {
must(os.Unsetenv("NOTIFY_SOCKET"))
if tt.envSocket != "" {
must(os.Setenv("NOTIFY_SOCKET", tt.envSocket))
}
sent, err := SdNotify(tt.unsetEnv, fmt.Sprintf("TestSdNotify test message #%d", i))
if sent != tt.wsent {
t.Errorf("#%d: expected send result %t, got %t", i, tt.wsent, sent)
}
if tt.werr && err == nil {
t.Errorf("#%d: want non-nil err, got nil", i)
} else if !tt.werr && err != nil {
t.Errorf("#%d: want nil err, got %v", i, err)
}
if tt.unsetEnv && tt.envSocket != "" && os.Getenv("NOTIFY_SOCKET") != "" {
t.Errorf("#%d: environment variable not cleaned up", i)
}
}
}
示例13: NewServer
// New is the constructor for Server.
func NewServer(setupSocket string) (*Server, error) {
p := path.Join(os.TempDir(), setupSocket)
conn, err := net.ListenUnixgram(udsType, &net.UnixAddr{p, udsType})
if err != nil {
return nil, fmt.Errorf("could not listen on domain socket %q: %s", setupSocket, err)
}
s := &Server{setupConn: conn, registry: make(map[string]*register, 1)}
return s, nil
}
示例14: TestSdNotify
// TestSdNotify
func TestSdNotify(t *testing.T) {
notificationSupportedDataSent := "Notification supported, data sent"
notificationSupportedFailure := "Notification supported, but failure happened"
notificationNotSupported := "Notification not supported"
testDir, e := ioutil.TempDir("/tmp/", "test-")
if e != nil {
panic(e)
}
defer os.RemoveAll(testDir)
notifySocket := testDir + "/notify-socket.sock"
laddr := net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}
_, e = net.ListenUnixgram("unixgram", &laddr)
if e != nil {
panic(e)
}
// (true, nil) - notification supported, data has been sent
e = os.Setenv("NOTIFY_SOCKET", notifySocket)
if e != nil {
panic(e)
}
sent, err := SdNotify(notificationSupportedDataSent)
if !sent || err != nil {
t.Errorf("TEST: %s FAILED", notificationSupportedDataSent)
}
// (false, err) - notification supported, but failure happened
e = os.Setenv("NOTIFY_SOCKET", testDir+"/not-exist.sock")
if e != nil {
panic(e)
}
sent, err = SdNotify(notificationSupportedFailure)
if sent && err == nil {
t.Errorf("TEST: %s FAILED", notificationSupportedFailure)
}
// (false, nil) - notification not supported
e = os.Unsetenv("NOTIFY_SOCKET")
if e != nil {
panic(e)
}
sent, err = SdNotify(notificationNotSupported)
if sent || err != nil {
t.Errorf("TEST: %s FAILED", notificationNotSupported)
}
}
示例15: Run
func (jrnl *JournalReader) Run(dest chan<- Message) {
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{jrnl.Journald, "unixgram"})
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
jrnl.errchan <- &InputError{jrnl.Driver, jrnl.Id, "listen " + jrnl.Journald, err}
return
}
default:
jrnl.errchan <- &InputError{jrnl.Driver, jrnl.Id, "listen " + jrnl.Journald, err}
return
}
if _, r_err := os.Stat(jrnl.Journald); r_err != nil {
jrnl.errchan <- &InputError{jrnl.Driver, jrnl.Id, "lstat " + jrnl.Journald, err}
return
}
os.Remove(jrnl.Journald)
conn, err = net.ListenUnixgram("unixgram", &net.UnixAddr{jrnl.Journald, "unixgram"})
}
defer conn.Close()
for {
select {
case <-jrnl.end:
return
default:
jrnl.cope_with(conn, make([]byte, 65536), dest)
}
}
}