Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #///////////////// message.nim /////////////
- type
- MessageKind* = enum
- mkRotX, mkRotY, mkRotZ,
- mkPosX, mkPosY, mkPosZ,
- mkLength, mkWidth, mkHeight,
- mkSelectPart,
- mkQuit
- Message* = object
- kind*: MessageKind
- value*: cfloat
- #////////////// server.nim ////////////
- #
- #Server Program for Win32 Named Pipes Example.
- #Based on cpp example by Peter R. Bloomfield.
- #
- #For an explanation of the code, see the associated blog post:
- #http://avidinsight.uk/2012/03/introduction-to-win32-named-pipes-cpp/
- #https://github.com/avidinsight/win32-named-pipes-example/blob/master/src/server.cpp
- #
- #This code is made freely available under the MIT open source license
- #(see accompanying LICENSE file for details).
- #It is intended only for educational purposes. and is provide as-is with no
- #guarantee about its reliability, correctness, or suitability for any purpose.
- #
- #
- #TEST INSTRUCTIONS:
- #
- #Run this server program first.
- #Before closing it, run the accompanying client program.
- #
- import winlean, os
- import message
- proc connectNamedPipe(hNamedPipe: Handle, lpOverlapped: pointer): WINBOOL
- {.importc: "ConnectNamedPipe", stdcall, dynlib: "kernel32".}
- const
- pipeHeaderName = r"\\.\pipe\mypipe"
- const
- DEFAULT_PIPE_SIZE = 65536'i32
- FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000'i32
- PIPE_WAIT = 0x00000000'i32
- PIPE_TYPE_BYTE = 0x00000000'i32
- PIPE_READMODE_BYTE = 0x00000000'i32
- ERROR_PIPE_CONNECTED = 535
- ERROR_PIPE_BUSY = 231
- ERROR_BROKEN_PIPE = 109
- ERROR_PIPE_NOT_CONNECTED = 233
- var pipe:Handle
- proc createPipe*() =
- echo "Creating an instance of a named pipe..."
- # Create a pipe to send data
- var pipeName = newWideCString(pipeHeaderName)
- pipe = createNamedPipe(pipeName, # name of the pipe
- PIPE_ACCESS_OUTBOUND, # 1-way pipe -- send only
- PIPE_TYPE_BYTE, # send data as a byte stream
- 1, # only allow 1 instance of this pipe
- 0, # no outbound buffer
- 0, # no inbound buffer
- 0, # use default wait time
- nil)
- if pipe == INVALID_HANDLE_VALUE:
- echo "Failed to create outbound pipe instance."
- let err = osLastError()
- if err.int32 != ERROR_PIPE_BUSY:
- raiseOsError(err)
- echo "Waiting for a client to connect to the pipe..."
- # This call blocks until a client process connects to the pipe
- var res = connectNamedPipe(pipe, nil)
- if res == 0:
- echo "Failed to make connection on named pipe."
- # look up error code here using GetLastError()
- discard closeHandle(pipe)
- # close the pipe
- proc sendMessage*(data: ptr Message) =
- #echo "Sending data to pipe..."
- # This call blocks until a client process reads all the data
- var
- numBytesWritten:int32 = 0
- res:int32 = 0
- try:
- res = writeFile(pipe, # handle to our outbound pipe
- cast[pointer](data), # data to send
- sizeof(Message).int32,
- numBytesWritten.addr, # will store actual amount of data sent
- nil)
- except:
- return
- # if res > 0:
- # echo "Number of bytes sent: ", numBytesWritten
- # else:
- # echo "Failed to send data."
- proc closePipe*() =
- # Close the pipe (automatically disconnects client too)
- echo "closing pipe"
- discard closeHandle(pipe)
- proc test() =
- var msg = Message()
- createPipe()
- msg.kind = mkRotX
- msg.value = 1.5
- sendMessage(msg.addr)
- sleep(3000)
- msg.kind = mkRotz
- msg.value = 0.25
- sendMessage(msg.addr)
- sleep(3000)
- msg.kind = mkQuit
- sendMessage(msg.addr)
- closePipe()
- when isMainModule:
- test()
Advertisement
Add Comment
Please, Sign In to add comment