Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipc.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package event
  2. import (
  3. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  4. "sync"
  5. )
  6. // IPCMessage is a wrapper for a regular eventMessage with a destination (onion|AppDest) so the other side of the bridge can route appropriately
  7. type IPCMessage struct {
  8. Dest string
  9. Message Event
  10. }
  11. type pipeBridge struct {
  12. in chan IPCMessage
  13. out chan IPCMessage
  14. closedChan chan bool
  15. closed bool
  16. lock sync.Mutex
  17. }
  18. // IPCBridge is an interface to a IPC construct used to communicate IPCMessages
  19. type IPCBridge interface {
  20. Read() (IPCMessage, bool)
  21. Write(message *IPCMessage)
  22. Shutdown()
  23. }
  24. // MakePipeBridge returns a simple testing IPCBridge made from inprocess go channels
  25. func MakePipeBridge() (b1, b2 IPCBridge) {
  26. chan1 := make(chan IPCMessage)
  27. chan2 := make(chan IPCMessage)
  28. closed := make(chan bool)
  29. a := &pipeBridge{in: chan1, out: chan2, closedChan: closed, closed: false}
  30. b := &pipeBridge{in: chan2, out: chan1, closedChan: closed, closed: false}
  31. go monitor(a, b)
  32. return a, b
  33. }
  34. func monitor(a, b *pipeBridge) {
  35. <-a.closedChan
  36. a.closed = true
  37. b.closed = true
  38. a.closedChan <- true
  39. }
  40. func (pb *pipeBridge) Read() (message IPCMessage, ok bool) {
  41. message, ok = <-pb.in
  42. return
  43. }
  44. func (pb *pipeBridge) Write(message *IPCMessage) {
  45. pb.lock.Lock()
  46. defer pb.lock.Unlock()
  47. log.Infof("pb.Write: %v\n", message)
  48. if !pb.closed {
  49. pb.out <- *message
  50. }
  51. }
  52. func (pb *pipeBridge) Shutdown() {
  53. if !pb.closed {
  54. close(pb.in)
  55. close(pb.out)
  56. pb.closedChan <- true
  57. <-pb.closedChan
  58. }
  59. }