quarta-feira, 27 de novembro de 2013

Socket Server para Linux - server.w ( Datasul )


/* *************************** Definitions ************************** */

/* Parameters Definitions --- */

/* Local Variable Definitions --- */

define variable hServerSocket  as handle.
define variable mBuffer        as memptr.
define variable cString        as character no-undo format "x(2000)":U.
define variable hSocket        as handle    no-undo.
define variable cConfig        as character no-undo extent 4.
define variable cConnectString as character no-undo.
define variable hStatus        as handle    no-undo.
define variable vinItem        as integer   no-undo.
define variable vinIndex       as integer   no-undo.
define variable vchDbase       as character no-undo format "x(200)":U.
define variable vchID          as character no-undo format "x(20)":U.
define variable vchLine        as character no-undo format "x(500)":U.

define stream stm-buffer.

/* ************************  Function Prototypes ********************** */

function getConfig returns logical () forward.

/* ***********************  Control Definitions  ********************** */

if search("linux/server.cfg") = ? then do:
  message
    "O arquivo de configura‡Æo do servidor nÆo foi encontrado.":U skip(1)
    "Exemplo: linux/server.cfg":U skip
    "-H 192.168.0.1":U skip
    "-N tcp":U skip
    "-S 3333":U skip
    "-P /DLC9/bin":U.
  quit.
end.

if not getConfig() then do:
  message
    "O servidor nÆo foi configurado corretamente.":U skip(1)
    "Exemplo: linux/server.cfg":U skip
    "-H 192.168.0.1":U skip
    "-N tcp":U skip
    "-S 3333":U skip
    "-P /DLC9/bin":U.
  quit.
end.

cConnectString = "-H ":U + cConfig[1] + " -N ":U + cConfig[2] + " -S ":U + cConfig[3].

if not valid-handle(hServerSocket) then do:
  create server-socket hServerSocket no-error.
  hServerSocket:set-connect-procedure("connProc":U) no-error.
  if hServerSocket:enable-connections(cConnectString) then do:
    repeat:
      process events.
      readkey pause 0.
      if keylabel(lastkey) = "CTRL-C":U then
        leave.

      if valid-handle(hSocket) then do:
        if hSocket:get-bytes-available() > 0 then do:
          set-size(mBuffer) = 2000.
          hSocket:read(mBuffer,1,hSocket:get-bytes-available()).
          cString = trim(get-string(mBuffer,1)).
          bell.

          if index(cString,"<connect>":U) > 0 then do:
            output to linux/connect.log append.
            put control replace(replace(cString,"<connect>":U,""),"…":U,"a").
            put skip(1).
            output close.
          end.
          else do:
            if index(cString,"<disconnect>":U) > 0 then do:
              vchDBase = replace(replace(cString,"<disconnect>":U,""),"…":U,"a").
              os-copy linux/connect.log linux/connect_buffer.log.
              output to linux/connect.log.
              input stream stm-buffer from linux/connect_buffer.log.
              repeat:
                import stream stm-buffer unformatted vchLine.
                if substring(vchLine,1,(index(vchLine,"logado") - 1)) <>    
                   substring(vchDBase,1,(index(vchDBase,"logado") - 1)) then do:
                  put control vchLine.
                  put skip(1).
                end.
              end.
              input stream stm-buffer close.
              output close.
              os-delete linux/connect_buffer.log.
            end.
            else do:
              cString = replace(cString,"<killer>":U,"").
              output to ./proshut.bat.
              do vinItem = 1 to num-entries(cString):
                vchDBase = trim(entry(1,entry(vinItem,cString),"@":U)).
                vchID    = trim(entry(2,entry(vinItem,cString),"@":U)).
                put control cConfig[4] "_mprshut ":U vchDBase " -C Disconnect ":U vchID.
                put skip(1).
              end.
              output close.

              os-command silent sh ./proshut.bat > ./proshut.log.
              os-delete silent ./proshut.bat.
              os-delete silent ./proshut.log.
            end.
          end.
        end.
      end.
    end.
  end.

  hServerSocket:disable-connections().
  if valid-handle(hServerSocket) then
    delete object hServerSocket no-error.
end.

if search("linux/connect.log") <> ? then
  os-delete silent linux/connect.log.

quit.

/* ************************  Procedure Implementations ***************** */

procedure connProc:
  define input parameter iphSocket as handle.
  assign hSocket = iphSocket.
end procedure.

/* ************************  Function Implementations ***************** */

function getConfig returns logical
  ( /* parameter-definitions */ ) :
  define variable cLine as character no-undo.
  define variable cFile as character no-undo.

  cFile = search("linux/server.cfg").
  input from value(cFile).

  cConfig = "".
  repeat:
    import unformatted cLine.
    if substring(cLine,1,2) = "-H" then cConfig[1] = substring(cLine,3,length(cLine)).
    if substring(cLine,1,2) = "-N" then cConfig[2] = substring(cLine,3,length(cLine)).
    if substring(cLine,1,2) = "-S" then cConfig[3] = substring(cLine,3,length(cLine)).
    if substring(cLine,1,2) = "-P" then cConfig[4] = substring(cLine,3,length(cLine)).
  end.

  if substring(cConfig[4],length(cConfig[4]),1) <> '/':U and
     substring(cConfig[4],length(cConfig[4]),1) <> '\':U then
    cConfig[4] = cConfig[4] + '/':U.

  /* Esta linha deve ser executada somente para servidores Linux */
  cConfig[4] = replace(cConfig[4],"\":U,"/":U).

  if length(cConfig[1]) > 0 and
     length(cConfig[2]) > 0 and
     length(cConfig[3]) > 0 and
     length(cConfig[4]) > 0 then
    return true.
  else
    return false.
end function.

Nenhum comentário:

Postar um comentário