File: n_sharedobj.sru
Size: 7503
Date: Mon, 31 Dec 2018 21:14:39 +0100
$PBExportHeader$n_sharedobj.sru
forward
global type n_sharedobj from n_winsock
end type
end forward

global type n_sharedobj from n_winsock
end type
global n_sharedobj n_sharedobj

type prototypes
Function long SendStringMessage ( &
   long hWnd, &
   uint Msg, &
   Ref string wParam, &
   ulong lParam &
   ) Library "user32.dll" Alias For "SendMessageW"

Function long SendStringMessage ( &
   long hWnd, &
   uint Msg, &
   Ref string wParam, &
   Ref string lParam &
   ) Library "user32.dll" Alias For "SendMessageW"

end prototypes

type variables
Long il_handle
Integer ii_msgevent
Integer ii_stopevent

end variables

forward prototypes
public subroutine of_notify (string as_msg)
public subroutine of_notify (string as_msg, string as_from)
public subroutine of_notifystop (string as_msg)
public subroutine of_registerevents (long al_handle, integer ai_msgevent, integer ai_stopevent)
public subroutine of_recvfromstring (unsignedinteger aui_port, integer ai_ipversion)
public subroutine of_recvfromblob (unsignedinteger aui_port, integer ai_ipversion)
end prototypes

public subroutine of_notify (string as_msg);// -----------------------------------------------------------------------------
// SCRIPT:     of_Notify
//
// PURPOSE:    This function sends a message to the registered object.
//
// ARGUMENTS:  as_msg - Message to send
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

SendStringMessage(il_handle, ii_msgevent, as_msg, 0)

end subroutine

public subroutine of_notify (string as_msg, string as_from);// -----------------------------------------------------------------------------
// SCRIPT:     of_Notify
//
// PURPOSE:    This function sends a message to the registered object.
//
// ARGUMENTS:  as_msg   - Message to send
//             as_from  - IP Address of the sender
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

SendStringMessage(il_handle, ii_msgevent, as_msg, as_from)

end subroutine

public subroutine of_notifystop (string as_msg);// -----------------------------------------------------------------------------
// SCRIPT:     of_NotifyStop
//
// PURPOSE:    This function sends a stop message to the registered object.
//
// ARGUMENTS:  as_msg - Message to send
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

SendStringMessage(il_handle, ii_stopevent, as_msg, 0)

end subroutine

public subroutine of_registerevents (long al_handle, integer ai_msgevent, integer ai_stopevent);// -----------------------------------------------------------------------------
// SCRIPT:     of_RegisterEvents
//
// PURPOSE:    This function saves pointers for sending data.
//
// ARGUMENTS:  al_handle      - Handle of object to receive messages
//             ai_msgevent    - pbm_customxx event to receive messages
//             ai_stopevent   - pbm_customxx event to receive messages
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

Constant Integer WM_USER = 1024

il_handle = al_handle
ii_msgevent  = WM_USER + (ai_msgevent - 1)
ii_stopevent = WM_USER + (ai_stopevent - 1)

end subroutine

public subroutine of_recvfromstring (unsignedinteger aui_port, integer ai_ipversion);// -----------------------------------------------------------------------------
// SCRIPT:     of_RecvFromString
//
// PURPOSE:    This function receives string data via UDP protocol.
//
// ARGUMENTS:  aui_port       - Port to listen on
//             ai_ipversion   - IP Version 4 or 6
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_BytesRecvd, ll_ErrorNum
String ls_buffer, ls_ipaddress, ls_msgtext

// initialize Winsock
this.of_Startup()

// set IP Version
this.of_SetIPVersion(ai_ipversion)

// set a timeout in milliseconds
this.of_SetRecvTimeout(5000)

do while True
   // receive data
   ll_BytesRecvd = this.of_RecvFrom(aui_port, &
                           ls_buffer, ls_ipaddress)
   // check for errors
   If ll_BytesRecvd = SOCKET_ERROR Then
      // get the error number
      ll_ErrorNum = this.of_GetLastErrorNum()
      If ll_ErrorNum = WSAETIMEDOUT Then
         // timeout period expired
      Else
         // handle errors
         ls_msgtext = this.of_GetErrorText(ll_ErrorNum)
         of_NotifyStop(ls_msgtext)
         Exit
      End If
   Else
      // check for data received
      If ll_BytesRecvd > 0 Then
         // check for stop message
         If Lower(ls_buffer) = "stop" Then
            of_NotifyStop("Stopped...")
            Exit
         End If
         // send the message
         of_Notify(ls_buffer, ls_ipaddress)
      End If
   End If
loop

// shutdown Winsock
this.of_Cleanup()

end subroutine

public subroutine of_recvfromblob (unsignedinteger aui_port, integer ai_ipversion);// -----------------------------------------------------------------------------
// SCRIPT:     of_RecvFromBlob
//
// PURPOSE:    This function receives blob data via UDP protocol.
//
// ARGUMENTS:  aui_port       - Port to listen on
//             ai_ipversion   - IP Version 4 or 6
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 09/27/2017  RolandS     Initial coding
// -----------------------------------------------------------------------------

Blob lblb_buffer
Long ll_BytesRecvd, ll_ErrorNum
String ls_ipaddress, ls_msgtext

// initialize Winsock
this.of_Startup()

// set IP Version
this.of_SetIPVersion(ai_ipversion)

// set a timeout in milliseconds
this.of_SetRecvTimeout(5000)

do while True
   // receive data
   ll_BytesRecvd = this.of_RecvFrom(aui_port, &
                           lblb_buffer, ls_ipaddress)
   // check for errors
   If ll_BytesRecvd = SOCKET_ERROR Then
      // get the error number
      ll_ErrorNum = this.of_GetLastErrorNum()
      If ll_ErrorNum = WSAETIMEDOUT Then
         // timeout period expired
      Else
         // handle errors
         ls_msgtext = this.of_GetErrorText(ll_ErrorNum)
         of_NotifyStop(ls_msgtext)
         Exit
      End If
   Else
      // check for data received
      If ll_BytesRecvd > 0 Then
         // check for stop message
         If Lower(String(lblb_buffer)) = "stop" Then
            of_NotifyStop("Stopped...")
            Exit
         End If
         // send the message
         of_Notify("Message from: " + ls_ipaddress)
         of_Notify("Bytes received: " + String(ll_BytesRecvd, "#,##0"))
         // here is where you would add code to do
         // something with the blob data
      End If
   End If
loop

// shutdown Winsock
this.of_Cleanup()

end subroutine

on n_sharedobj.create
call super::create
end on

on n_sharedobj.destroy
call super::destroy
end on