From aede70d0071256e8f9cb7f54dacb48119e76cd20 Mon Sep 17 00:00:00 2001 From: sourcecaster Date: Sun, 10 Jul 2022 21:08:24 +0200 Subject: [PATCH] Buffer type support added. Example updated (up to date). --- CHANGELOG.md | 4 + example/example.dart | 48 +-- .../generated/example-posts.generated.dart | 338 +++++++++++------ .../generated/example-users.generated.dart | 339 +++++++++++++----- pubspec.yaml | 6 +- test/generated/test.generated.dart | 1 + 6 files changed, 510 insertions(+), 226 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06e3dc8..614a0c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v1.2.0 +* Added support for binary type (uses Uint8List). Format: binary12, binary64 etc. - any buffer length in bytes. +* Example file is up to date now. (Message constructors require non-optional parameters). + ## v1.1.3 * Bugfix: concurrent modifications during iteration occurred in some cases while processing events. diff --git a/example/example.dart b/example/example.dart index 29faaf5..ecca4be 100644 --- a/example/example.dart +++ b/example/example.dart @@ -140,30 +140,30 @@ class MehModule extends Module { console.on('sendPackedMessage', (_, __) { /// GetResponse is just the name of response message of command Get. /// It's declared in example-users.generated.dart. - final GetResponse message = GetResponse() - ..email = '${randomString()}@${randomString()}.com' - ..nickname = 'Mr. ${randomString()}' - ..hidden = false - ..created = DateTime.now() - ..info = (GetResponseInfo() - ..firstName = randomString() - ..lastName = randomString() - ..age = randomInt() - ) - ..social = (GetResponseSocial() - ..facebookId = randomInt() < 128 ? 'fbID_${randomString()}' : null - ..twitterId = randomInt() < 128 ? 'twID_${randomString()}' : null - ..instagramId = randomInt() < 128 ? 'inID_${randomString()}' : null - ) - ..stats = (GetResponseStats() - ..posts = randomInt() - ..comments = randomInt() - ..likes = randomInt() * 10 - ..dislikes = randomInt() - ..rating = randomInt() / 25.5 - - ) - ..sessions = []; + final GetResponse message = GetResponse( + email: '${randomString()}@${randomString()}.com', + nickname: 'Mr. ${randomString()}', + hidden: false, + created: DateTime.now(), + info: GetResponseInfo( + firstName: randomString(), + lastName: randomString(), + age: randomInt(), + ), + social: GetResponseSocial( + facebookId: randomInt() < 128 ? 'fbID_${randomString()}' : null, + twitterId: randomInt() < 128 ? 'twID_${randomString()}' : null, + instagramId: randomInt() < 128 ? 'inID_${randomString()}' : null, + ), + stats: GetResponseStats( + posts: randomInt(), + comments: randomInt(), + likes: randomInt() * 10, + dislikes: randomInt(), + rating: randomInt() / 25.5, + ), + sessions: [], + ); server.broadcast(message); log('Here is the message I sent:', MAGENTA); messageToStrings(message).forEach(log); diff --git a/example/generated/example-posts.generated.dart b/example/generated/example-posts.generated.dart index 1e468d1..eee7c8d 100644 --- a/example/generated/example-posts.generated.dart +++ b/example/generated/example-posts.generated.dart @@ -1,34 +1,14 @@ +import 'dart:typed_data'; import 'package:packme/packme.dart'; -class GetAllRequest extends PackMeMessage { - - @override - GetAllResponse get $response { - final GetAllResponse message = GetAllResponse(); - message.$request = this; - return message; - } - - @override - int $estimate() { - $reset(); - int bytes = 8; - return bytes; - } - - @override - void $pack() { - $initPack(63570112); - } - - @override - void $unpack() { - $initUnpack(); - } - -} - class GetAllResponsePostAuthor extends PackMeMessage { + GetAllResponsePostAuthor({ + required this.id, + required this.nickname, + required this.avatar, + }); + GetAllResponsePostAuthor.$empty(); + late List id; late String nickname; late String avatar; @@ -43,15 +23,15 @@ class GetAllResponsePostAuthor extends PackMeMessage { bytes += $stringBytes(avatar); return bytes; } - + @override void $pack() { $packUint32(id.length); - id.forEach($packUint8); + for (final int item in id) $packUint8(item); $packString(nickname); $packString(avatar); } - + @override void $unpack() { id = []; @@ -62,10 +42,23 @@ class GetAllResponsePostAuthor extends PackMeMessage { nickname = $unpackString(); avatar = $unpackString(); } - + + @override + String toString() { + return 'GetAllResponsePostAuthor\x1b[0m(id: ${PackMe.dye(id)}, nickname: ${PackMe.dye(nickname)}, avatar: ${PackMe.dye(avatar)})'; + } } class GetAllResponsePost extends PackMeMessage { + GetAllResponsePost({ + required this.id, + required this.author, + required this.title, + required this.shortContent, + required this.posted, + }); + GetAllResponsePost.$empty(); + late List id; late GetAllResponsePostAuthor author; late String title; @@ -83,17 +76,17 @@ class GetAllResponsePost extends PackMeMessage { bytes += $stringBytes(shortContent); return bytes; } - + @override void $pack() { $packUint32(id.length); - id.forEach($packUint8); + for (final int item in id) $packUint8(item); $packMessage(author); $packString(title); $packString(shortContent); $packDateTime(posted); } - + @override void $unpack() { id = []; @@ -101,15 +94,24 @@ class GetAllResponsePost extends PackMeMessage { for (int i = 0; i < idLength; i++) { id.add($unpackUint8()); } - author = $unpackMessage(GetAllResponsePostAuthor()) as GetAllResponsePostAuthor; + author = $unpackMessage(GetAllResponsePostAuthor.$empty()); title = $unpackString(); shortContent = $unpackString(); posted = $unpackDateTime(); } - + + @override + String toString() { + return 'GetAllResponsePost\x1b[0m(id: ${PackMe.dye(id)}, author: ${PackMe.dye(author)}, title: ${PackMe.dye(title)}, shortContent: ${PackMe.dye(shortContent)}, posted: ${PackMe.dye(posted)})'; + } } class GetAllResponse extends PackMeMessage { + GetAllResponse({ + required this.posts, + }); + GetAllResponse.$empty(); + late List posts; @override @@ -120,65 +122,77 @@ class GetAllResponse extends PackMeMessage { for (int i = 0; i < posts.length; i++) bytes += posts[i].$estimate(); return bytes; } - + @override void $pack() { $initPack(280110613); $packUint32(posts.length); - posts.forEach($packMessage); + for (final GetAllResponsePost item in posts) $packMessage(item); } - + @override void $unpack() { $initUnpack(); posts = []; final int postsLength = $unpackUint32(); for (int i = 0; i < postsLength; i++) { - posts.add($unpackMessage(GetAllResponsePost()) as GetAllResponsePost); + posts.add($unpackMessage(GetAllResponsePost.$empty())); } } - + + @override + String toString() { + return 'GetAllResponse\x1b[0m(posts: ${PackMe.dye(posts)})'; + } } -class GetRequest extends PackMeMessage { - late List postId; +class GetAllRequest extends PackMeMessage { + GetAllRequest(); + GetAllRequest.$empty(); + - @override - GetResponse get $response { - final GetResponse message = GetResponse(); + GetAllResponse $response({ + required List posts, + }) { + final GetAllResponse message = GetAllResponse(posts: posts); message.$request = this; return message; } - + @override int $estimate() { $reset(); int bytes = 8; - bytes += 4; - bytes += 1 * postId.length; return bytes; } - + @override void $pack() { - $initPack(187698222); - $packUint32(postId.length); - postId.forEach($packUint8); + $initPack(63570112); } - + @override void $unpack() { $initUnpack(); - postId = []; - final int postIdLength = $unpackUint32(); - for (int i = 0; i < postIdLength; i++) { - postId.add($unpackUint8()); - } } - + + @override + String toString() { + return 'GetAllRequest\x1b[0m()'; + } } class GetResponseAuthor extends PackMeMessage { + GetResponseAuthor({ + required this.id, + required this.nickname, + required this.avatar, + this.facebookId, + this.twitterId, + this.instagramId, + }); + GetResponseAuthor.$empty(); + late List id; late String nickname; late String avatar; @@ -208,19 +222,19 @@ class GetResponseAuthor extends PackMeMessage { } return bytes; } - + @override void $pack() { for (int i = 0; i < 1; i++) $packUint8($flags[i]); $packUint32(id.length); - id.forEach($packUint8); + for (final int item in id) $packUint8(item); $packString(nickname); $packString(avatar); if (facebookId != null) $packString(facebookId!); if (twitterId != null) $packString(twitterId!); if (instagramId != null) $packString(instagramId!); } - + @override void $unpack() { for (int i = 0; i < 1; i++) $flags.add($unpackUint8()); @@ -241,10 +255,20 @@ class GetResponseAuthor extends PackMeMessage { instagramId = $unpackString(); } } - + + @override + String toString() { + return 'GetResponseAuthor\x1b[0m(id: ${PackMe.dye(id)}, nickname: ${PackMe.dye(nickname)}, avatar: ${PackMe.dye(avatar)}, facebookId: ${PackMe.dye(facebookId)}, twitterId: ${PackMe.dye(twitterId)}, instagramId: ${PackMe.dye(instagramId)})'; + } } class GetResponseStats extends PackMeMessage { + GetResponseStats({ + required this.likes, + required this.dislikes, + }); + GetResponseStats.$empty(); + late int likes; late int dislikes; @@ -254,22 +278,33 @@ class GetResponseStats extends PackMeMessage { int bytes = 8; return bytes; } - + @override void $pack() { $packUint32(likes); $packUint32(dislikes); } - + @override void $unpack() { likes = $unpackUint32(); dislikes = $unpackUint32(); } - + + @override + String toString() { + return 'GetResponseStats\x1b[0m(likes: ${PackMe.dye(likes)}, dislikes: ${PackMe.dye(dislikes)})'; + } } class GetResponseCommentAuthor extends PackMeMessage { + GetResponseCommentAuthor({ + required this.id, + required this.nickname, + required this.avatar, + }); + GetResponseCommentAuthor.$empty(); + late List id; late String nickname; late String avatar; @@ -284,15 +319,15 @@ class GetResponseCommentAuthor extends PackMeMessage { bytes += $stringBytes(avatar); return bytes; } - + @override void $pack() { $packUint32(id.length); - id.forEach($packUint8); + for (final int item in id) $packUint8(item); $packString(nickname); $packString(avatar); } - + @override void $unpack() { id = []; @@ -303,10 +338,21 @@ class GetResponseCommentAuthor extends PackMeMessage { nickname = $unpackString(); avatar = $unpackString(); } - + + @override + String toString() { + return 'GetResponseCommentAuthor\x1b[0m(id: ${PackMe.dye(id)}, nickname: ${PackMe.dye(nickname)}, avatar: ${PackMe.dye(avatar)})'; + } } class GetResponseComment extends PackMeMessage { + GetResponseComment({ + required this.author, + required this.comment, + required this.posted, + }); + GetResponseComment.$empty(); + late GetResponseCommentAuthor author; late String comment; late DateTime posted; @@ -319,24 +365,38 @@ class GetResponseComment extends PackMeMessage { bytes += $stringBytes(comment); return bytes; } - + @override void $pack() { $packMessage(author); $packString(comment); $packDateTime(posted); } - + @override void $unpack() { - author = $unpackMessage(GetResponseCommentAuthor()) as GetResponseCommentAuthor; + author = $unpackMessage(GetResponseCommentAuthor.$empty()); comment = $unpackString(); posted = $unpackDateTime(); } - + + @override + String toString() { + return 'GetResponseComment\x1b[0m(author: ${PackMe.dye(author)}, comment: ${PackMe.dye(comment)}, posted: ${PackMe.dye(posted)})'; + } } class GetResponse extends PackMeMessage { + GetResponse({ + required this.title, + required this.content, + required this.posted, + required this.author, + required this.stats, + required this.comments, + }); + GetResponse.$empty(); + late String title; late String content; late DateTime posted; @@ -356,7 +416,7 @@ class GetResponse extends PackMeMessage { for (int i = 0; i < comments.length; i++) bytes += comments[i].$estimate(); return bytes; } - + @override void $pack() { $initPack(244485545); @@ -366,36 +426,51 @@ class GetResponse extends PackMeMessage { $packMessage(author); $packMessage(stats); $packUint32(comments.length); - comments.forEach($packMessage); + for (final GetResponseComment item in comments) $packMessage(item); } - + @override void $unpack() { $initUnpack(); title = $unpackString(); content = $unpackString(); posted = $unpackDateTime(); - author = $unpackMessage(GetResponseAuthor()) as GetResponseAuthor; - stats = $unpackMessage(GetResponseStats()) as GetResponseStats; + author = $unpackMessage(GetResponseAuthor.$empty()); + stats = $unpackMessage(GetResponseStats.$empty()); comments = []; final int commentsLength = $unpackUint32(); for (int i = 0; i < commentsLength; i++) { - comments.add($unpackMessage(GetResponseComment()) as GetResponseComment); + comments.add($unpackMessage(GetResponseComment.$empty())); } } - + + @override + String toString() { + return 'GetResponse\x1b[0m(title: ${PackMe.dye(title)}, content: ${PackMe.dye(content)}, posted: ${PackMe.dye(posted)}, author: ${PackMe.dye(author)}, stats: ${PackMe.dye(stats)}, comments: ${PackMe.dye(comments)})'; + } } -class DeleteRequest extends PackMeMessage { +class GetRequest extends PackMeMessage { + GetRequest({ + required this.postId, + }); + GetRequest.$empty(); + late List postId; - @override - DeleteResponse get $response { - final DeleteResponse message = DeleteResponse(); + GetResponse $response({ + required String title, + required String content, + required DateTime posted, + required GetResponseAuthor author, + required GetResponseStats stats, + required List comments, + }) { + final GetResponse message = GetResponse(title: title, content: content, posted: posted, author: author, stats: stats, comments: comments); message.$request = this; return message; } - + @override int $estimate() { $reset(); @@ -404,14 +479,14 @@ class DeleteRequest extends PackMeMessage { bytes += 1 * postId.length; return bytes; } - + @override void $pack() { - $initPack(486637631); + $initPack(187698222); $packUint32(postId.length); - postId.forEach($packUint8); + for (final int item in postId) $packUint8(item); } - + @override void $unpack() { $initUnpack(); @@ -421,10 +496,19 @@ class DeleteRequest extends PackMeMessage { postId.add($unpackUint8()); } } - + + @override + String toString() { + return 'GetRequest\x1b[0m(postId: ${PackMe.dye(postId)})'; + } } class DeleteResponse extends PackMeMessage { + DeleteResponse({ + this.error, + }); + DeleteResponse.$empty(); + String? error; @override @@ -437,14 +521,14 @@ class DeleteResponse extends PackMeMessage { } return bytes; } - + @override void $pack() { $initPack(788388804); for (int i = 0; i < 1; i++) $packUint8($flags[i]); if (error != null) $packString(error!); } - + @override void $unpack() { $initUnpack(); @@ -453,14 +537,66 @@ class DeleteResponse extends PackMeMessage { error = $unpackString(); } } + + @override + String toString() { + return 'DeleteResponse\x1b[0m(error: ${PackMe.dye(error)})'; + } +} + +class DeleteRequest extends PackMeMessage { + DeleteRequest({ + required this.postId, + }); + DeleteRequest.$empty(); + + late List postId; + DeleteResponse $response({ + String? error, + }) { + final DeleteResponse message = DeleteResponse(error: error); + message.$request = this; + return message; + } + + @override + int $estimate() { + $reset(); + int bytes = 8; + bytes += 4; + bytes += 1 * postId.length; + return bytes; + } + + @override + void $pack() { + $initPack(486637631); + $packUint32(postId.length); + for (final int item in postId) $packUint8(item); + } + + @override + void $unpack() { + $initUnpack(); + postId = []; + final int postIdLength = $unpackUint32(); + for (int i = 0; i < postIdLength; i++) { + postId.add($unpackUint8()); + } + } + + @override + String toString() { + return 'DeleteRequest\x1b[0m(postId: ${PackMe.dye(postId)})'; + } } final Map examplePostsMessageFactory = { - 63570112: () => GetAllRequest(), - 280110613: () => GetAllResponse(), - 187698222: () => GetRequest(), - 244485545: () => GetResponse(), - 486637631: () => DeleteRequest(), - 788388804: () => DeleteResponse(), + 280110613: () => GetAllResponse.$empty(), + 63570112: () => GetAllRequest.$empty(), + 244485545: () => GetResponse.$empty(), + 187698222: () => GetRequest.$empty(), + 788388804: () => DeleteResponse.$empty(), + 486637631: () => DeleteRequest.$empty(), }; \ No newline at end of file diff --git a/example/generated/example-users.generated.dart b/example/generated/example-users.generated.dart index 3fc82fe..2c5f980 100644 --- a/example/generated/example-users.generated.dart +++ b/example/generated/example-users.generated.dart @@ -1,34 +1,16 @@ +import 'dart:typed_data'; import 'package:packme/packme.dart'; -class GetAllRequest extends PackMeMessage { - - @override - GetAllResponse get $response { - final GetAllResponse message = GetAllResponse(); - message.$request = this; - return message; - } - - @override - int $estimate() { - $reset(); - int bytes = 8; - return bytes; - } - - @override - void $pack() { - $initPack(12982278); - } - - @override - void $unpack() { - $initUnpack(); - } - -} - class GetAllResponseUser extends PackMeMessage { + GetAllResponseUser({ + required this.id, + required this.nickname, + this.firstName, + this.lastName, + this.age, + }); + GetAllResponseUser.$empty(); + late List id; late String nickname; String? firstName; @@ -56,18 +38,18 @@ class GetAllResponseUser extends PackMeMessage { } return bytes; } - + @override void $pack() { for (int i = 0; i < 1; i++) $packUint8($flags[i]); $packUint32(id.length); - id.forEach($packUint8); + for (final int item in id) $packUint8(item); $packString(nickname); if (firstName != null) $packString(firstName!); if (lastName != null) $packString(lastName!); if (age != null) $packUint8(age!); } - + @override void $unpack() { for (int i = 0; i < 1; i++) $flags.add($unpackUint8()); @@ -87,10 +69,19 @@ class GetAllResponseUser extends PackMeMessage { age = $unpackUint8(); } } - + + @override + String toString() { + return 'GetAllResponseUser\x1b[0m(id: ${PackMe.dye(id)}, nickname: ${PackMe.dye(nickname)}, firstName: ${PackMe.dye(firstName)}, lastName: ${PackMe.dye(lastName)}, age: ${PackMe.dye(age)})'; + } } class GetAllResponse extends PackMeMessage { + GetAllResponse({ + required this.users, + }); + GetAllResponse.$empty(); + late List users; @override @@ -101,65 +92,76 @@ class GetAllResponse extends PackMeMessage { for (int i = 0; i < users.length; i++) bytes += users[i].$estimate(); return bytes; } - + @override void $pack() { $initPack(242206268); $packUint32(users.length); - users.forEach($packMessage); + for (final GetAllResponseUser item in users) $packMessage(item); } - + @override void $unpack() { $initUnpack(); users = []; final int usersLength = $unpackUint32(); for (int i = 0; i < usersLength; i++) { - users.add($unpackMessage(GetAllResponseUser()) as GetAllResponseUser); + users.add($unpackMessage(GetAllResponseUser.$empty())); } } - + + @override + String toString() { + return 'GetAllResponse\x1b[0m(users: ${PackMe.dye(users)})'; + } } -class GetRequest extends PackMeMessage { - late List userId; +class GetAllRequest extends PackMeMessage { + GetAllRequest(); + GetAllRequest.$empty(); + - @override - GetResponse get $response { - final GetResponse message = GetResponse(); + GetAllResponse $response({ + required List users, + }) { + final GetAllResponse message = GetAllResponse(users: users); message.$request = this; return message; } - + @override int $estimate() { $reset(); int bytes = 8; - bytes += 4; - bytes += 1 * userId.length; return bytes; } - + @override void $pack() { - $initPack(781905656); - $packUint32(userId.length); - userId.forEach($packUint8); + $initPack(12982278); } - + @override void $unpack() { $initUnpack(); - userId = []; - final int userIdLength = $unpackUint32(); - for (int i = 0; i < userIdLength; i++) { - userId.add($unpackUint8()); - } } - + + @override + String toString() { + return 'GetAllRequest\x1b[0m()'; + } } class GetResponseInfo extends PackMeMessage { + GetResponseInfo({ + this.firstName, + this.lastName, + this.male, + this.age, + this.birthDate, + }); + GetResponseInfo.$empty(); + String? firstName; String? lastName; int? male; @@ -192,7 +194,7 @@ class GetResponseInfo extends PackMeMessage { } return bytes; } - + @override void $pack() { for (int i = 0; i < 1; i++) $packUint8($flags[i]); @@ -202,7 +204,7 @@ class GetResponseInfo extends PackMeMessage { if (age != null) $packUint8(age!); if (birthDate != null) $packDateTime(birthDate!); } - + @override void $unpack() { for (int i = 0; i < 1; i++) $flags.add($unpackUint8()); @@ -222,10 +224,21 @@ class GetResponseInfo extends PackMeMessage { birthDate = $unpackDateTime(); } } - + + @override + String toString() { + return 'GetResponseInfo\x1b[0m(firstName: ${PackMe.dye(firstName)}, lastName: ${PackMe.dye(lastName)}, male: ${PackMe.dye(male)}, age: ${PackMe.dye(age)}, birthDate: ${PackMe.dye(birthDate)})'; + } } class GetResponseSocial extends PackMeMessage { + GetResponseSocial({ + this.facebookId, + this.twitterId, + this.instagramId, + }); + GetResponseSocial.$empty(); + String? facebookId; String? twitterId; String? instagramId; @@ -248,7 +261,7 @@ class GetResponseSocial extends PackMeMessage { } return bytes; } - + @override void $pack() { for (int i = 0; i < 1; i++) $packUint8($flags[i]); @@ -256,7 +269,7 @@ class GetResponseSocial extends PackMeMessage { if (twitterId != null) $packString(twitterId!); if (instagramId != null) $packString(instagramId!); } - + @override void $unpack() { for (int i = 0; i < 1; i++) $flags.add($unpackUint8()); @@ -270,10 +283,23 @@ class GetResponseSocial extends PackMeMessage { instagramId = $unpackString(); } } - + + @override + String toString() { + return 'GetResponseSocial\x1b[0m(facebookId: ${PackMe.dye(facebookId)}, twitterId: ${PackMe.dye(twitterId)}, instagramId: ${PackMe.dye(instagramId)})'; + } } class GetResponseStats extends PackMeMessage { + GetResponseStats({ + required this.posts, + required this.comments, + required this.likes, + required this.dislikes, + required this.rating, + }); + GetResponseStats.$empty(); + late int posts; late int comments; late int likes; @@ -286,7 +312,7 @@ class GetResponseStats extends PackMeMessage { int bytes = 20; return bytes; } - + @override void $pack() { $packUint32(posts); @@ -295,7 +321,7 @@ class GetResponseStats extends PackMeMessage { $packUint32(dislikes); $packFloat(rating); } - + @override void $unpack() { posts = $unpackUint32(); @@ -304,10 +330,20 @@ class GetResponseStats extends PackMeMessage { dislikes = $unpackUint32(); rating = $unpackFloat(); } - + + @override + String toString() { + return 'GetResponseStats\x1b[0m(posts: ${PackMe.dye(posts)}, comments: ${PackMe.dye(comments)}, likes: ${PackMe.dye(likes)}, dislikes: ${PackMe.dye(dislikes)}, rating: ${PackMe.dye(rating)})'; + } } class GetResponseLastActive extends PackMeMessage { + GetResponseLastActive({ + required this.datetime, + required this.ip, + }); + GetResponseLastActive.$empty(); + late DateTime datetime; late String ip; @@ -318,22 +354,33 @@ class GetResponseLastActive extends PackMeMessage { bytes += $stringBytes(ip); return bytes; } - + @override void $pack() { $packDateTime(datetime); $packString(ip); } - + @override void $unpack() { datetime = $unpackDateTime(); ip = $unpackString(); } - + + @override + String toString() { + return 'GetResponseLastActive\x1b[0m(datetime: ${PackMe.dye(datetime)}, ip: ${PackMe.dye(ip)})'; + } } class GetResponseSession extends PackMeMessage { + GetResponseSession({ + required this.created, + required this.ip, + required this.active, + }); + GetResponseSession.$empty(); + late DateTime created; late String ip; late bool active; @@ -345,24 +392,41 @@ class GetResponseSession extends PackMeMessage { bytes += $stringBytes(ip); return bytes; } - + @override void $pack() { $packDateTime(created); $packString(ip); $packBool(active); } - + @override void $unpack() { created = $unpackDateTime(); ip = $unpackString(); active = $unpackBool(); } - + + @override + String toString() { + return 'GetResponseSession\x1b[0m(created: ${PackMe.dye(created)}, ip: ${PackMe.dye(ip)}, active: ${PackMe.dye(active)})'; + } } class GetResponse extends PackMeMessage { + GetResponse({ + required this.email, + required this.nickname, + required this.hidden, + required this.created, + required this.info, + required this.social, + required this.stats, + this.lastActive, + required this.sessions, + }); + GetResponse.$empty(); + late String email; late String nickname; late bool hidden; @@ -390,7 +454,7 @@ class GetResponse extends PackMeMessage { for (int i = 0; i < sessions.length; i++) bytes += sessions[i].$estimate(); return bytes; } - + @override void $pack() { $initPack(430536944); @@ -404,9 +468,9 @@ class GetResponse extends PackMeMessage { $packMessage(stats); if (lastActive != null) $packMessage(lastActive!); $packUint32(sessions.length); - sessions.forEach($packMessage); + for (final GetResponseSession item in sessions) $packMessage(item); } - + @override void $unpack() { $initUnpack(); @@ -415,31 +479,49 @@ class GetResponse extends PackMeMessage { nickname = $unpackString(); hidden = $unpackBool(); created = $unpackDateTime(); - info = $unpackMessage(GetResponseInfo()) as GetResponseInfo; - social = $unpackMessage(GetResponseSocial()) as GetResponseSocial; - stats = $unpackMessage(GetResponseStats()) as GetResponseStats; + info = $unpackMessage(GetResponseInfo.$empty()); + social = $unpackMessage(GetResponseSocial.$empty()); + stats = $unpackMessage(GetResponseStats.$empty()); if ($getFlag()) { - lastActive = $unpackMessage(GetResponseLastActive()) as GetResponseLastActive; + lastActive = $unpackMessage(GetResponseLastActive.$empty()); } sessions = []; final int sessionsLength = $unpackUint32(); for (int i = 0; i < sessionsLength; i++) { - sessions.add($unpackMessage(GetResponseSession()) as GetResponseSession); + sessions.add($unpackMessage(GetResponseSession.$empty())); } } - + + @override + String toString() { + return 'GetResponse\x1b[0m(email: ${PackMe.dye(email)}, nickname: ${PackMe.dye(nickname)}, hidden: ${PackMe.dye(hidden)}, created: ${PackMe.dye(created)}, info: ${PackMe.dye(info)}, social: ${PackMe.dye(social)}, stats: ${PackMe.dye(stats)}, lastActive: ${PackMe.dye(lastActive)}, sessions: ${PackMe.dye(sessions)})'; + } } -class DeleteRequest extends PackMeMessage { +class GetRequest extends PackMeMessage { + GetRequest({ + required this.userId, + }); + GetRequest.$empty(); + late List userId; - @override - DeleteResponse get $response { - final DeleteResponse message = DeleteResponse(); + GetResponse $response({ + required String email, + required String nickname, + required bool hidden, + required DateTime created, + required GetResponseInfo info, + required GetResponseSocial social, + required GetResponseStats stats, + GetResponseLastActive? lastActive, + required List sessions, + }) { + final GetResponse message = GetResponse(email: email, nickname: nickname, hidden: hidden, created: created, info: info, social: social, stats: stats, lastActive: lastActive, sessions: sessions); message.$request = this; return message; } - + @override int $estimate() { $reset(); @@ -448,14 +530,14 @@ class DeleteRequest extends PackMeMessage { bytes += 1 * userId.length; return bytes; } - + @override void $pack() { - $initPack(808423104); + $initPack(781905656); $packUint32(userId.length); - userId.forEach($packUint8); + for (final int item in userId) $packUint8(item); } - + @override void $unpack() { $initUnpack(); @@ -465,10 +547,19 @@ class DeleteRequest extends PackMeMessage { userId.add($unpackUint8()); } } - + + @override + String toString() { + return 'GetRequest\x1b[0m(userId: ${PackMe.dye(userId)})'; + } } class DeleteResponse extends PackMeMessage { + DeleteResponse({ + this.error, + }); + DeleteResponse.$empty(); + String? error; @override @@ -481,14 +572,14 @@ class DeleteResponse extends PackMeMessage { } return bytes; } - + @override void $pack() { $initPack(69897231); for (int i = 0; i < 1; i++) $packUint8($flags[i]); if (error != null) $packString(error!); } - + @override void $unpack() { $initUnpack(); @@ -497,14 +588,66 @@ class DeleteResponse extends PackMeMessage { error = $unpackString(); } } + + @override + String toString() { + return 'DeleteResponse\x1b[0m(error: ${PackMe.dye(error)})'; + } +} + +class DeleteRequest extends PackMeMessage { + DeleteRequest({ + required this.userId, + }); + DeleteRequest.$empty(); + + late List userId; + DeleteResponse $response({ + String? error, + }) { + final DeleteResponse message = DeleteResponse(error: error); + message.$request = this; + return message; + } + + @override + int $estimate() { + $reset(); + int bytes = 8; + bytes += 4; + bytes += 1 * userId.length; + return bytes; + } + + @override + void $pack() { + $initPack(808423104); + $packUint32(userId.length); + for (final int item in userId) $packUint8(item); + } + + @override + void $unpack() { + $initUnpack(); + userId = []; + final int userIdLength = $unpackUint32(); + for (int i = 0; i < userIdLength; i++) { + userId.add($unpackUint8()); + } + } + + @override + String toString() { + return 'DeleteRequest\x1b[0m(userId: ${PackMe.dye(userId)})'; + } } final Map exampleUsersMessageFactory = { - 12982278: () => GetAllRequest(), - 242206268: () => GetAllResponse(), - 781905656: () => GetRequest(), - 430536944: () => GetResponse(), - 808423104: () => DeleteRequest(), - 69897231: () => DeleteResponse(), + 242206268: () => GetAllResponse.$empty(), + 12982278: () => GetAllRequest.$empty(), + 430536944: () => GetResponse.$empty(), + 781905656: () => GetRequest.$empty(), + 69897231: () => DeleteResponse.$empty(), + 808423104: () => DeleteRequest.$empty(), }; \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 102c6c1..b06517e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: serveme description: Backend server framework designed for a quick development of modular WebSocket based server applications with MongoDB integration. -version: 1.1.3 +version: 1.2.0 homepage: https://github.com/sourcecaster/serveme repository: https://github.com/sourcecaster/serveme issue_tracker: https://github.com/sourcecaster/serveme/issues @@ -10,7 +10,7 @@ environment: dependencies: mongo_dart: ^0.7.0+2 yaml: ^3.1.0 - connectme: ^2.0.2 - packme: ^1.1.5 + connectme: ^2.1.0 + packme: ^1.2.0 dev_dependencies: test: ^1.17.11 \ No newline at end of file diff --git a/test/generated/test.generated.dart b/test/generated/test.generated.dart index 9641a50..61d44b2 100644 --- a/test/generated/test.generated.dart +++ b/test/generated/test.generated.dart @@ -1,3 +1,4 @@ +import 'dart:typed_data'; import 'package:packme/packme.dart'; class TestResponse extends PackMeMessage {