github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/rpcplugin/main.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package rpcplugin 5 6 import ( 7 "bufio" 8 "encoding/binary" 9 "fmt" 10 "log" 11 "os" 12 ) 13 14 // Makes a set of hooks available via RPC. This function never returns. 15 func Main(hooks interface{}) { 16 ipc, err := InheritedProcessIPC() 17 if err != nil { 18 log.Fatal(err.Error()) 19 } 20 muxer := NewMuxer(ipc, true) 21 id, conn := muxer.Serve() 22 buf := make([]byte, 11) 23 buf[0] = 0 24 n := binary.PutVarint(buf[1:], id) 25 if _, err := muxer.Write(buf[:1+n]); err != nil { 26 log.Fatal(err.Error()) 27 } 28 ServeHooks(hooks, conn, muxer) 29 os.Exit(0) 30 } 31 32 // Returns the hooks being served by a call to Main. 33 func ConnectMain(muxer *Muxer, pluginId string) (*RemoteHooks, error) { 34 buf := make([]byte, 1) 35 if _, err := muxer.Read(buf); err != nil { 36 return nil, err 37 } else if buf[0] != 0 { 38 return nil, fmt.Errorf("unexpected control byte") 39 } 40 reader := bufio.NewReader(muxer) 41 id, err := binary.ReadVarint(reader) 42 if err != nil { 43 return nil, err 44 } 45 46 return ConnectHooks(muxer.Connect(id), muxer, pluginId) 47 }