-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Later we'll add a new command to list messages - Bump protocol version due to new/changed commands - Add Message dataclass with ID field - Add message cache to HighCommand - Add Reader.read_bigint() - Add snowflake.create_snowflake() - Replace ClientEventMessageReceived fields with one message field - Replace ServerMessagePost fields with one message field - Replace Server.send_message() parameters with one message parameter
- Loading branch information
1 parent
c1acd9b
commit 5909c2a
Showing
14 changed files
with
162 additions
and
59 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
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
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,32 @@ | ||
from dataclasses import dataclass | ||
from typing import Self | ||
|
||
from . import varchar | ||
from .constants import MAX_CHANNEL_NAME_LENGTH, MAX_MESSAGE_LENGTH, MAX_NICK_LENGTH | ||
from .reader import Reader | ||
|
||
|
||
@dataclass | ||
class Message: | ||
id: int | ||
channel_name: str | ||
nick: str | ||
content: str | ||
|
||
def __bytes__(self) -> bytes: | ||
return bytes( | ||
[ | ||
*self.id.to_bytes(8, byteorder="big"), | ||
*varchar.dumps(self.channel_name, max_length=MAX_CHANNEL_NAME_LENGTH), | ||
*varchar.dumps(self.nick, max_length=MAX_NICK_LENGTH), | ||
*varchar.dumps(self.content, max_length=MAX_MESSAGE_LENGTH), | ||
] | ||
) | ||
|
||
@classmethod | ||
def from_reader(cls, reader: Reader) -> Self: | ||
id = reader.read_bigint() | ||
channel_name = reader.read_varchar(max_length=MAX_CHANNEL_NAME_LENGTH) | ||
nick = reader.read_varchar(max_length=MAX_NICK_LENGTH) | ||
content = reader.read_varchar(max_length=MAX_MESSAGE_LENGTH) | ||
return cls(id=id, channel_name=channel_name, nick=nick, content=content) |
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,45 @@ | ||
""" | ||
64 63 19 12 0 | ||
0 00011000111000111000000101110101101101000100 1110101 000000000111 | ||
64-63: Always 0 | ||
63-19: Unix timestamp in milliseconds | ||
19-12: Process ID | ||
12-00: Incrementing per-process ID | ||
""" | ||
|
||
import datetime | ||
import os | ||
import time | ||
|
||
_incrementing_id = 0 | ||
|
||
|
||
def create_snowflake( | ||
t: float | int | datetime.datetime | None = None, | ||
pid: int | None = None, | ||
increment: int | None = None, | ||
) -> int: | ||
if t is None: | ||
# WARNING: this may roll back or forwards depending on system time | ||
t = time.time_ns() // 1000000 | ||
elif isinstance(t, datetime.datetime): | ||
t = int(t.timestamp() * 1000) | ||
elif isinstance(t, float): | ||
t = int(t * 1000) | ||
|
||
if t.bit_length() > 44: | ||
raise OverflowError(f"Timestamp {t} out of bounds (are we in 2527?)") | ||
|
||
if pid is None: | ||
pid = os.getpid() | ||
|
||
if increment is None: | ||
global _incrementing_id | ||
increment = _incrementing_id | ||
_incrementing_id += 1 | ||
|
||
pid = pid % 128 | ||
increment = increment % 4096 | ||
|
||
return (t << 19) + (pid << 12) + increment |
Oops, something went wrong.