12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- // +build windows
- package sipc_conn
- import (
- "fmt"
- "github.com/Microsoft/go-winio"
- uuid "github.com/satori/go.uuid"
- "net"
- "path"
- )
- type ServerCtx struct {
- pipeConfig *winio.PipeConfig
- }
- func GenerateServerName() string {
- return fmt.Sprintf("satoripc-%s", uuid.Must(uuid.NewV4()).String())
- }
- func GeneratePath() string {
- return fmt.Sprintf("\\\\.\\Pipe\\%s", GenerateServerName())
- }
- func GenerateCustomPath(basePath4Unix string, customName string) string {
- return path.Join("\\\\.\\Pipe\\", customName)
- }
- func NewDefaultServer(name string) (*Server, error) {
- var sp string
- if name == "" {
- sp = GeneratePath()
- } else {
- sp = fmt.Sprintf("\\\\.\\Pipe\\%s", name)
- }
- return NewServer(sp, false)
- }
- func NewServer(path string, msgmode bool) (*Server, error) {
- o := &Server{
- serverPath: path,
- isUnix: false,
- connErrorHandler: defaultConnErrHandler,
- connHandler: defaultConnHandler,
- ctx: ServerCtx{
- pipeConfig: &winio.PipeConfig{
- SecurityDescriptor: "",
- MessageMode: msgmode,
- InputBufferSize: 0,
- OutputBufferSize: 0,
- },
- },
- }
- return o, nil
- }
- func (s *Server) Win_UseSecurityDescrptor(sd string) {
- s.ctx.pipeConfig.SecurityDescriptor = sd
- }
- func (s *Server) Win_SetBufferSize(in, out int32) {
- s.ctx.pipeConfig.InputBufferSize = in
- s.ctx.pipeConfig.OutputBufferSize = out
- }
- func (s *Server) GetListenerAndListen() (net.Listener, error) {
- return winio.ListenPipe(s.serverPath, s.ctx.pipeConfig)
- }
- func (s *Server) Start() error {
- var err error
- s.listener, err = winio.ListenPipe(s.serverPath, s.ctx.pipeConfig)
- if err != nil {
- return err
- }
- s.quitChan = make(chan int)
- go s.doAccept()
- return nil
- }
|