Attachment 'UDP.v1.1.txt'

Download

   1 :Class UDP
   2 ⍝ Class for Asynchronous UDP/IP Communication
   3 
   4 ⍝ List of methods:
   5 ⍝ SendReceive  = Send the bytes and return the bytes received
   6 
   7 ⍝ Version 1.1 January 2016
   8 
   9 ⍝ .Net assemblies:
  10 :Using
  11 :Using System.Net,System.dll
  12 :Using System.Net.Sockets,System.dll
  13 
  14     :Field Public udpObj
  15     :Field Public Type←'UDP'
  16     :Field Public _Retries←3        ⍝ Number of retries if no answer from remote host.
  17     :Field Public _PortTimeOut←250  ⍝ Time in milliseconds to Wait for a UdpClient that is not Active (Receiving Nothing).
  18 
  19     (⎕IO ⎕ML ⎕WX)←1 3 3  ⍝ System variables
  20 
  21     ∇ Init0
  22       :Access Public
  23       :Implements Constructor
  24 
  25 
  26     ∇ r←SendReceive(bytes ip port);data;ipep;retries;task
  27       :Access Public
  28     ⍝ Send the bytes and return the bytes received
  29     ⍝ bytes = Bytes (as Numbers 0 to 255) to be Written to the Communication Port
  30     ⍝ ip    = IP Address where to send the bytes in characters ('192.168.1.1') or numbers (192 168 1 1)
  31     ⍝ port  = Port Number to send the bytes (0 - 65535)
  32 
  33     ⍝ r[1] = 1 for Success, 0 for Failure
  34     ⍝ r[2] = Response if Success, Literal Error if Failure
  35      
  36       :Trap 0
  37         ⍝ Prepare the IP adress:
  38           :If ' '=↑1↑0⍴ip ⍝ Characters ?
  39               ip←IPAddress.Parse(⊂,ip)
  40               ip←⎕NEW IPAddress(ip.Address)
  41      
  42           :Else ⍝ Numbers
  43               ip←⎕NEW IPAddress(⊂,ip)
  44      
  45           :EndIf
  46      
  47         ⍝ Prepare the IPEndPoint
  48           ipep←⎕NEW IPEndPoint(ip port)
  49      
  50           retries←_Retries
  51      
  52         ⍝ Loop until some data is received
  53         ⍝ or the number of retries is exceeded.
  54           :While retries>0
  55      
  56             ⍝ Prepare the UdpClient
  57               udpObj←⎕NEW UdpClient
  58               udpObj.SendTimeout←_PortTimeOut
  59               udpObj.ReceiveTimeout←_PortTimeOut
  60      
  61             ⍝ Send the data asynchronously to the IPEndPoint
  62               task←udpObj.SendAsync(bytes(⍬⍴⍴,bytes)ipep)  ⍝ Prepare the task
  63      
  64               :If 0=⎕TSYNC{task.Wait ⍵}&(_PortTimeOut)
  65                 ⍝ Failure to send all the data.
  66                   r←0 'UDP Error Sending: Task did not complete sending the bytes' ⋄ →RETRIES
  67               :Else
  68                 ⍝ Success
  69                 ⍝ Sending the data seems to work all the time even if the
  70                 ⍝ ethernet cable is unplug from the computer.
  71                   task.Dispose
  72               :EndIf
  73      
  74             ⍝ Receive the data asynchronously
  75               task←udpObj.ReceiveAsync  ⍝ Prepare the task
  76      
  77               :If ⎕TSYNC{task.Wait ⍵}&(_PortTimeOut)
  78                 ⍝ Task completed successfully
  79                   data←task.Result.Buffer
  80                   task.Dispose
  81                   r←1 data
  82               :Else
  83                 ⍝ taskResult=0 when no data is received
  84                 ⍝ Trying to Send again with a udpObj that did
  85                 ⍝ not received does not work. A new udpObj is required.
  86                 ⍝ Hence a new UdpClient object for each retry.
  87                   r←0 'UDP: No Data Received' ⋄ →RETRIES
  88               :EndIf
  89      
  90      RETRIES: retries-←1
  91      
  92              ⍝ Clean-up before trying again.
  93               :Trap 0
  94                   udpObj.Close
  95                   udpObj←⎕NULL
  96               :EndTrap
  97      
  98           :Until 1=1↑r
  99      
 100       :Else
 101         ⍝ Show the error.
 102           :If 90=⎕EN
 103             ⍝ .Net Error
 104               r←0('UDP Error: ',⎕EXCEPTION.GetBaseException.Message) ⋄ →0
 105           :Else
 106             ⍝ APL Error
 107               r←0(1⊃⎕DM),': ',{(' '=1↑⍵)↓((1↓a,0)∨a←' '≠⍵)/⍵}(2⊃⎕DM) ⋄ →0
 108           :EndIf
 109       :EndTrap
 110 
 111 :EndClass

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2016-01-08 12:23:52, 3.3 KB) [[attachment:UDP.v1.0.txt]]
  • [get | view] (2016-01-09 14:35:47, 3.8 KB) [[attachment:UDP.v1.1.txt]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.