server_windows.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // +build windows
  2. package sipc_conn
  3. import (
  4. "fmt"
  5. "github.com/Microsoft/go-winio"
  6. uuid "github.com/satori/go.uuid"
  7. "net"
  8. "path"
  9. )
  10. type ServerCtx struct {
  11. pipeConfig *winio.PipeConfig
  12. }
  13. func GenerateServerName() string {
  14. return fmt.Sprintf("satoripc-%s", uuid.Must(uuid.NewV4()).String())
  15. }
  16. func GeneratePath() string {
  17. return fmt.Sprintf("\\\\.\\Pipe\\%s", GenerateServerName())
  18. }
  19. func GenerateCustomPath(basePath4Unix string, customName string) string {
  20. return path.Join("\\\\.\\Pipe\\", customName)
  21. }
  22. func NewDefaultServer(name string) (*Server, error) {
  23. var sp string
  24. if name == "" {
  25. sp = GeneratePath()
  26. } else {
  27. sp = fmt.Sprintf("\\\\.\\Pipe\\%s", name)
  28. }
  29. return NewServer(sp, false)
  30. }
  31. func NewServer(path string, msgmode bool) (*Server, error) {
  32. o := &Server{
  33. serverPath: path,
  34. isUnix: false,
  35. connErrorHandler: defaultConnErrHandler,
  36. connHandler: defaultConnHandler,
  37. ctx: ServerCtx{
  38. pipeConfig: &winio.PipeConfig{
  39. SecurityDescriptor: "",
  40. MessageMode: msgmode,
  41. InputBufferSize: 0,
  42. OutputBufferSize: 0,
  43. },
  44. },
  45. }
  46. return o, nil
  47. }
  48. func (s *Server) Win_UseSecurityDescrptor(sd string) {
  49. s.ctx.pipeConfig.SecurityDescriptor = sd
  50. }
  51. func (s *Server) Win_SetBufferSize(in, out int32) {
  52. s.ctx.pipeConfig.InputBufferSize = in
  53. s.ctx.pipeConfig.OutputBufferSize = out
  54. }
  55. func (s *Server) GetListenerAndListen() (net.Listener, error) {
  56. return winio.ListenPipe(s.serverPath, s.ctx.pipeConfig)
  57. }
  58. func (s *Server) Start() error {
  59. var err error
  60. s.listener, err = winio.ListenPipe(s.serverPath, s.ctx.pipeConfig)
  61. if err != nil {
  62. return err
  63. }
  64. s.quitChan = make(chan int)
  65. go s.doAccept()
  66. return nil
  67. }