Guest User

pipe example server

a guest
Jan 25th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 3.71 KB | None | 0 0
  1. #///////////////// message.nim /////////////
  2. type
  3.   MessageKind* = enum
  4.     mkRotX, mkRotY, mkRotZ,
  5.     mkPosX, mkPosY, mkPosZ,
  6.     mkLength, mkWidth, mkHeight,
  7.     mkSelectPart,
  8.     mkQuit
  9.  
  10.   Message* = object
  11.     kind*: MessageKind
  12.     value*: cfloat
  13.  
  14. #////////////// server.nim ////////////
  15. #
  16. #Server Program for Win32 Named Pipes Example.
  17. #Based on cpp example by Peter R. Bloomfield.
  18. #
  19. #For an explanation of the code, see the associated blog post:
  20. #http://avidinsight.uk/2012/03/introduction-to-win32-named-pipes-cpp/
  21. #https://github.com/avidinsight/win32-named-pipes-example/blob/master/src/server.cpp
  22. #
  23. #This code is made freely available under the MIT open source license
  24. #(see accompanying LICENSE file for details).
  25. #It is intended only for educational purposes. and is provide as-is with no
  26. #guarantee about its reliability, correctness, or suitability for any purpose.
  27. #
  28. #
  29. #TEST INSTRUCTIONS:
  30. #
  31. #Run this server program first.
  32. #Before closing it, run the accompanying client program.
  33. #
  34.  
  35. import winlean, os
  36. import message
  37.  
  38. proc connectNamedPipe(hNamedPipe: Handle, lpOverlapped: pointer): WINBOOL
  39.      {.importc: "ConnectNamedPipe", stdcall, dynlib: "kernel32".}
  40. const
  41.   pipeHeaderName = r"\\.\pipe\mypipe"
  42.  
  43. const
  44.   DEFAULT_PIPE_SIZE = 65536'i32
  45.   FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000'i32
  46.   PIPE_WAIT = 0x00000000'i32
  47.   PIPE_TYPE_BYTE = 0x00000000'i32
  48.   PIPE_READMODE_BYTE = 0x00000000'i32
  49.   ERROR_PIPE_CONNECTED = 535
  50.   ERROR_PIPE_BUSY = 231
  51.   ERROR_BROKEN_PIPE = 109
  52.   ERROR_PIPE_NOT_CONNECTED = 233
  53.  
  54. var pipe:Handle
  55.  
  56. proc createPipe*() =
  57.   echo "Creating an instance of a named pipe..."
  58.   # Create a pipe to send data
  59.   var pipeName = newWideCString(pipeHeaderName)
  60.   pipe = createNamedPipe(pipeName, # name of the pipe
  61.                          PIPE_ACCESS_OUTBOUND, # 1-way pipe -- send only
  62.                          PIPE_TYPE_BYTE, # send data as a byte stream
  63.                          1, # only allow 1 instance of this pipe
  64.                          0, # no outbound buffer
  65.                          0, # no inbound buffer
  66.                          0, # use default wait time
  67.                          nil)
  68.  
  69.  
  70.   if pipe == INVALID_HANDLE_VALUE:
  71.     echo "Failed to create outbound pipe instance."
  72.     let err = osLastError()
  73.     if err.int32 != ERROR_PIPE_BUSY:
  74.       raiseOsError(err)
  75.  
  76.   echo "Waiting for a client to connect to the pipe..."
  77.   # This call blocks until a client process connects to the pipe
  78.   var res = connectNamedPipe(pipe, nil)
  79.   if res == 0:
  80.     echo "Failed to make connection on named pipe."
  81.     # look up error code here using GetLastError()
  82.     discard closeHandle(pipe)
  83.     # close the pipe
  84.  
  85. proc sendMessage*(data: ptr Message) =
  86.   #echo "Sending data to pipe..."
  87.   # This call blocks until a client process reads all the data
  88.   var
  89.     numBytesWritten:int32  = 0
  90.     res:int32 = 0
  91.  
  92.   try:
  93.     res = writeFile(pipe,      # handle to our outbound pipe
  94.                     cast[pointer](data),      # data to send
  95.                     sizeof(Message).int32,
  96.                     numBytesWritten.addr, # will store actual amount of data sent
  97.                     nil)
  98.   except:
  99.     return
  100.   # if res > 0:
  101.   #   echo "Number of bytes sent: ", numBytesWritten
  102.   # else:
  103.   #   echo "Failed to send data."
  104.  
  105. proc closePipe*() =
  106.   # Close the pipe (automatically disconnects client too)
  107.   echo "closing pipe"
  108.   discard closeHandle(pipe)
  109.  
  110.  
  111. proc test() =
  112.   var msg = Message()
  113.  
  114.   createPipe()
  115.  
  116.   msg.kind = mkRotX
  117.   msg.value = 1.5
  118.   sendMessage(msg.addr)
  119.   sleep(3000)
  120.  
  121.   msg.kind = mkRotz
  122.   msg.value = 0.25
  123.   sendMessage(msg.addr)
  124.   sleep(3000)
  125.  
  126.  
  127.   msg.kind = mkQuit
  128.   sendMessage(msg.addr)
  129.  
  130.   closePipe()
  131.  
  132. when isMainModule:
  133.   test()
Advertisement
Add Comment
Please, Sign In to add comment