File: u_tabpg_tcp_listen.sru
Size: 11771
Date: Mon, 31 Dec 2018 21:14:39 +0100
$PBExportHeader$u_tabpg_tcp_listen.sru
$PBExportComments$Base tabpage object
forward
global type u_tabpg_tcp_listen from u_tabpg
end type
type cb_listenblob from commandbutton within u_tabpg_tcp_listen
end type
type cb_reset from commandbutton within u_tabpg_tcp_listen
end type
type cb_stop from commandbutton within u_tabpg_tcp_listen
end type
type cb_listen from commandbutton within u_tabpg_tcp_listen
end type
type lb_msgs from listbox within u_tabpg_tcp_listen
end type
type st_2 from statictext within u_tabpg_tcp_listen
end type
type st_1 from statictext within u_tabpg_tcp_listen
end type
type sle_port from singlelineedit within u_tabpg_tcp_listen
end type
end forward

global type u_tabpg_tcp_listen from u_tabpg
string text = "TCP Listen"
event ue_listenstring pbm_custom01
event ue_listenblob pbm_custom02
cb_listenblob cb_listenblob
cb_reset cb_reset
cb_stop cb_stop
cb_listen cb_listen
lb_msgs lb_msgs
st_2 st_2
st_1 st_1
sle_port sle_port
end type
global u_tabpg_tcp_listen u_tabpg_tcp_listen

type variables
// for the ue_listen events
Constant Long FD_READ      = 1
Constant Long FD_WRITE     = 2
Constant Long FD_OOB       = 4
Constant Long FD_ACCEPT    = 8
Constant Long FD_CONNECT   = 10
Constant Long FD_CLOSE     = 32

ULong iul_socket
ULong iul_listen
Boolean ib_Running

// Listen For Blob
Boolean ib_ReceivedFirst
Long il_BytesExpected
Long il_BytesReceived
String is_folder
String is_filename

end variables

event ue_listenstring;// listen for messages

Long ll_BytesRecvd
String ls_ipaddress, ls_message
UInt lui_peerport

choose case lparam
   case FD_ACCEPT
      // accept the incoming socket
      iul_listen = gn_ws.of_Accept(iul_socket)
      If iul_listen > 0 Then
         gn_ws.of_GetPeerName(iul_listen, ls_ipaddress, lui_peerport)
         lb_msgs.AddItem("Incoming IP Address: " + ls_ipaddress)
         lb_msgs.AddItem("Peer Port: " + String(lui_peerport))
      Else
         lb_msgs.AddItem("of_Accept Error: " + gn_ws.of_GetLastError())
      End If
   case FD_READ
      // read data from the incoming socket
      ll_BytesRecvd = gn_ws.of_Recv(iul_listen, ls_message)
      If ll_BytesRecvd > 0 Then
         lb_msgs.AddItem(ls_message)
      Else
         lb_msgs.AddItem("of_Recv Error: " + gn_ws.of_GetLastError())
      End If
      // let the sender know we got the message
      gn_ws.of_Send(iul_listen, &
         "Bytes received: " + String(ll_BytesRecvd, "#,##0"))
      // close the incoming socket
      gn_ws.of_CloseSocket(iul_listen)
end choose

end event

event ue_listenblob;// listen for messages

Blob lblb_filedata
Integer li_fnum, li_hdrlen
Long ll_BytesRecvd
String ls_ipaddress, ls_header
String ls_datalen, ls_filename
UInt lui_peerport

