Network Server Admin...

Share your advanced PureBasic knowledge/code with the community.
darklordz
Enthusiast
Enthusiast
Posts: 119
Joined: Wed May 21, 2003 1:44 pm
Location: Netherlands
Contact:

Network Server Admin...

Post by darklordz »

Code updated for 5.20+

I hope this helps others that wish to start their own network server communication tools. I am writing a application like RAdmin it would be pretty funky. Besides i have a quake ||| arena server id like to manage remotely :P

It has functions for saving and packing screenshots, then you can send the screenshots (while compressed) to the client <-> server unpack and voila a remote desktop view has been born. All you need todo then is use the windows api to fake mouseclicks and mouse movements... it's still WIP but this would help anyone i guess...

Also greetz and props to everybody who's code ive used...

Code: Select all

UseZipPacker()

Enumeration
  #MEM_BUFFER
EndEnumeration

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !"+Chr(9), #MB_ICONERROR)
  End
EndIf

Procedure _CaptureScreen(Left, Top, Width, Height)
  DM.DEVMODE
  srcDC.i
  trgDC.i
  BMPHandle.i
  srcDC = CreateDC_("DISPLAY","","",DM)
  trgDC = CreateCompatibleDC_(srcDC)
  BMPHandle = CreateCompatibleBitmap_(srcDC,Width,Height)
  SelectObject_(trgDC,BMPHandle)
  BitBlt_(trgDC,0,0,Width,Height,srcDC,Left,Top,#SRCCOPY)
  DeleteDC_(trgDC)
  ReleaseDC_(BMPHandle,srcDC)
  ProcedureReturn BMPHandle
EndProcedure

Procedure _SaveScreen(FileName.s,Left,Top,Width,Height)
  *DeskTop = _CaptureScreen(Left,Top,Width,Height) 
  CreateImage(DeskTop,Width,Height) 
  StartDrawing(ImageOutput(DeskTop)) 
    DrawImage(*DeskTop,0,0)
  StopDrawing() 
  SaveImage(DeskTopSprite,FileName.s,#PB_ImagePlugin_BMP)
EndProcedure

Procedure _SetImageToClipboard(BMPHandle)
  OpenClipboard_(#Null)
  EmptyClipboard_()
  SetClipboardData_(2, BMPHandle)
  CloseClipboard_()
EndProcedure

Procedure _CompressFile(FileName.s,OutFile.s)
  If ReadFile(0,FileName.s)
    FileLength = Lof(0)
    *fileBuffer = AllocateMemory(FileLength)
    *packBuffer = AllocateMemory(FileLength)
    If FileLength And *fileBuffer And *packBuffer
      ReadData(0, *fileBuffer, FileLength)
      StartTimer = GetTickCount_()
      CompressedLength = CompressMemory(*fileBuffer, FileLength, *packBuffer, FileLength)
      CloseFile(0)
      If CompressedLength
        CompressionTime = GetTickCount_()-StartTimer
        OpenFile(1,OutFile.s)
        WriteData(1, *packBuffer, CompressedLength)
        WriteLong(1, FileLength)
        CloseFile(1)
        ProcedureReturn 1
      Else
        ProcedureReturn 0
      EndIf
    Else
      ProcedureReturn 0
    EndIf
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure _UncompressFile(InFile.s,OutFile.s)
  If ReadFile(0,InFile.s)
    FileLength = Lof(0)
    FileSeek(0,FileLength-4)
    OriginalLenght=ReadLong(0)
    *fileBuffer = AllocateMemory(FileLength)
    *packBuffer = AllocateMemory(FileLength + 4)
    If FileLength And *fileBuffer And *packBuffer
      FileSeek(0,0)
      ReadData(0,*fileBuffer, FileLength)
      StartTimer = GetTickCount_()
      DecompressedLength = UncompressMemory(*fileBuffer, FileLength, *packBuffer, FileLength + 4)
      If DecompressedLength = OriginalLenght
        OpenFile(1,OutFile.s)
        WriteData(1, *packBuffer,DecompressedLength)
        CloseFile(1)
        DecompressionTime = GetTickCount_()-StartTimer
        ProcedureReturn 1
      Else
        ProcedureReturn 0
      EndIf
    Else
      ProcedureReturn 0
    EndIf
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

ProcedureDLL _SendTextMessage(ConnectionID,Text.s)
  Debug Text.s
  SendNetworkString(ConnectionID,Str(Len(Text.s))+"[/]"+Text.s)
  ProcedureReturn 1
EndProcedure

ProcedureDLL _CreateServer(Port)
  If CreateNetworkServer(1, Port)
    *memBuffer = AllocateMemory(1000)
    Repeat
      ServerEvent = NetworkServerEvent()          
      If ServerEvent > -1
        ClientID = EventClient()
        Select ServerEvent
          Case 0
          Case 1
          Case 2
            ReceiveNetworkData(ClientID, *memBuffer, 1000)
            If FindString(Trim(PeekS(*memBuffer)),"[/]",1) > 0
              Length = Val(Trim(StringField(Trim(PeekS(*memBuffer)), 1, "[/]")))
              Debug Left(StringField(Trim(PeekS(*memBuffer)), 2, "[/]"),Length)
            EndIf
          Case 3
            ;ReceiveNetworkFile(ClientID, "recdta\scr.dat")
          Case 4
        EndSelect
      EndIf
    Until Quit = 1 
    FreeMemory(*memBuffer)
    CloseNetworkServer(1)
    ProcedureReturn 1
  Else
    MessageRequester("Error", "Can't create the server..."+Chr(9)+Chr(13)+"(Port: "+Str(Port)+")", #MB_ICONERROR)
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure _CreateClient(ServerLoc.s,Port)
  ConnectionID = OpenNetworkConnection(ServerLoc.s, Port)
  If ConnectionID
    *memBuffer = AllocateMemory(1000)
    _SendTextMessage(ConnectionID,"connected")
    Repeat
      ClientEvent = NetworkClientEvent(ConnectionID)
      If ClientEvent > -1
        Select ClientEvent
          Case 0
          Case 2
            ReceiveNetworkData(ConnectionID, *memBuffer, 1000)
          Case 3
            ;ReceiveNetworkFile(ConnectionID, "recdta\scr.dat")
        EndSelect
      EndIf
      For I = 0 To 256
        If GetAsyncKeyState_(I)
          Debug Hex(I)
        EndIf
      Next I
    Until Quit = 1
    FreeMemory(*memBuffer)
    CloseNetworkConnection(ConnectionID)
    ProcedureReturn 1
  Else
    MessageRequester("Error", "Can't locate the server..."+Chr(13)+"(Location: "+ServerLoc.s+":"+Str(Port)+")"+Chr(9), #MB_ICONERROR)
    ProcedureReturn 0
  EndIf
EndProcedure

_CreateServer(8000)
;_CreateClient("127.0.0.1",8000)
Image
netmon
User
User
Posts: 12
Joined: Sun Jul 27, 2003 4:29 pm

Post by netmon »

I havent tried any of this code, but do you get any kind of screen capture delays? what i mean is, sometime i have had to take 2 screenshot before the clip board would take the image. and the image that is in there is the first image and not the second.

a friend was over and we talked about this, and he was thinking that there is a keyboard thread thats kinda out of sync. he doesnt use pb, but that was his guess.

second i have been working with this model of remote server and i have come to the conclusion that it would be best to code something up to get the screens pointer and feed that to a memory bank instead of wasting time sending info to the clipboard and saving to a file.
Post Reply