Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: client/internal/logdog/butler/streamserver/npipe_posix.go

Issue 1429993002: LogDog: Add Butler stream server package. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-butlerproto
Patch Set: Cleanup, comments. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // +build darwin dragonfly freebsd linux netbsd openbsd
6
7 package streamserver
8
9 import (
10 "fmt"
11 "net"
12 "os"
13
14 log "github.com/luci/luci-go/common/logging"
15 "golang.org/x/net/context"
16 )
17
18 type namedPipeServer struct {
19 namedPipeServerBase
20
21 path string // The path on the filesystem of the UNIX domain named socke t
22 }
23
24 func (s *namedPipeServer) GoString() string {
25 return fmt.Sprintf("namedPipeServer[%s]", s.path)
26 }
27
28 // Wrapper around the "unix"-type Listener that cleans up the named pipe on
29 // creation and 'Close()'
30 type selfCleaningUNIXListener struct {
31 path string
32 listener net.Listener
33 }
34
35 // NewNamedPipeServer instantiates a new POSIX named pipe server instance.
36 func NewNamedPipeServer(ctx context.Context, path string) StreamServer {
37 s := &namedPipeServer{
38 path: path,
39 }
40 s.initBase(ctx)
41 return s
42 }
43
44 // OS-specific override to create a UNIX named socket listener
45 func (s *namedPipeServer) createListener() (net.Listener, error) {
46 log.Fields{
47 "path": s.path,
48 }.Infof(s.ctx, "Creating POSIX server socket Listener.")
49 return newSelfCleaningUNIXListener(s.path)
50 }
51
52 //
53 // selfCleaningUNIXListener
54 //
55
56 func newSelfCleaningUNIXListener(path string) (net.Listener, error) {
57 // Cleanup any previous entry
58 os.Remove(path)
iannucci 2015/11/12 22:44:24 error?
dnj 2015/11/13 20:17:15 Done.
59
60 // Create a UNIX listener
61 listener, err := net.Listen("unix", path)
62 if err != nil {
63 return nil, err
64 }
65
66 l := selfCleaningUNIXListener{
67 path: path,
68 listener: listener,
69 }
70 return &l, nil
71 }
72
73 // Implements net.Listner.Accept
74 func (l *selfCleaningUNIXListener) Accept() (c net.Conn, err error) {
75 return l.listener.Accept()
76 }
77
78 // Implements net.Listener.Close
79 func (l *selfCleaningUNIXListener) Close() error {
80 if err := l.listener.Close(); err != nil {
81 return err
82 }
83
84 os.Remove(l.path)
85 return nil
86 }
87
88 // Implements net.Listener.Addr
89 func (l *selfCleaningUNIXListener) Addr() net.Addr {
90 return l.listener.Addr()
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698