choose case lparam
   case FD_ACCEPT
      // accept the incoming socket
      iul_listen = gn_ws.of_Accept(iul_socket)
      If iul_listen > 0 Then
         gn_ws.of_GetPeerName(iul_listen, ls_ipaddress, lui_peerport)
         lb_msgs.AddItem("Incoming IP Address: " + ls_ipaddress)
         lb_msgs.AddItem("Peer Port: " + String(lui_peerport))
      Else
         lb_msgs.AddItem("of_Accept Error: " + &
                        gn_ws.of_GetLastError())
      End If
   case FD_READ
      // read data from the incoming socket
      ll_BytesRecvd = gn_ws.of_Recv(iul_listen, lblb_filedata)
      If ll_BytesRecvd > -1 Then
         If Not ib_ReceivedFirst Then
            ib_ReceivedFirst = True

            // parse out the header
            ls_header = String(lblb_filedata)

            // parse out the datalength
            ls_datalen = Left(ls_header, Pos(ls_header, "|") - 1)
            li_hdrlen = Len(ls_datalen) + 1
            il_BytesExpected = Long(ls_datalen)
            lb_msgs.AddItem("Bytes Expected: " + &
                              String(il_BytesExpected))

            // parse out the filename
            ls_filename = Mid(ls_header, Pos(ls_header, "|") + 1)
            li_hdrlen += Len(ls_filename)
            ls_filename = Left(ls_filename, Pos(ls_filename, "|") - 1)
            lb_msgs.AddItem("File Expected: " + ls_filename)

            // build full filename and delete existing
            is_filename = is_folder + "\" + ls_filename
            FileDelete(is_filename)

            // remove the header from the blob
            li_hdrlen = (li_hdrlen * 2) // Unicode strings are 2 bytes
            lblb_filedata = BlobMid(lblb_filedata, li_hdrlen + 1)
         End If
         If Len(lblb_filedata) > 0 Then
            // update the byte counter
            il_BytesReceived = il_BytesReceived + Len(lblb_filedata)

            // write the data to the file
            // use append mode in case file sent in parts
            li_fnum = FileOpen(is_FileName, StreamMode!, &
                                 Write!, Shared!, Append!)
            If li_fnum > 0 Then
               FileWrite(li_fnum, lblb_filedata)
               FileClose(li_fnum)
            Else
               lb_msgs.AddItem("FileOpen Failed!")
            End If

            // check to see if done
            If il_BytesReceived >= il_BytesExpected Then
               lb_msgs.AddItem("Transfer is Complete!")

               // let the sender know we got the message
               gn_ws.of_Send(iul_listen, &
                  "Bytes received: " + String(il_BytesReceived, "#,##0"))

               // close the incoming socket
               gn_ws.of_CloseSocket(iul_listen)
            End If
         End if
      Else
         lb_msgs.AddItem("of_Recv Error: " + gn_ws.of_GetLastError())
      End If
end choose

end event

on u_tabpg_tcp_listen.create
int iCurrent
call super::create
this.cb_listenblob=create cb_listenblob
this.cb_reset=create cb_reset
this.cb_stop=create cb_stop
this.cb_listen=create cb_listen
this.lb_msgs=create lb_msgs
this.st_2=create st_2
this.st_1=create st_1
this.sle_port=create sle_port
iCurrent=UpperBound(this.Control)
this.Control[iCurrent+1]=this.cb_listenblob
this.Control[iCurrent+2]=this.cb_reset
this.Control[iCurrent+3]=this.cb_stop
this.Control[iCurrent+4]=this.cb_listen
this.Control[iCurrent+5]=this.lb_msgs
this.Control[iCurrent+6]=this.st_2
this.Control[iCurrent+7]=this.st_1
this.Control[iCurrent+8]=this.sle_port
end on

on u_tabpg_tcp_listen.destroy
call super::destroy
destroy(this.cb_listenblob)
destroy(this.cb_reset)
destroy(this.cb_stop)
destroy(this.cb_listen)
destroy(this.lb_msgs)
destroy(this.st_2)
destroy(this.st_1)
destroy(this.sle_port)
end on

event ue_pagechanged;call super::ue_pagechanged;sle_port.SetFocus()

end event

event destructor;call super::destructor;of_setreg("tcp_listen_port", sle_port.text)

end event

event ue_postopen;call super::ue_postopen;sle_port.text = of_getreg("tcp_listen_port", "")

end event

type cb_listenblob from commandbutton within u_tabpg_tcp_listen
integer x = 37
integer y = 416
integer width = 480
integer height = 100
integer taborder = 30
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Listen for a Blob"
end type

event clicked;// start listening

