Skip to content

Commit

Permalink
Merge pull request #15 from huskar-t/fix/advise
Browse files Browse the repository at this point in the history
fix: advise
  • Loading branch information
huskar-t authored Sep 29, 2024
2 parents 9b5d6f8 + 158ab1c commit 412286b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
10 changes: 5 additions & 5 deletions opcgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ type DataChangeCallBackData struct {

// RegisterDataChange Register to receive data change events
func (g *OPCGroup) RegisterDataChange(ch chan *DataChangeCallBackData) error {
err := g.advice()
err := g.advise()
if err != nil {
return err
}
Expand All @@ -284,7 +284,7 @@ func (g *OPCGroup) RegisterDataChange(ch chan *DataChangeCallBackData) error {

// RegisterReadComplete Register to receive read complete events
func (g *OPCGroup) RegisterReadComplete(ch chan *ReadCompleteCallBackData) error {
err := g.advice()
err := g.advise()
if err != nil {
return err
}
Expand All @@ -294,7 +294,7 @@ func (g *OPCGroup) RegisterReadComplete(ch chan *ReadCompleteCallBackData) error

// RegisterWriteComplete Register to receive write complete events
func (g *OPCGroup) RegisterWriteComplete(ch chan *WriteCompleteCallBackData) error {
err := g.advice()
err := g.advise()
if err != nil {
return err
}
Expand All @@ -304,7 +304,7 @@ func (g *OPCGroup) RegisterWriteComplete(ch chan *WriteCompleteCallBackData) err

// RegisterCancelComplete Register to receive cancel complete events
func (g *OPCGroup) RegisterCancelComplete(ch chan *CancelCompleteCallBackData) error {
err := g.advice()
err := g.advise()
if err != nil {
return err
}
Expand Down Expand Up @@ -337,7 +337,7 @@ type CancelCompleteCallBackData struct {
GroupHandle uint32
}

func (g *OPCGroup) advice() (err error) {
func (g *OPCGroup) advise() (err error) {
g.callbackLock.Lock()
defer g.callbackLock.Unlock()
if g.event != nil {
Expand Down
66 changes: 35 additions & 31 deletions opcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,6 @@ func Connect(progID, node string) (opcServer *OPCServer, err error) {
location: location,
}
opcServer.groups = NewOPCGroups(opcServer)

var iUnknownContainer *com.IUnknown
err = iUnknownServer.QueryInterface(&com.IID_IConnectionPointContainer, unsafe.Pointer(&iUnknownContainer))
if err != nil {
return opcServer, nil
}
defer func() {
if err != nil {
iUnknownContainer.Release()
}
}()
container := &com.IConnectionPointContainer{IUnknown: iUnknownContainer}
point, err := container.FindConnectionPoint(&IID_IOPCShutdown)
if err != nil {
return nil, NewOPCWrapperError("container find connect point", err)
}
defer func() {
if err != nil {
point.Release()
}
}()
event := NewShutdownEventReceiver()
cookie, err := point.Advise((*com.IUnknown)(unsafe.Pointer(event)))
if err != nil {
return nil, NewOPCWrapperError("point advice", err)
}

opcServer.container = container
opcServer.point = point
opcServer.event = event
opcServer.cookie = cookie
return opcServer, nil
}

Expand Down Expand Up @@ -476,6 +445,41 @@ func (s *OPCServer) errors(errs []int32) []error {

// RegisterServerShutDown register server shut down event
func (s *OPCServer) RegisterServerShutDown(ch chan string) error {
if s.event == nil {
var err error
var iUnknownContainer *com.IUnknown
var point *com.IConnectionPoint
var cookie uint32

err = s.iServer.IUnknown.QueryInterface(&com.IID_IConnectionPointContainer, unsafe.Pointer(&iUnknownContainer))
if err != nil {
return NewOPCWrapperError("query interface IConnectionPointContainer", err)
}
defer func() {
if err != nil {
iUnknownContainer.Release()
}
}()
container := &com.IConnectionPointContainer{IUnknown: iUnknownContainer}
point, err = container.FindConnectionPoint(&IID_IOPCShutdown)
if err != nil {
return NewOPCWrapperError("container find connect point", err)
}
defer func() {
if err != nil {
point.Release()
}
}()
event := NewShutdownEventReceiver()
cookie, err = point.Advise((*com.IUnknown)(unsafe.Pointer(event)))
if err != nil {
return NewOPCWrapperError("point advise", err)
}
s.container = container
s.point = point
s.event = event
s.cookie = cookie
}
s.event.AddReceiver(ch)
return nil
}
Expand Down

0 comments on commit 412286b

Please sign in to comment.