[Haskell-Cafe] Parsing bytestream

C K Kashyap ckkashyap at gmail.com
Tue Nov 9 04:07:12 EST 2010


Hi,
I've been trying to implement the RFB (VNC) protocol and I was
exploring a neat way of parsing the protocol bytestream.

The protocol specifies that the client first sends a byte that
identifies the command then a sequence of word8, word16, word32 and
paddings. I've tried to capture that logic with the function below.

commandFormat :: Word8 -> [Int] -- 0 for padding bytes
commandFormat c
		| c == setPixelFormat	= [0,0,0,1,1,1,1,2,2,2,1,1,1,0,0,0]
		| c == setEncodings	= [0,2]
		| c == framebufferUpdateRequest = [0,2,2,2,2]
		| c == keyEvent 	= [1,2,4]
		| c == pointerEvent	= [1,2,2]
		| c == clientCutText	= [0,0,0,4]
		| otherwise		= []


What I am struggling with is a function like this  -

parseCommand :: Word8 -> ByteStream -> [Int]
parseCommand command byteStream = ...

I want the function to use the commandFormat above and return me a
list of integers --

for example - if the command is keyEvent and the bytestream is "<1>
<0> <2> <0> <0> <0> <3>"
The output should be -> [1,2,3]!!! -> That way, I can process it easily.

I'd appreciate some help on this very much.

-- 
Regards,
Kashyap


More information about the Haskell-Cafe mailing list