-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
923c289
commit 2d5b304
Showing
11 changed files
with
140 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"nim.project": ["src/modernnet.nim"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#! Copyright 2024 Yu-Vitaqua-fer-Chronos | ||
#! | ||
#! Licensed under the Apache License, Version 2.0 (the "License"); | ||
#! you may not use this file except in compliance with the License. | ||
#! You may obtain a copy of the License at | ||
#! | ||
#! http://www.apache.org/licenses/LICENSE-2.0 | ||
#! | ||
#! Unless required by applicable law or agreed to in writing, software | ||
#! distributed under the License is distributed on an "AS IS" BASIS, | ||
#! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
#! See the License for the specific language governing permissions and | ||
#! limitations under the License. | ||
|
||
import ./[ | ||
exceptions, | ||
buffer | ||
] | ||
import ./private/[ | ||
constants, | ||
utils | ||
] | ||
|
||
using receive_bytes: proc(_: int): seq[byte] | ||
|
||
proc read[R: SizedNum](_: typedesc[R], receive_bytes): R = | ||
## Reads any numeric type or boolean from the callback | ||
let bytes = receive_bytes(sizeof(R)) | ||
bytes.extract(R) | ||
|
||
proc readVar[R: int32 | int64](_: typedesc[R], receive_bytes): R = | ||
## Reads a VarInt or a VarLong from the callback | ||
var | ||
position: int8 = 0 | ||
currentByte: int8 | ||
|
||
while true: | ||
currentByte = read(int8, receive_bytes) | ||
result = result or ((currentByte.R and SegmentBits) shl position) | ||
|
||
if (currentByte and ContinueBit) == 0: | ||
break | ||
|
||
position += 7 | ||
|
||
when R is int32: | ||
if position >= VarIntBits: | ||
raise newException(MnPacketParsingError, "VarInt is too big!") | ||
|
||
elif R is int64: | ||
if position >= VarLongBits: | ||
raise newException(MnPacketParsingError, "VarLong is too big!") | ||
|
||
else: | ||
{.error: "Deserialisation of `" & $R & "` is not implemented!".} | ||
|
||
proc readRawPacket*(receive_bytes): (int, Buffer) = | ||
let packetId = readVar(int32, receive_bytes) | ||
let length = readVar(int32, receive_bytes) | ||
|
||
return (packetId.int, newBuffer(receive_bytes(length))) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#! Copyright 2024 Yu-Vitaqua-fer-Chronos | ||
#! | ||
#! Licensed under the Apache License, Version 2.0 (the "License"); | ||
#! you may not use this file except in compliance with the License. | ||
#! You may obtain a copy of the License at | ||
#! | ||
#! http://www.apache.org/licenses/LICENSE-2.0 | ||
#! | ||
#! Unless required by applicable law or agreed to in writing, software | ||
#! distributed under the License is distributed on an "AS IS" BASIS, | ||
#! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
#! See the License for the specific language governing permissions and | ||
#! limitations under the License. | ||
|
||
import ./stew/endians2 | ||
|
||
type SizedNum* = bool | byte | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64 | ||
|
||
template unsignedSize(T: typedesc): typedesc = | ||
when sizeof(T) == 1: | ||
uint8 | ||
|
||
elif sizeof(T) == 2: | ||
uint16 | ||
|
||
elif sizeof(T) == 4: | ||
uint32 | ||
|
||
elif sizeof(T) == 8: | ||
uint64 | ||
|
||
else: | ||
{.error: "Deserialisation of `" & $T & "` is not implemented!".} | ||
|
||
proc extract*[T: SizedNum](oa: openArray[byte], _: typedesc[T]): T = | ||
if oa.len < sizeof(T): | ||
raise newException(ValueError, "The buffer was to small to extract a " & $T & '!') | ||
|
||
elif oa.len > sizeof(T): | ||
raise newException(ValueError, "The buffer was to big to extract a " & $T & '!') | ||
|
||
cast[T](unsignedSize(T).fromBytesBE(oa.toOpenArray(0, sizeof(T) - 1))) | ||
|
||
proc deposit*[T: SizedNum](value: T, oa: out openArray[byte]) = | ||
if oa.len < sizeof(T): | ||
raise newException(ValueError, "The buffer was to small to deposit a " & $T & '!') | ||
|
||
let res = cast[unsignedSize(T)](value).toBytesBE() | ||
|
||
for i in 0..<sizeof(T): | ||
oa[i] = res[i] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters