Message: Create a simple Message Buffer class and use it
Message will contain the fragments of a message and then will combine them into one big bytearray.
This commit is contained in:
parent
0dffcb86e4
commit
e0ea928d63
21
IPAMuxer.st
21
IPAMuxer.st
|
@ -35,30 +35,27 @@ Object subclass: IPAMuxer [
|
|||
yourself.
|
||||
]
|
||||
|
||||
prepareNext: aData [
|
||||
prepareNext: aData with: aStream [
|
||||
"Write the data onto the stream"
|
||||
| stream size |
|
||||
| msg |
|
||||
|
||||
aData size > 65536
|
||||
ifTrue: [
|
||||
self error: 'Too much data'.
|
||||
].
|
||||
|
||||
stream := WriteStream on: (ByteArray new: aData size + 3).
|
||||
msg := Message new.
|
||||
|
||||
"put the size in ntohs"
|
||||
size := aData size swap16.
|
||||
stream nextPut: size bitAnd: 16rFF.
|
||||
stream nextPut: ((size bitShift: 8) bitAnd: 16rFF).
|
||||
msg putLen16: aData size.
|
||||
msg putByte: aStream.
|
||||
msg putByteArray: aData.
|
||||
|
||||
"Copy the old data to the new stream"
|
||||
1 to: aData size do: [:byte | stream nextPut: aData at: byte].
|
||||
|
||||
^ stream collection.
|
||||
^ msg toByteArray.
|
||||
]
|
||||
|
||||
nextPut: aData [
|
||||
socket nextPut: (self prepareNext: aData).
|
||||
nextPut: aData with: aStream [
|
||||
socket nextPut: (self prepareNext: aData) with: aStream.
|
||||
]
|
||||
|
||||
socket: aSocket [
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
"Copyright header"
|
||||
|
||||
|
||||
Object subclass: Message [
|
||||
| chunks |
|
||||
<comment: 'A network buffer/creation class. Modeled after the msgb of osmocore'>
|
||||
|
||||
initialize [
|
||||
chunks := OrderedCollection new.
|
||||
]
|
||||
|
||||
prependByteArray: aByteArray [
|
||||
chunks addFirst: aByteArray.
|
||||
]
|
||||
|
||||
putByte: aByte [
|
||||
chunks add: (ByteArray with: aByte)
|
||||
]
|
||||
|
||||
putByteArray: aByteArray [
|
||||
chunks add: aByteArray.
|
||||
]
|
||||
|
||||
putLen16: aInt [
|
||||
| data low high |
|
||||
low := (aInt bitShift: 8) bitAnd: 16rFF.
|
||||
high := aInt bitAnd: 16rFF.
|
||||
data := ByteArray with: low with: high.
|
||||
chunks add: data.
|
||||
]
|
||||
|
||||
toByteArray [
|
||||
^ ByteArray join: chunks.
|
||||
]
|
||||
]
|
Reference in New Issue