Integer li_result
UInt lui_port

SetPointer(HourGlass!)

// make sure Port is specified
lui_port = Long(sle_port.text)
If lui_port = 0 Then
   sle_port.SetFocus()
   MessageBox("Edit Error", "Server Port is required!")
   Return
End If

// get folder to save the file to
li_result = GetFolder("Choose a folder to save incoming files", &
                     is_folder)
If li_result < 1 Then Return

// disable controls
sle_port.Enabled = False
cb_listen.Enabled = False
cb_listenblob.Enabled = False
cb_stop.Enabled = True

// reset instance variables
ib_ReceivedFirst = False
il_BytesReceived = 0
is_FileName = ""

// create a listening socket (event ue_listenblob)
iul_socket = gn_ws.of_Listen(lui_port, Handle(Parent), 2)
If iul_socket = 0 Then
   MessageBox("Winsock Error in of_Listen", gn_ws.of_GetLastError())
   Return
End If

gb_Listening = True

lb_msgs.Reset()
lb_msgs.AddItem("Listening...")
lb_msgs.AddItem("Save folder: " + is_folder)

end event

type cb_reset from commandbutton within u_tabpg_tcp_listen
integer x = 951
integer y = 1248
integer width = 334
integer height = 100
integer taborder = 50
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Reset Msgs"
end type

event clicked;lb_msgs.Reset()

end event

type cb_stop from commandbutton within u_tabpg_tcp_listen
integer x = 37
integer y = 576
integer width = 480
integer height = 100
integer taborder = 40
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
boolean enabled = false
string text = "Stop Listening"
end type

event clicked;// stop listening

SetPointer(HourGlass!)

// close the listening socket
gn_ws.of_CloseSocket(iul_socket)

// enable controls
sle_port.Enabled = True
cb_listen.Enabled = True
cb_listenblob.Enabled = True
cb_stop.Enabled = False

gb_Listening = False

lb_msgs.AddItem("Stopped...")

end event

type cb_listen from commandbutton within u_tabpg_tcp_listen
integer x = 37
integer y = 256
integer width = 480
integer height = 100
integer taborder = 20
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Listen for a String"
end type

event clicked;// start listening

UInt lui_port

SetPointer(HourGlass!)

// make sure Port is specified
lui_port = Long(sle_port.text)
If lui_port = 0 Then
   sle_port.SetFocus()
   MessageBox("Edit Error", "Server Port is required!")
   Return
End If

// disable controls
sle_port.Enabled = False
cb_listen.Enabled = False
cb_listenblob.Enabled = False
cb_stop.Enabled = True

// create a listening socket (event ue_listen)
gn_ws.of_SetUnicode(w_main.cbx_setunicode.checked)
iul_socket = gn_ws.of_Listen(lui_port, Handle(Parent), 1)
If iul_socket = 0 Then
   MessageBox("Winsock Error in of_Listen", gn_ws.of_GetLastError())
   Return
End If

gb_Listening = True

lb_msgs.Reset()
lb_msgs.AddItem("Listening...")

end event

type lb_msgs from listbox within u_tabpg_tcp_listen
integer x = 1353
integer y = 96
integer width = 1541
integer height = 1252
integer taborder = 60
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
boolean hscrollbar = true
boolean vscrollbar = true
boolean sorted = false
borderstyle borderstyle = stylelowered!
end type

type st_2 from statictext within u_tabpg_tcp_listen
integer x = 1353
integer y = 32
integer width = 283
integer height = 60
integer textsize = -8
integer weight = 700
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "Messages:"
boolean focusrectangle = false
end type

type st_1 from statictext within u_tabpg_tcp_listen
integer x = 37
integer y = 108
integer width = 151
integer height = 60
integer textsize = -8
integer weight = 700
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "Port:"
boolean focusrectangle = false
end type

type sle_port from singlelineedit within u_tabpg_tcp_listen
integer x = 219
integer y = 96
integer width = 224
integer height = 80
integer taborder = 10
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
integer limit = 5
borderstyle borderstyle = stylelowered!
end type