├── .gitignore ├── LICENSE ├── README.md ├── README.zh_CN.md ├── addins ├── broker │ ├── broker.go │ ├── broker_helper.go │ ├── define.go │ ├── nats_broker │ │ ├── broker.go │ │ ├── broker_options.go │ │ ├── define.go │ │ └── subscriber.go │ ├── subscriber.go │ └── subscriber_options.go ├── conf │ ├── config.go │ ├── config_options.go │ └── define.go ├── db │ ├── db.go │ ├── dbtypes │ │ └── dbtypes.go │ ├── injectdb.go │ ├── migratedb.go │ ├── mongodb │ │ ├── define.go │ │ ├── mongo.go │ │ └── mongo_options.go │ ├── redisdb │ │ ├── define.go │ │ ├── redis.go │ │ └── redis_options.go │ └── sqldb │ │ ├── define.go │ │ ├── sqldb.go │ │ └── sqldb_options.go ├── dentq │ ├── define.go │ ├── distentityquerier.go │ ├── distentityquerier_options.go │ └── generated.deepcopy.go ├── dentr │ ├── define.go │ ├── distentityregistry.go │ ├── distentityregistry_event.gen.go │ ├── distentityregistry_event.go │ ├── distentityregistry_event.tab.gen.go │ └── distentityregistry_options.go ├── discovery │ ├── cache_discovery │ │ ├── define.go │ │ ├── registry.go │ │ └── registry_options.go │ ├── define.go │ ├── etcd_discovery │ │ ├── define.go │ │ ├── registry.go │ │ ├── registry_options.go │ │ └── watcher.go │ ├── generated.deepcopy.go │ ├── redis_discovery │ │ ├── define.go │ │ ├── registry.go │ │ ├── registry_options.go │ │ └── watcher.go │ ├── registry.go │ └── watcher.go ├── dsvc │ ├── define.go │ ├── details.go │ ├── distservice.go │ ├── distservice_msgwatcher.go │ ├── distservice_options.go │ └── distservice_running.go ├── dsync │ ├── define.go │ ├── dmutex.go │ ├── dmutex_options.go │ ├── dsync.go │ ├── etcd_dsync │ │ ├── define.go │ │ ├── dmutex.go │ │ ├── dsync.go │ │ └── dsync_options.go │ └── redis_dsync │ │ ├── define.go │ │ ├── dmutex.go │ │ ├── dsync.go │ │ └── dsync_options.go ├── gate │ ├── acceptor.go │ ├── acceptor_handshake.go │ ├── cli │ │ ├── client.go │ │ ├── client_datawatcher.go │ │ ├── client_eventwatcher.go │ │ ├── client_options.go │ │ ├── client_running.go │ │ ├── client_synctime.go │ │ ├── connect.go │ │ ├── connector.go │ │ ├── connector_handshake.go │ │ ├── dialer_socketopt_nonwindows.go │ │ └── dialer_socketopt_windows.go │ ├── define.go │ ├── details.go │ ├── gate.go │ ├── gate_handlesession.go │ ├── gate_options.go │ ├── gate_sessionwatcher.go │ ├── listener_socketopt_nonwindows.go │ ├── listener_socketopt_windows.go │ ├── session.go │ ├── session_datawatcher.go │ ├── session_eventwatcher.go │ ├── session_options.go │ ├── session_running.go │ ├── session_settings.go │ ├── sessionstate_string.go │ └── websocket.go ├── log │ ├── console_log │ │ ├── define.go │ │ ├── logger.go │ │ └── logger_options.go │ ├── define.go │ ├── logger.go │ ├── logger_helper.go │ └── zap_log │ │ ├── define.go │ │ ├── logger.go │ │ ├── logger_options.go │ │ └── zaplogger.go ├── router │ ├── define.go │ ├── group.go │ ├── mapping.go │ ├── router.go │ ├── router_group.go │ └── router_options.go ├── rpc │ ├── callpath │ │ ├── cache.go │ │ └── callpath.go │ ├── define.go │ ├── proxy_const.go │ ├── proxy_entity.go │ ├── proxy_group.go │ ├── proxy_runtime.go │ ├── proxy_service.go │ ├── rpc.go │ ├── rpc_options.go │ ├── rpc_result.go │ ├── rpc_result_assert.go │ ├── rpcli │ │ ├── cachecp.go │ │ ├── creator.go │ │ ├── procedure.go │ │ ├── rpcli.go │ │ ├── rpcli_dispatcher.go │ │ └── rpcli_procedure.go │ └── rpcpcsr │ │ ├── callmethod.go │ │ ├── forward.go │ │ ├── forward_in_dispatcher.go │ │ ├── forward_out_deliverer.go │ │ ├── gate.go │ │ ├── gate_in_dispatcher.go │ │ ├── gate_out_dispatcher.go │ │ ├── permission_validator.go │ │ ├── processor.go │ │ ├── processor_lifecycle.go │ │ ├── service.go │ │ ├── service_deliverer.go │ │ └── service_dispatcher.go └── rpcstack │ ├── define.go │ ├── rpcstack.go │ ├── rpcstack_types.go │ └── unsafe_rpcstack.go ├── app.go ├── cachecp.go ├── comp.go ├── comp_async.go ├── comp_await.go ├── comp_rpc.go ├── doc.go ├── ec.go ├── ec_await.go ├── entity.go ├── entity_async.go ├── entity_await.go ├── entity_rpc.go ├── entitycreator.go ├── entityptcreator.go ├── errors.go ├── go.mod ├── go.sum ├── net ├── gap │ ├── codec │ │ ├── codec_decoder.go │ │ └── codec_encoder.go │ ├── doc.go │ ├── marshaler.go │ ├── msg.go │ ├── msg_forward.go │ ├── msg_onewayrpc.go │ ├── msg_rpc_reply.go │ ├── msg_rpc_request.go │ ├── msgcreator.go │ ├── msghead.go │ ├── msgid.go │ ├── msgid_builtin.go │ ├── msgpacket.go │ ├── serializedmsg.go │ └── variant │ │ ├── serializedvalue.go │ │ ├── typeid.go │ │ ├── typeid_builtin.go │ │ ├── value.go │ │ ├── value_builtin_array.go │ │ ├── value_builtin_array_readonly.go │ │ ├── value_builtin_array_serialized.go │ │ ├── value_builtin_bool.go │ │ ├── value_builtin_byte.go │ │ ├── value_builtin_bytes.go │ │ ├── value_builtin_callchain.go │ │ ├── value_builtin_double.go │ │ ├── value_builtin_error.go │ │ ├── value_builtin_float.go │ │ ├── value_builtin_int.go │ │ ├── value_builtin_int16.go │ │ ├── value_builtin_int32.go │ │ ├── value_builtin_int64.go │ │ ├── value_builtin_int8.go │ │ ├── value_builtin_map.go │ │ ├── value_builtin_map_readonly.go │ │ ├── value_builtin_map_serialized.go │ │ ├── value_builtin_null.go │ │ ├── value_builtin_string.go │ │ ├── value_builtin_uint.go │ │ ├── value_builtin_uint16.go │ │ ├── value_builtin_uint32.go │ │ ├── value_builtin_uint64.go │ │ ├── value_builtin_uint8.go │ │ ├── variant.go │ │ ├── variant_convert.go │ │ ├── variant_readonly.go │ │ ├── variant_readonly_cast.go │ │ ├── variant_serialized.go │ │ ├── variant_serialized_cast.go │ │ └── variantcreator.go ├── gtp │ ├── codec │ │ ├── codec_decoder.go │ │ ├── codec_encoder.go │ │ ├── codec_test.go │ │ ├── module_compression.go │ │ ├── module_encryption.go │ │ ├── module_mac.go │ │ ├── module_mac32.go │ │ └── module_mac64.go │ ├── common.go │ ├── doc.go │ ├── marshaler.go │ ├── method │ │ ├── asymmetricencryption.go │ │ ├── compression.go │ │ ├── errors.go │ │ ├── hash.go │ │ ├── namedcurve.go │ │ ├── padding.go │ │ └── symmetricencryption.go │ ├── msg.go │ ├── msg_auth.go │ ├── msg_changecipherspec.go │ ├── msg_continue.go │ ├── msg_ecdhesecretkeyexchange.go │ ├── msg_finished.go │ ├── msg_heartbeat.go │ ├── msg_hello.go │ ├── msg_payload.go │ ├── msg_rst.go │ ├── msg_synctime.go │ ├── msgcompressed.go │ ├── msgcreator.go │ ├── msghead.go │ ├── msgid.go │ ├── msgid_builtin.go │ ├── msgmac.go │ ├── msgpacket.go │ ├── sign.go │ ├── sign │ │ └── main.go │ └── transport │ │ ├── event.go │ │ ├── event_dispatcher.go │ │ ├── protocol.go │ │ ├── protocol_handshake.go │ │ ├── protocol_test.go │ │ ├── protocol_trans.go │ │ ├── ptotocol_ctrl.go │ │ ├── rsterror.go │ │ ├── synchronizer.go │ │ ├── synchronizer_sequenced.go │ │ ├── synchronizer_unsequenced.go │ │ ├── transceiver.go │ │ └── transceiver_retry.go └── netpath │ ├── details.go │ └── path.go ├── runtime.go ├── runtime_addin.go ├── runtime_generic.go ├── runtime_instantiation.go ├── runtime_lifecycle.go ├── runtimecreator.go ├── service.go ├── service_addin.go ├── service_generic.go ├── service_instantiation.go ├── service_lifecycle.go └── utils ├── binaryutil ├── bytespool.go ├── bytestream.go ├── byteswriter.go ├── copy.go ├── limitwriter.go └── sizeof.go └── concurrent ├── cache.go ├── deduplicator.go ├── futures.go ├── futures_future.go ├── futures_resp.go ├── futures_respasyncret.go ├── futures_respasyncrett.go ├── futures_respfunc.go ├── futures_task.go ├── locked.go ├── lockedmap.go ├── lockedslice.go ├── rwlocked.go └── watcher.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | -------------------------------------------------------------------------------- /addins/broker/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package broker 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddInInterface[IBroker]() 26 | Name = self.Name 27 | Using = self.Using 28 | ) 29 | -------------------------------------------------------------------------------- /addins/broker/nats_broker/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package nats_broker 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newBroker) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/conf/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package conf 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newConfig) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/db/db.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package db 21 | 22 | import ( 23 | "git.golaxy.org/core/service" 24 | "git.golaxy.org/framework/addins/db/mongodb" 25 | "git.golaxy.org/framework/addins/db/redisdb" 26 | "git.golaxy.org/framework/addins/db/sqldb" 27 | "github.com/redis/go-redis/v9" 28 | "go.mongodb.org/mongo-driver/mongo" 29 | "gorm.io/gorm" 30 | ) 31 | 32 | func SQLDB(svcCtx service.Context, tag string) *gorm.DB { 33 | return sqldb.Using(svcCtx).SQLDB(tag) 34 | } 35 | 36 | func RedisDB(svcCtx service.Context, tag string) *redis.Client { 37 | return redisdb.Using(svcCtx).RedisDB(tag) 38 | } 39 | 40 | func MongoDB(svcCtx service.Context, tag string) *mongo.Client { 41 | return mongodb.Using(svcCtx).MongoDB(tag) 42 | } 43 | -------------------------------------------------------------------------------- /addins/db/dbtypes/dbtypes.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dbtypes 21 | 22 | const ( 23 | MySQL = "mysql" 24 | PostgreSQL = "postgresql" 25 | SQLServer = "sqlserver" 26 | SQLite = "sqlite" 27 | Redis = "redis" 28 | MongoDB = "mongodb" 29 | ) 30 | 31 | type DBInfo struct { 32 | Tag string `json:"tag,omitempty"` 33 | Type string `json:"type,omitempty"` 34 | ConnStr string `json:"conn_str,omitempty"` 35 | } 36 | -------------------------------------------------------------------------------- /addins/db/migratedb.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package db 21 | 22 | type IMigrateDB interface { 23 | MigrateDB() error 24 | } 25 | 26 | func MigrateDB(services ...any) error { 27 | for _, service := range services { 28 | migrateDB, ok := service.(IMigrateDB) 29 | if !ok { 30 | continue 31 | } 32 | if err := migrateDB.MigrateDB(); err != nil { 33 | return err 34 | } 35 | } 36 | return nil 37 | } 38 | -------------------------------------------------------------------------------- /addins/db/mongodb/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package mongodb 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newMongoDB) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/db/mongodb/mongo_options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package mongodb 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/exception" 24 | "git.golaxy.org/core/utils/option" 25 | "git.golaxy.org/framework/addins/db/dbtypes" 26 | "github.com/elliotchance/pie/v2" 27 | ) 28 | 29 | type MongoDBOptions struct { 30 | DBInfos []*dbtypes.DBInfo 31 | } 32 | 33 | var With _Option 34 | 35 | type _Option struct{} 36 | 37 | func (_Option) Default() option.Setting[MongoDBOptions] { 38 | return func(options *MongoDBOptions) { 39 | With.DBInfos().Apply(options) 40 | } 41 | } 42 | 43 | func (_Option) DBInfos(infos ...*dbtypes.DBInfo) option.Setting[MongoDBOptions] { 44 | return func(options *MongoDBOptions) { 45 | infos = pie.Filter(infos, func(info *dbtypes.DBInfo) bool { 46 | if info == nil { 47 | return false 48 | } 49 | switch info.Type { 50 | case dbtypes.MongoDB: 51 | return true 52 | } 53 | return false 54 | }) 55 | 56 | if len(infos) != len(pie.Map(infos, func(info *dbtypes.DBInfo) string { return info.Tag })) { 57 | exception.Panicf("db: %w: tags in db infos must be unique", exception.ErrArgs) 58 | } 59 | 60 | options.DBInfos = infos 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /addins/db/redisdb/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package redisdb 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newRedisDB) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/db/redisdb/redis_options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package redisdb 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/exception" 24 | "git.golaxy.org/core/utils/option" 25 | "git.golaxy.org/framework/addins/db/dbtypes" 26 | "github.com/elliotchance/pie/v2" 27 | ) 28 | 29 | type RedisDBOptions struct { 30 | DBInfos []*dbtypes.DBInfo 31 | } 32 | 33 | var With _Option 34 | 35 | type _Option struct{} 36 | 37 | func (_Option) Default() option.Setting[RedisDBOptions] { 38 | return func(options *RedisDBOptions) { 39 | With.DBInfos().Apply(options) 40 | } 41 | } 42 | 43 | func (_Option) DBInfos(infos ...*dbtypes.DBInfo) option.Setting[RedisDBOptions] { 44 | return func(options *RedisDBOptions) { 45 | infos = pie.Filter(infos, func(info *dbtypes.DBInfo) bool { 46 | if info == nil { 47 | return false 48 | } 49 | switch info.Type { 50 | case dbtypes.Redis: 51 | return true 52 | } 53 | return false 54 | }) 55 | 56 | if len(infos) != len(pie.Map(infos, func(info *dbtypes.DBInfo) string { return info.Tag })) { 57 | exception.Panicf("db: %w: tags in db infos must be unique", exception.ErrArgs) 58 | } 59 | 60 | options.DBInfos = infos 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /addins/db/sqldb/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package sqldb 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newSQLDB) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/db/sqldb/sqldb_options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package sqldb 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/exception" 24 | "git.golaxy.org/core/utils/option" 25 | "git.golaxy.org/framework/addins/db/dbtypes" 26 | "github.com/elliotchance/pie/v2" 27 | ) 28 | 29 | type SQLDBOptions struct { 30 | DBInfos []*dbtypes.DBInfo 31 | } 32 | 33 | var With _Option 34 | 35 | type _Option struct{} 36 | 37 | func (_Option) Default() option.Setting[SQLDBOptions] { 38 | return func(options *SQLDBOptions) { 39 | With.DBInfos().Apply(options) 40 | } 41 | } 42 | 43 | func (_Option) DBInfos(infos ...*dbtypes.DBInfo) option.Setting[SQLDBOptions] { 44 | return func(options *SQLDBOptions) { 45 | infos = pie.Filter(infos, func(info *dbtypes.DBInfo) bool { 46 | if info == nil { 47 | return false 48 | } 49 | switch info.Type { 50 | case dbtypes.MySQL, dbtypes.PostgreSQL, dbtypes.SQLServer, dbtypes.SQLite: 51 | return true 52 | } 53 | return false 54 | }) 55 | 56 | if len(infos) != len(pie.Map(infos, func(info *dbtypes.DBInfo) string { return info.Tag })) { 57 | exception.Panicf("db: %w: tags in db infos must be unique", exception.ErrArgs) 58 | } 59 | 60 | options.DBInfos = infos 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /addins/dentq/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dentq 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newDistEntityQuerier) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/dentq/generated.deepcopy.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by deepcopy-gen. DO NOT EDIT. 5 | 6 | package dentq 7 | 8 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 9 | func (in *DistEntity) DeepCopyInto(out *DistEntity) { 10 | *out = *in 11 | if in.Nodes != nil { 12 | in, out := &in.Nodes, &out.Nodes 13 | *out = make([]Node, len(*in)) 14 | copy(*out, *in) 15 | } 16 | return 17 | } 18 | 19 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DistEntity. 20 | func (in *DistEntity) DeepCopy() *DistEntity { 21 | if in == nil { 22 | return nil 23 | } 24 | out := new(DistEntity) 25 | in.DeepCopyInto(out) 26 | return out 27 | } 28 | 29 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 30 | func (in *Node) DeepCopyInto(out *Node) { 31 | *out = *in 32 | return 33 | } 34 | 35 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Node. 36 | func (in *Node) DeepCopy() *Node { 37 | if in == nil { 38 | return nil 39 | } 40 | out := new(Node) 41 | in.DeepCopyInto(out) 42 | return out 43 | } 44 | -------------------------------------------------------------------------------- /addins/dentr/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dentr 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.RuntimeAddIn(newDistEntityRegistry) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/dentr/distentityregistry_event.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | //go:generate go run git.golaxy.org/core/event/eventc event --default_export=false 21 | //go:generate go run git.golaxy.org/core/event/eventc eventtab --name=distEntityRegistryEventTab 22 | package dentr 23 | 24 | import ( 25 | "git.golaxy.org/core/ec" 26 | ) 27 | 28 | // EventDistEntityOnline 事件:分布式实体上线 29 | type EventDistEntityOnline interface { 30 | OnDistEntityOnline(entity ec.Entity) 31 | } 32 | 33 | // EventDistEntityOffline 事件:分布式实体下线 34 | type EventDistEntityOffline interface { 35 | OnDistEntityOffline(entity ec.Entity) 36 | } 37 | -------------------------------------------------------------------------------- /addins/discovery/cache_discovery/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cache_discovery 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newRegistry) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/discovery/cache_discovery/registry_options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cache_discovery 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/option" 24 | "git.golaxy.org/framework/addins/discovery" 25 | ) 26 | 27 | // RegistryOptions 所有选项 28 | type RegistryOptions struct { 29 | Registry discovery.IRegistry // 包装的其他registry插件 30 | } 31 | 32 | var With _Option 33 | 34 | type _Option struct{} 35 | 36 | // Default 默认值 37 | func (_Option) Default() option.Setting[RegistryOptions] { 38 | return func(options *RegistryOptions) { 39 | With.Wrap(nil).Apply(options) 40 | } 41 | } 42 | 43 | // Wrap 包装的其他registry插件 44 | func (_Option) Wrap(r discovery.IRegistry) option.Setting[RegistryOptions] { 45 | return func(options *RegistryOptions) { 46 | options.Registry = r 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /addins/discovery/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package discovery 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddInInterface[IRegistry]() 28 | Name = self.Name 29 | Using = self.Using 30 | ) 31 | -------------------------------------------------------------------------------- /addins/discovery/etcd_discovery/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package etcd_discovery 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(NewRegistry) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/discovery/generated.deepcopy.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by deepcopy-gen. DO NOT EDIT. 5 | 6 | package discovery 7 | 8 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 9 | func (in *Event) DeepCopyInto(out *Event) { 10 | *out = *in 11 | if in.Service != nil { 12 | in, out := &in.Service, &out.Service 13 | *out = new(Service) 14 | (*in).DeepCopyInto(*out) 15 | } 16 | return 17 | } 18 | 19 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Event. 20 | func (in *Event) DeepCopy() *Event { 21 | if in == nil { 22 | return nil 23 | } 24 | out := new(Event) 25 | in.DeepCopyInto(out) 26 | return out 27 | } 28 | 29 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 30 | func (in *Node) DeepCopyInto(out *Node) { 31 | *out = *in 32 | if in.Meta != nil { 33 | in, out := &in.Meta, &out.Meta 34 | *out = make(map[string]string, len(*in)) 35 | for key, val := range *in { 36 | (*out)[key] = val 37 | } 38 | } 39 | return 40 | } 41 | 42 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Node. 43 | func (in *Node) DeepCopy() *Node { 44 | if in == nil { 45 | return nil 46 | } 47 | out := new(Node) 48 | in.DeepCopyInto(out) 49 | return out 50 | } 51 | 52 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 53 | func (in *Service) DeepCopyInto(out *Service) { 54 | *out = *in 55 | if in.Nodes != nil { 56 | in, out := &in.Nodes, &out.Nodes 57 | *out = make([]Node, len(*in)) 58 | for i := range *in { 59 | (*in)[i].DeepCopyInto(&(*out)[i]) 60 | } 61 | } 62 | return 63 | } 64 | 65 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Service. 66 | func (in *Service) DeepCopy() *Service { 67 | if in == nil { 68 | return nil 69 | } 70 | out := new(Service) 71 | in.DeepCopyInto(out) 72 | return out 73 | } 74 | -------------------------------------------------------------------------------- /addins/discovery/redis_discovery/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package redis_discovery 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(NewRegistry) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/dsvc/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dsvc 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newDistService) 28 | Name = self.Name 29 | Using = self.Using 30 | Install = self.Install 31 | Uninstall = self.Uninstall 32 | ) 33 | -------------------------------------------------------------------------------- /addins/dsvc/details.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dsvc 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/core" 25 | "git.golaxy.org/core/utils/uid" 26 | "git.golaxy.org/framework/net/netpath" 27 | "unique" 28 | ) 29 | 30 | // NodeDetails 服务节点地址信息 31 | type NodeDetails struct { 32 | netpath.NodeDetails 33 | GlobalBroadcastAddr string // 全局广播地址 34 | GlobalBalanceAddr string // 全局负载均衡地址 35 | BroadcastAddr string // 服务广播地址 36 | BalanceAddr string // 服务负载均衡地址 37 | LocalAddr string // 本服务节点地址 38 | } 39 | 40 | // MakeBroadcastAddr 创建服务广播地址 41 | func (d *NodeDetails) MakeBroadcastAddr(service string) string { 42 | return unique.Make(d.DomainBroadcast.Join(service)).Value() 43 | } 44 | 45 | // MakeBalanceAddr 创建服务负载均衡地址 46 | func (d *NodeDetails) MakeBalanceAddr(service string) string { 47 | return unique.Make(d.DomainBalance.Join(service)).Value() 48 | } 49 | 50 | // MakeNodeAddr 创建服务节点地址 51 | func (d *NodeDetails) MakeNodeAddr(nodeId uid.Id) (string, error) { 52 | if nodeId.IsNil() { 53 | return "", fmt.Errorf("dsvc: %w: nodeId is nil", core.ErrArgs) 54 | } 55 | return unique.Make(d.DomainUnicast.Join(nodeId.String())).Value(), nil 56 | } 57 | -------------------------------------------------------------------------------- /addins/dsync/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dsync 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddInInterface[IDistSync]() 26 | Name = self.Name 27 | Using = self.Using 28 | ) 29 | -------------------------------------------------------------------------------- /addins/dsync/dmutex.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dsync 21 | 22 | import ( 23 | "context" 24 | "time" 25 | ) 26 | 27 | // A IDistMutex is a distributed mutual exclusion lock. Avoid sharing the same IDistMutex instance among multiple goroutines. Create a separate IDistMutex instance for each goroutine. 28 | type IDistMutex interface { 29 | // Name returns mutex name. 30 | Name() string 31 | // Value returns the current random value. The value will be empty until a lock is acquired (or Value option is used). 32 | Value() string 33 | // Until returns the time of validity of acquired lock. The value will be zero value until a lock is acquired. 34 | Until() time.Time 35 | // Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again. 36 | Lock(ctx context.Context) error 37 | // Unlock unlocks m and returns the status of unlock. 38 | Unlock(ctx context.Context) error 39 | // Extend resets the mutex's expiry and returns the status of expiry extension. 40 | Extend(ctx context.Context) error 41 | // Valid returns true if the lock acquired through m is still valid. It may also return true erroneously if quorum is achieved during the call and at 42 | // least one node then takes long enough to respond for the lock to expire. 43 | Valid(ctx context.Context) (bool, error) 44 | } 45 | -------------------------------------------------------------------------------- /addins/dsync/dsync.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package dsync 21 | 22 | import ( 23 | "errors" 24 | "git.golaxy.org/core/utils/option" 25 | ) 26 | 27 | var ( 28 | // ErrNotAcquired is an error indicating that the distributed lock was not acquired. It is returned by IDistMutex.Unlock and IDistMutex.Extend when the lock was not successfully acquired or has expired. 29 | ErrNotAcquired = errors.New("dsync: lock is not acquired") 30 | ) 31 | 32 | // IDistSync represents a distributed synchronization mechanism. 33 | type IDistSync interface { 34 | // NewMutex returns a new distributed mutex with given name. 35 | NewMutex(name string, settings ...option.Setting[DistMutexOptions]) IDistMutex 36 | // NewMutexf returns a new distributed mutex using a formatted string. 37 | NewMutexf(format string, args ...any) func(settings ...option.Setting[DistMutexOptions]) IDistMutex 38 | // NewMutexp returns a new distributed mutex using elements. 39 | NewMutexp(elems ...string) func(settings ...option.Setting[DistMutexOptions]) IDistMutex 40 | // GetSeparator return name path separator. 41 | GetSeparator() string 42 | } 43 | -------------------------------------------------------------------------------- /addins/dsync/etcd_dsync/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package etcd_dsync 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newDSync) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/dsync/redis_dsync/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package redis_dsync 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newDSync) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/gate/cli/client_datawatcher.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cli 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | func (c *Client) newDataWatcher(ctx context.Context, handler RecvDataHandler) *_DataWatcher { 28 | if ctx == nil { 29 | ctx = context.Background() 30 | } 31 | 32 | ctx, cancel := context.WithCancel(ctx) 33 | 34 | watcher := &_DataWatcher{ 35 | Context: ctx, 36 | terminate: cancel, 37 | terminated: async.MakeAsyncRet(), 38 | client: c, 39 | handler: handler, 40 | } 41 | c.dataWatchers.Append(watcher) 42 | 43 | c.wg.Add(1) 44 | go watcher.mainLoop() 45 | 46 | return watcher 47 | } 48 | 49 | type _DataWatcher struct { 50 | context.Context 51 | terminate context.CancelFunc 52 | terminated chan async.Ret 53 | client *Client 54 | handler RecvDataHandler 55 | } 56 | 57 | func (w *_DataWatcher) Terminate() async.AsyncRet { 58 | w.terminate() 59 | return w.terminated 60 | } 61 | 62 | func (w *_DataWatcher) Terminated() async.AsyncRet { 63 | return w.terminated 64 | } 65 | 66 | func (w *_DataWatcher) mainLoop() { 67 | defer func() { 68 | w.terminate() 69 | w.client.wg.Done() 70 | async.Return(w.terminated, async.VoidRet) 71 | }() 72 | 73 | select { 74 | case <-w.Done(): 75 | case <-w.client.Done(): 76 | } 77 | 78 | w.client.dataWatchers.DeleteOnce(func(exists *_DataWatcher) bool { 79 | return exists == w 80 | }) 81 | } 82 | -------------------------------------------------------------------------------- /addins/gate/cli/client_eventwatcher.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cli 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | func (c *Client) newEventWatcher(ctx context.Context, handler RecvEventHandler) *_EventWatcher { 28 | if ctx == nil { 29 | ctx = context.Background() 30 | } 31 | 32 | ctx, cancel := context.WithCancel(ctx) 33 | 34 | watcher := &_EventWatcher{ 35 | Context: ctx, 36 | terminate: cancel, 37 | terminated: async.MakeAsyncRet(), 38 | client: c, 39 | handler: handler, 40 | } 41 | c.eventWatchers.Append(watcher) 42 | 43 | c.wg.Add(1) 44 | go watcher.mainLoop() 45 | 46 | return watcher 47 | } 48 | 49 | type _EventWatcher struct { 50 | context.Context 51 | terminate context.CancelFunc 52 | terminated chan async.Ret 53 | client *Client 54 | handler RecvEventHandler 55 | } 56 | 57 | func (w *_EventWatcher) Terminate() async.AsyncRet { 58 | w.terminate() 59 | return w.terminated 60 | } 61 | 62 | func (w *_EventWatcher) Terminated() async.AsyncRet { 63 | return w.terminated 64 | } 65 | 66 | func (w *_EventWatcher) mainLoop() { 67 | defer func() { 68 | w.terminate() 69 | w.client.wg.Done() 70 | async.Return(w.terminated, async.VoidRet) 71 | }() 72 | 73 | select { 74 | case <-w.Done(): 75 | case <-w.client.Done(): 76 | } 77 | 78 | w.client.eventWatchers.DeleteOnce(func(exists *_EventWatcher) bool { 79 | return exists == w 80 | }) 81 | } 82 | -------------------------------------------------------------------------------- /addins/gate/cli/client_synctime.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cli 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | "git.golaxy.org/framework/utils/concurrent" 26 | "time" 27 | ) 28 | 29 | // ResponseTime 响应同步时间 30 | type ResponseTime struct { 31 | RequestTime time.Time // 请求时的本地时间 32 | LocalTime time.Time // 响应时的本地时间 33 | RemoteTime time.Time // 响应时的对端时间 34 | } 35 | 36 | // RTT 往返时间 37 | func (rt ResponseTime) RTT() time.Duration { 38 | return rt.LocalTime.Sub(rt.RequestTime) 39 | } 40 | 41 | // SyncTime 同步的时间 42 | func (rt ResponseTime) SyncTime() time.Time { 43 | return rt.RemoteTime.Add(rt.RTT() / 2) 44 | } 45 | 46 | // NowTime 当前时间 47 | func (rt ResponseTime) NowTime() time.Time { 48 | return rt.SyncTime().Add(time.Now().Sub(rt.LocalTime)) 49 | } 50 | 51 | // RequestTime 请求对端同步时间 52 | func (c *Client) RequestTime(ctx context.Context) async.AsyncRetT[*ResponseTime] { 53 | resp := concurrent.MakeRespAsyncRetT[*ResponseTime]() 54 | future := c.GetFutures().Make(ctx, resp) 55 | if err := c.ctrl.RequestTime(future.Id); err != nil { 56 | future.Cancel(err) 57 | } 58 | return resp.ToAsyncRetT() 59 | } 60 | -------------------------------------------------------------------------------- /addins/gate/cli/connect.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package cli 21 | 22 | import ( 23 | "context" 24 | "fmt" 25 | "git.golaxy.org/core" 26 | "git.golaxy.org/core/utils/option" 27 | ) 28 | 29 | // Connect 连接服务端 30 | func Connect(ctx context.Context, endpoint string, settings ...option.Setting[ClientOptions]) (*Client, error) { 31 | connector := _Connector{ 32 | options: option.Make(With.Default(), settings...), 33 | } 34 | return connector.connect(ctx, endpoint) 35 | } 36 | 37 | // Reconnect 重连服务端 38 | func Reconnect(client *Client) error { 39 | if client == nil { 40 | return fmt.Errorf("cli: %w: client is nil", core.ErrArgs) 41 | } 42 | 43 | connector := _Connector{ 44 | options: client.options, 45 | } 46 | 47 | return connector.reconnect(client) 48 | } 49 | -------------------------------------------------------------------------------- /addins/gate/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gate 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newGate) 28 | Name = self.Name 29 | Using = self.Using 30 | Install = self.Install 31 | Uninstall = self.Uninstall 32 | ) 33 | -------------------------------------------------------------------------------- /addins/gate/details.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gate 21 | 22 | import "git.golaxy.org/framework/net/netpath" 23 | 24 | // CliDetails 客户端地址信息 25 | var CliDetails = &netpath.NodeDetails{ 26 | DomainRoot: netpath.Domain{Path: "cli", Sep: "."}, 27 | DomainBroadcast: netpath.Domain{Path: "cli.bc", Sep: "."}, 28 | DomainMulticast: netpath.Domain{Path: "cli.mc", Sep: "."}, 29 | DomainUnicast: netpath.Domain{Path: "cli.ep", Sep: "."}, 30 | } 31 | -------------------------------------------------------------------------------- /addins/gate/session_datawatcher.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gate 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | func (s *_Session) newDataWatcher(ctx context.Context, handler SessionRecvDataHandler) *_DataWatcher { 28 | if ctx == nil { 29 | ctx = context.Background() 30 | } 31 | 32 | ctx, cancel := context.WithCancel(ctx) 33 | 34 | watcher := &_DataWatcher{ 35 | Context: ctx, 36 | terminate: cancel, 37 | terminated: async.MakeAsyncRet(), 38 | session: s, 39 | handler: handler, 40 | } 41 | s.dataWatchers.Append(watcher) 42 | 43 | s.gate.wg.Add(1) 44 | go watcher.mainLoop() 45 | 46 | return watcher 47 | } 48 | 49 | type _DataWatcher struct { 50 | context.Context 51 | terminate context.CancelFunc 52 | terminated chan async.Ret 53 | session *_Session 54 | handler SessionRecvDataHandler 55 | } 56 | 57 | func (w *_DataWatcher) Terminate() async.AsyncRet { 58 | w.terminate() 59 | return w.terminated 60 | } 61 | 62 | func (w *_DataWatcher) Terminated() async.AsyncRet { 63 | return w.terminated 64 | } 65 | 66 | func (w *_DataWatcher) mainLoop() { 67 | defer func() { 68 | w.terminate() 69 | w.session.gate.wg.Done() 70 | async.Return(w.terminated, async.VoidRet) 71 | }() 72 | 73 | select { 74 | case <-w.Done(): 75 | case <-w.session.Done(): 76 | } 77 | 78 | w.session.dataWatchers.DeleteOnce(func(exists *_DataWatcher) bool { 79 | return exists == w 80 | }) 81 | } 82 | -------------------------------------------------------------------------------- /addins/gate/sessionstate_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -type SessionState"; DO NOT EDIT. 2 | 3 | package gate 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[SessionState_Birth-0] 12 | _ = x[SessionState_Handshake-1] 13 | _ = x[SessionState_Confirmed-2] 14 | _ = x[SessionState_Active-3] 15 | _ = x[SessionState_Inactive-4] 16 | _ = x[SessionState_Death-5] 17 | } 18 | 19 | const _SessionState_name = "SessionState_BirthSessionState_HandshakeSessionState_ConfirmedSessionState_ActiveSessionState_InactiveSessionState_Death" 20 | 21 | var _SessionState_index = [...]uint8{0, 18, 40, 62, 81, 102, 120} 22 | 23 | func (i SessionState) String() string { 24 | if i < 0 || i >= SessionState(len(_SessionState_index)-1) { 25 | return "SessionState(" + strconv.FormatInt(int64(i), 10) + ")" 26 | } 27 | return _SessionState_name[_SessionState_index[i]:_SessionState_index[i+1]] 28 | } 29 | -------------------------------------------------------------------------------- /addins/gate/websocket.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gate 21 | 22 | import ( 23 | "golang.org/x/net/websocket" 24 | "net" 25 | "strings" 26 | ) 27 | 28 | func DefaultWebSocketLocalAddrResolver(conn *websocket.Conn) net.Addr { 29 | return conn.LocalAddr() 30 | } 31 | 32 | func DefaultWebSocketRemoteAddrResolver(conn *websocket.Conn) net.Addr { 33 | if xrip := conn.Request().Header.Get("X-Real-Ip"); xrip != "" { 34 | return _WebSocketAddr(strings.TrimSpace(xrip)) 35 | } 36 | 37 | if xff := conn.Request().Header.Get("X-Forwarded-For"); xff != "" { 38 | ips := strings.Split(xff, ",") 39 | if len(ips) > 0 { 40 | return _WebSocketAddr(strings.TrimSpace(ips[0])) 41 | } 42 | } 43 | 44 | ip, _, err := net.SplitHostPort(conn.Request().RemoteAddr) 45 | if err == nil { 46 | return _WebSocketAddr(ip) 47 | } 48 | 49 | return _WebSocketAddr("unknown") 50 | } 51 | 52 | type _WebSocketAddr string 53 | 54 | func (_WebSocketAddr) Network() string { 55 | return "websocket" 56 | } 57 | 58 | func (addr _WebSocketAddr) String() string { 59 | return string(addr) 60 | } 61 | 62 | type _WebSocketConn struct { 63 | *websocket.Conn 64 | gate *_Gate 65 | } 66 | 67 | func (c *_WebSocketConn) LocalAddr() net.Addr { 68 | return c.gate.options.WebSocketLocalAddrResolver(c.Conn) 69 | } 70 | 71 | func (c *_WebSocketConn) RemoteAddr() net.Addr { 72 | return c.gate.options.WebSocketRemoteAddrResolver(c.Conn) 73 | } 74 | -------------------------------------------------------------------------------- /addins/log/console_log/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package console_log 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.AddIn(newLogger) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/log/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package log 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.AddInInterface[ILogger]() 26 | Name = self.Name 27 | Using = self.Using 28 | ) 29 | -------------------------------------------------------------------------------- /addins/log/zap_log/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package zap_log 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.AddIn(newLogger) 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/router/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package router 21 | 22 | import "git.golaxy.org/core/define" 23 | 24 | var ( 25 | self = define.ServiceAddIn(newRouter) 26 | Name = self.Name 27 | Using = self.Using 28 | Install = self.Install 29 | Uninstall = self.Uninstall 30 | ) 31 | -------------------------------------------------------------------------------- /addins/router/mapping.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package router 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/ec" 25 | "git.golaxy.org/framework/addins/gate" 26 | ) 27 | 28 | // IMapping 映射 29 | type IMapping interface { 30 | context.Context 31 | // GetEntity 获取实体 32 | GetEntity() ec.ConcurrentEntity 33 | // GetSession 获取会话 34 | GetSession() gate.ISession 35 | // GetCliAddr 获取客户端地址 36 | GetCliAddr() string 37 | } 38 | 39 | type _Mapping struct { 40 | context.Context 41 | terminate context.CancelFunc 42 | router *_Router 43 | entity ec.ConcurrentEntity 44 | session gate.ISession 45 | cliAddr string 46 | } 47 | 48 | // GetEntity 获取实体 49 | func (m *_Mapping) GetEntity() ec.ConcurrentEntity { 50 | return m.entity 51 | } 52 | 53 | // GetSession 获取会话 54 | func (m *_Mapping) GetSession() gate.ISession { 55 | return m.session 56 | } 57 | 58 | // GetCliAddr 获取客户端地址 59 | func (m *_Mapping) GetCliAddr() string { 60 | return m.cliAddr 61 | } 62 | 63 | func (m *_Mapping) mainLoop() { 64 | select { 65 | case <-m.Done(): 66 | return 67 | case <-m.entity.Done(): 68 | m.router.ClearEntity(m.entity.GetId()) 69 | case <-m.session.Done(): 70 | m.router.ClearSession(m.session.GetId()) 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /addins/rpc/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpc 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.ServiceAddIn(newRPC) 28 | Name = self.Name 29 | Using = self.Using 30 | Install = self.Install 31 | Uninstall = self.Uninstall 32 | ) 33 | -------------------------------------------------------------------------------- /addins/rpc/proxy_const.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpc 21 | 22 | const ( 23 | Main = "" // 调用客户端主过程 24 | EntitySelf = "" // 调用实体自身方法 25 | RuntimeSelf = "" // 调用运行时自身方法 26 | ServiceSelf = "" // 调用服务自身方法 27 | ) 28 | -------------------------------------------------------------------------------- /addins/rpc/rpc_options.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpc 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/option" 24 | "git.golaxy.org/framework/addins/rpc/rpcpcsr" 25 | ) 26 | 27 | type RPCOptions struct { 28 | Processors []any 29 | } 30 | 31 | var With _Option 32 | 33 | type _Option struct{} 34 | 35 | func (_Option) Default() option.Setting[RPCOptions] { 36 | return func(options *RPCOptions) { 37 | With.Processors(rpcpcsr.NewServiceProcessor(nil, true))(options) 38 | } 39 | } 40 | 41 | func (_Option) Processors(processors ...any) option.Setting[RPCOptions] { 42 | return func(options *RPCOptions) { 43 | options.Processors = processors 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /addins/rpc/rpcli/cachecp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcli 21 | 22 | import ( 23 | "git.golaxy.org/framework/addins/rpc/callpath" 24 | "reflect" 25 | ) 26 | 27 | func cacheCallPath(script string, rt reflect.Type) { 28 | if rt == nil { 29 | return 30 | } 31 | for i := range rt.NumMethod() { 32 | callpath.Cache(script, rt.Method(i).Name) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /addins/rpc/rpcli/procedure.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcli 21 | 22 | import ( 23 | "reflect" 24 | ) 25 | 26 | const ( 27 | Main = "" // 主过程名称 28 | ) 29 | 30 | // IProcedure 过程接口 31 | type IProcedure interface { 32 | iProcedure 33 | // GetCli 获取RPC客户端 34 | GetCli() *RPCli 35 | // GetName 获取名称 36 | GetName() string 37 | // GetReflected 获取反射值 38 | GetReflected() reflect.Value 39 | } 40 | 41 | type iProcedure interface { 42 | init(cli *RPCli, name string, instance any) 43 | } 44 | 45 | // Procedure 过程 46 | type Procedure struct { 47 | cli *RPCli 48 | name string 49 | reflected reflect.Value 50 | } 51 | 52 | func (p *Procedure) init(cli *RPCli, name string, instance any) { 53 | p.cli = cli 54 | p.name = name 55 | p.reflected = reflect.ValueOf(instance) 56 | } 57 | 58 | // GetCli 获取RPC客户端 59 | func (p *Procedure) GetCli() *RPCli { 60 | return p.cli 61 | } 62 | 63 | // GetName 获取名称 64 | func (p *Procedure) GetName() string { 65 | return p.name 66 | } 67 | 68 | // GetReflected 获取反射值 69 | func (p *Procedure) GetReflected() reflect.Value { 70 | return p.reflected 71 | } 72 | -------------------------------------------------------------------------------- /addins/rpc/rpcli/rpcli_procedure.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcli 21 | 22 | import ( 23 | "errors" 24 | "fmt" 25 | "git.golaxy.org/core" 26 | ) 27 | 28 | // AddProcedure 添加过程 29 | func (c *RPCli) AddProcedure(name string, proc any) error { 30 | _proc, ok := proc.(IProcedure) 31 | if !ok { 32 | return fmt.Errorf("rpcli: %w: incorrect proc type", core.ErrArgs) 33 | } 34 | 35 | _proc.init(c, name, proc) 36 | cacheCallPath(name, _proc.GetReflected().Type()) 37 | 38 | if !c.procs.TryAdd(name, _proc) { 39 | return ErrProcedureExists 40 | } 41 | 42 | return nil 43 | } 44 | 45 | // RemoveProcedure 删除过程 46 | func (c *RPCli) RemoveProcedure(name string) error { 47 | if name == "" { 48 | return errors.New("rpcli: the main procedure can't be removed") 49 | } 50 | 51 | if !c.procs.Delete(name) { 52 | return ErrProcedureNotFound 53 | } 54 | 55 | return nil 56 | } 57 | 58 | // GetProcedure 查询过程 59 | func (c *RPCli) GetProcedure(name string) (IProcedure, bool) { 60 | return c.procs.Get(name) 61 | } 62 | -------------------------------------------------------------------------------- /addins/rpc/rpcpcsr/permission_validator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcpcsr 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/generic" 24 | "git.golaxy.org/framework/addins/gate" 25 | "git.golaxy.org/framework/addins/rpc/callpath" 26 | "git.golaxy.org/framework/addins/rpcstack" 27 | "strings" 28 | ) 29 | 30 | // PermissionValidator 权限验证器 31 | type PermissionValidator = generic.Delegate2[rpcstack.CallChain, callpath.CallPath, bool] 32 | 33 | // DefaultValidateCliPermission 默认的客户端RPC请求权限验证函数,限制客户端RPC只能调用前缀为C_的函数 34 | func DefaultValidateCliPermission(cc rpcstack.CallChain, cp callpath.CallPath) bool { 35 | if !gate.CliDetails.DomainRoot.Contains(cc.First().Addr) { 36 | return true 37 | } 38 | return strings.HasPrefix(cp.Method, "C_") 39 | } 40 | -------------------------------------------------------------------------------- /addins/rpc/rpcpcsr/processor_lifecycle.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcpcsr 21 | 22 | import "git.golaxy.org/core/service" 23 | 24 | // LifecycleInit RPC处理器生命周期开始 25 | type LifecycleInit interface { 26 | Init(svcCtx service.Context) 27 | } 28 | 29 | // LifecycleShut RPC处理器生命周期结束 30 | type LifecycleShut interface { 31 | Shut(svcCtx service.Context) 32 | } 33 | -------------------------------------------------------------------------------- /addins/rpc/rpcpcsr/service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcpcsr 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/service" 25 | "git.golaxy.org/core/utils/generic" 26 | "git.golaxy.org/core/utils/types" 27 | "git.golaxy.org/framework/addins/dsvc" 28 | "git.golaxy.org/framework/addins/log" 29 | "git.golaxy.org/framework/utils/concurrent" 30 | ) 31 | 32 | // NewServiceProcessor 创建分布式服务间的RPC处理器 33 | func NewServiceProcessor(permValidator PermissionValidator, reduceCallPath bool) any { 34 | return &_ServiceProcessor{ 35 | permValidator: permValidator, 36 | reduceCallPath: reduceCallPath, 37 | } 38 | } 39 | 40 | // _ServiceProcessor 分布式服务间的RPC处理器 41 | type _ServiceProcessor struct { 42 | svcCtx service.Context 43 | dist dsvc.IDistService 44 | msgWatcher concurrent.IWatcher 45 | permValidator PermissionValidator 46 | reduceCallPath bool 47 | } 48 | 49 | // Init 初始化 50 | func (p *_ServiceProcessor) Init(svcCtx service.Context) { 51 | p.svcCtx = svcCtx 52 | p.dist = dsvc.Using(svcCtx) 53 | p.msgWatcher = p.dist.WatchMsg(context.Background(), generic.CastDelegate2(p.handleRecvMsg)) 54 | 55 | log.Debugf(p.svcCtx, "rpc processor %q started", types.FullName(*p)) 56 | } 57 | 58 | // Shut 结束 59 | func (p *_ServiceProcessor) Shut(svcCtx service.Context) { 60 | <-p.msgWatcher.Terminate() 61 | 62 | log.Debugf(p.svcCtx, "rpc processor %q stopped", types.FullName(*p)) 63 | } 64 | -------------------------------------------------------------------------------- /addins/rpcstack/define.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcstack 21 | 22 | import ( 23 | "git.golaxy.org/core/define" 24 | ) 25 | 26 | var ( 27 | self = define.RuntimeAddIn(newRPCStack) 28 | Name = self.Name 29 | Using = self.Using 30 | Install = self.Install 31 | Uninstall = self.Uninstall 32 | ) 33 | -------------------------------------------------------------------------------- /addins/rpcstack/rpcstack_types.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcstack 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/generic" 24 | "git.golaxy.org/framework/net/gap/variant" 25 | ) 26 | 27 | type ( 28 | Call = variant.Call 29 | CallChain = variant.CallChain 30 | Variables = generic.UnorderedSliceMap[string, any] 31 | ) 32 | 33 | var EmptyCallChain = CallChain{} 34 | -------------------------------------------------------------------------------- /addins/rpcstack/unsafe_rpcstack.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package rpcstack 21 | 22 | // Deprecated: UnsafeFrame 访问RPC调用堆栈支持内部方法 23 | func UnsafeRPCStack(r IRPCStack) _UnsafeRPCStack { 24 | return _UnsafeRPCStack{ 25 | IRPCStack: r, 26 | } 27 | } 28 | 29 | type _UnsafeRPCStack struct { 30 | IRPCStack 31 | } 32 | 33 | func (ur _UnsafeRPCStack) PushCallChain(cc CallChain) { 34 | ur.pushCallChain(cc) 35 | } 36 | 37 | func (ur _UnsafeRPCStack) PopCallChain() { 38 | ur.popCallChain() 39 | } 40 | -------------------------------------------------------------------------------- /cachecp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/framework/addins/rpc/callpath" 24 | "reflect" 25 | ) 26 | 27 | func init() { 28 | cacheCallPath("", reflect.TypeFor[*EntityBehavior]()) 29 | } 30 | 31 | func cacheCallPath(script string, rt reflect.Type) { 32 | if rt == nil { 33 | return 34 | } 35 | for i := range rt.NumMethod() { 36 | callpath.Cache(script, rt.Method(i).Name) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /comp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core/ec" 24 | "git.golaxy.org/core/extension" 25 | "git.golaxy.org/core/runtime" 26 | "git.golaxy.org/core/service" 27 | "git.golaxy.org/core/utils/reinterpret" 28 | ) 29 | 30 | // ComponentBehavior 组件行为,在开发新组件时,匿名嵌入至组件结构体中 31 | type ComponentBehavior struct { 32 | ec.ComponentBehavior 33 | } 34 | 35 | // GetRuntime 获取运行时 36 | func (c *ComponentBehavior) GetRuntime() IRuntime { 37 | return reinterpret.Cast[IRuntime](runtime.Current(c)) 38 | } 39 | 40 | // GetService 获取服务 41 | func (c *ComponentBehavior) GetService() IService { 42 | return reinterpret.Cast[IService](service.Current(c)) 43 | } 44 | 45 | // GetAddInManager 获取插件管理器 46 | func (c *ComponentBehavior) GetAddInManager() extension.AddInManager { 47 | return runtime.Current(c).GetAddInManager() 48 | } 49 | 50 | // GetLiving 是否活跃可用 51 | func (c *ComponentBehavior) GetLiving() bool { 52 | return c.GetState() <= ec.ComponentState_Alive 53 | } 54 | -------------------------------------------------------------------------------- /comp_await.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | // Await 异步等待结果返回 28 | func (c *ComponentBehavior) Await(asyncRets ...async.AsyncRet) AwaitDirector { 29 | return AwaitDirector{ 30 | iec: c, 31 | director: core.Await(c, asyncRets...), 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | // Package framework Golaxy分布式服务开发框架。 21 | package framework 22 | -------------------------------------------------------------------------------- /ec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | type iEC interface { 23 | GetRuntime() IRuntime 24 | GetService() IService 25 | GetLiving() bool 26 | } 27 | -------------------------------------------------------------------------------- /entity.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core/ec" 24 | "git.golaxy.org/core/extension" 25 | "git.golaxy.org/core/runtime" 26 | "git.golaxy.org/core/service" 27 | "git.golaxy.org/core/utils/reinterpret" 28 | ) 29 | 30 | // EntityBehavior 实体行为,在需要扩展实体能力时,匿名嵌入至实体结构体中 31 | type EntityBehavior struct { 32 | ec.EntityBehavior 33 | } 34 | 35 | // GetRuntime 获取运行时 36 | func (e *EntityBehavior) GetRuntime() IRuntime { 37 | return reinterpret.Cast[IRuntime](runtime.Current(e)) 38 | } 39 | 40 | // GetService 获取服务 41 | func (e *EntityBehavior) GetService() IService { 42 | return reinterpret.Cast[IService](service.Current(e)) 43 | } 44 | 45 | // GetAddInManager 获取插件管理器 46 | func (e *EntityBehavior) GetAddInManager() extension.AddInManager { 47 | return runtime.Current(e).GetAddInManager() 48 | } 49 | 50 | // GetLiving 是否活跃可用 51 | func (e *EntityBehavior) GetLiving() bool { 52 | return e.GetState() <= ec.EntityState_Alive 53 | } 54 | -------------------------------------------------------------------------------- /entity_await.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | // Await 异步等待结果返回 28 | func (e *EntityBehavior) Await(asyncRets ...async.AsyncRet) AwaitDirector { 29 | return AwaitDirector{ 30 | iec: e, 31 | director: core.Await(e, asyncRets...), 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /entityptcreator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core" 24 | ) 25 | 26 | // BuildEntityPT 创建实体原型 27 | var BuildEntityPT = core.BuildEntityPT 28 | 29 | // EntityPTCreator 实体原型构建器 30 | type EntityPTCreator = core.EntityPTCreator 31 | -------------------------------------------------------------------------------- /errors.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "errors" 24 | ) 25 | 26 | var ( 27 | ErrFramework = errors.New("framework") // 框架错误 28 | ) 29 | -------------------------------------------------------------------------------- /net/gap/codec/codec_encoder.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package codec 21 | 22 | import ( 23 | "errors" 24 | "fmt" 25 | "git.golaxy.org/core" 26 | "git.golaxy.org/framework/net/gap" 27 | "git.golaxy.org/framework/utils/binaryutil" 28 | ) 29 | 30 | var ( 31 | ErrEncode = errors.New("gap-encode") // 编码错误 32 | ) 33 | 34 | var encoder = &Encoder{} 35 | 36 | // NewEncoder 创建消息包编码器 37 | func NewEncoder() *Encoder { 38 | return encoder 39 | } 40 | 41 | // Encoder 消息包编码器 42 | type Encoder struct{} 43 | 44 | // Encode 编码消息包 45 | func (*Encoder) Encode(src gap.Origin, seq int64, msg gap.MsgReader) (ret binaryutil.RecycleBytes, err error) { 46 | if msg == nil { 47 | return binaryutil.NilRecycleBytes, fmt.Errorf("%w: %w: msg is nil", ErrEncode, core.ErrArgs) 48 | } 49 | 50 | mp := gap.MsgPacket{ 51 | Head: gap.MsgHead{ 52 | MsgId: msg.MsgId(), 53 | Src: src, 54 | Seq: seq, 55 | }, 56 | Msg: msg, 57 | } 58 | mp.Head.Len = uint32(mp.Size()) 59 | 60 | mpBuf := binaryutil.MakeRecycleBytes(int(mp.Head.Len)) 61 | defer func() { 62 | if !mpBuf.Equal(ret) { 63 | mpBuf.Release() 64 | } 65 | }() 66 | 67 | if _, err := binaryutil.CopyToBuff(mpBuf.Data(), mp); err != nil { 68 | return binaryutil.NilRecycleBytes, fmt.Errorf("%w: write msg failed, %w", ErrEncode, err) 69 | } 70 | 71 | return mpBuf, nil 72 | } 73 | -------------------------------------------------------------------------------- /net/gap/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | // Package gap Golaxy应用层协议(Golaxy Application Protocol),适用于开发应用层通信消息,需要工作在GTP协议或MQ之上,支持消息判重、自定义消息、自定义可变类型等特性。 21 | package gap 22 | -------------------------------------------------------------------------------- /net/gap/marshaler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/framework/utils/binaryutil" 25 | ) 26 | 27 | // Marshal 序列化 28 | func Marshal[T MsgReader](msg T) (ret binaryutil.RecycleBytes, err error) { 29 | bs := binaryutil.MakeRecycleBytes(msg.Size()) 30 | defer func() { 31 | if !bs.Equal(ret) { 32 | bs.Release() 33 | } 34 | }() 35 | 36 | if _, err := binaryutil.CopyToBuff(bs.Data(), msg); err != nil { 37 | return binaryutil.NilRecycleBytes, fmt.Errorf("%w: marshal msg(%d) failed, %w", ErrGAP, msg.MsgId(), err) 38 | } 39 | 40 | return bs, nil 41 | } 42 | 43 | // Unmarshal 反序列化 44 | func Unmarshal[T MsgWriter](msg T, data []byte) error { 45 | if _, err := msg.Write(data); err != nil { 46 | return fmt.Errorf("%w: unmarshal msg(%d) failed, %w", ErrGAP, msg.MsgId(), err) 47 | } 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /net/gap/msg.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | import ( 23 | "errors" 24 | "io" 25 | ) 26 | 27 | var ( 28 | ErrGAP = errors.New("gap") // 消息协议错误 29 | ) 30 | 31 | // Msg 消息接口 32 | type Msg interface { 33 | MsgReader 34 | MsgWriter 35 | } 36 | 37 | // MsgReader 读取消息 38 | type MsgReader interface { 39 | io.Reader 40 | // Size 大小 41 | Size() int 42 | // MsgId 消息Id 43 | MsgId() MsgId 44 | } 45 | 46 | // MsgWriter 写入消息 47 | type MsgWriter interface { 48 | io.Writer 49 | // Size 大小 50 | Size() int 51 | // MsgId 消息Id 52 | MsgId() MsgId 53 | } 54 | -------------------------------------------------------------------------------- /net/gap/msgid.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/exception" 24 | "git.golaxy.org/core/utils/types" 25 | "hash/fnv" 26 | "reflect" 27 | ) 28 | 29 | // MsgId 消息Id 30 | type MsgId = uint32 31 | 32 | // MakeMsgId 创建类型Id 33 | func MakeMsgId(msg Msg) MsgId { 34 | hash := fnv.New32a() 35 | rt := reflect.ValueOf(msg).Elem().Type() 36 | if rt.PkgPath() == "" || rt.Name() == "" { 37 | exception.Panicf("%w: unsupported type", ErrGAP) 38 | } 39 | hash.Write([]byte(types.FullNameRT(rt))) 40 | return MsgId(MsgId_Customize + hash.Sum32()) 41 | } 42 | 43 | // MakeMsgIdT 创建类型Id 44 | func MakeMsgIdT[T any]() MsgId { 45 | hash := fnv.New32a() 46 | rt := reflect.TypeFor[T]() 47 | if rt.PkgPath() == "" || rt.Name() == "" || !reflect.PointerTo(rt).Implements(reflect.TypeFor[Msg]()) { 48 | exception.Panicf("%w: unsupported type", ErrGAP) 49 | } 50 | hash.Write([]byte(types.FullNameRT(rt))) 51 | return MsgId(MsgId_Customize + hash.Sum32()) 52 | } 53 | -------------------------------------------------------------------------------- /net/gap/msgid_builtin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | const ( 23 | MsgId_None MsgId = iota // 未设置 24 | MsgId_RPC_Request // RPC请求 25 | MsgId_RPC_Reply // RPC答复 26 | MsgId_OnewayRPC // 单程RPC请求 27 | MsgId_Forward // 转发 28 | MsgId_Customize = 32 // 自定义消息起点 29 | ) 30 | -------------------------------------------------------------------------------- /net/gap/msgpacket.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // MsgPacket 消息包 28 | type MsgPacket struct { 29 | Head MsgHead // 消息头 30 | Msg MsgReader // 消息 31 | } 32 | 33 | // Read implements io.Reader 34 | func (mp MsgPacket) Read(p []byte) (int, error) { 35 | bs := binaryutil.NewBigEndianStream(p) 36 | 37 | if _, err := binaryutil.CopyToByteStream(&bs, mp.Head); err != nil { 38 | return bs.BytesWritten(), err 39 | } 40 | 41 | if mp.Msg == nil { 42 | return bs.BytesWritten(), io.EOF 43 | } 44 | 45 | if _, err := binaryutil.CopyToByteStream(&bs, mp.Msg); err != nil { 46 | return bs.BytesWritten(), err 47 | } 48 | 49 | return bs.BytesWritten(), io.EOF 50 | } 51 | 52 | // Size 大小 53 | func (mp MsgPacket) Size() int { 54 | n := mp.Head.Size() 55 | 56 | if mp.Msg != nil { 57 | n += mp.Msg.Size() 58 | } 59 | 60 | return n 61 | } 62 | -------------------------------------------------------------------------------- /net/gap/serializedmsg.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gap 21 | 22 | import ( 23 | "io" 24 | ) 25 | 26 | // SerializedMsg 已序列化消息 27 | type SerializedMsg struct { 28 | Id MsgId // 消息Id 29 | Data []byte // 消息内容(引用) 30 | } 31 | 32 | // Read implements io.Reader 33 | func (m *SerializedMsg) Read(p []byte) (int, error) { 34 | if len(p) < len(m.Data) { 35 | return 0, io.ErrShortWrite 36 | } 37 | return copy(p, m.Data), io.EOF 38 | } 39 | 40 | // Size 大小 41 | func (m *SerializedMsg) Size() int { 42 | return len(m.Data) 43 | } 44 | 45 | // MsgId 消息Id 46 | func (m *SerializedMsg) MsgId() MsgId { 47 | return m.Id 48 | } 49 | -------------------------------------------------------------------------------- /net/gap/variant/typeid_builtin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | const ( 23 | TypeId_None TypeId = iota 24 | TypeId_Int 25 | TypeId_Int8 26 | TypeId_Int16 27 | TypeId_Int32 28 | TypeId_Int64 29 | TypeId_Uint 30 | TypeId_Uint8 31 | TypeId_Uint16 32 | TypeId_Uint32 33 | TypeId_Uint64 34 | TypeId_Float 35 | TypeId_Double 36 | TypeId_Byte 37 | TypeId_Bool 38 | TypeId_Bytes 39 | TypeId_String 40 | TypeId_Null 41 | TypeId_Array 42 | TypeId_Map 43 | TypeId_Error 44 | TypeId_CallChain 45 | TypeId_Customize = 32 // 自定义类型起点 46 | ) 47 | -------------------------------------------------------------------------------- /net/gap/variant/value.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "io" 24 | ) 25 | 26 | // Value 值 27 | type Value interface { 28 | ValueReader 29 | ValueWriter 30 | } 31 | 32 | // ValueReader 读取值 33 | type ValueReader interface { 34 | io.Reader 35 | // Size 大小 36 | Size() int 37 | // TypeId 类型 38 | TypeId() TypeId 39 | // Indirect 原始值 40 | Indirect() any 41 | } 42 | 43 | // ValueWriter 写入值 44 | type ValueWriter interface { 45 | io.Writer 46 | // Size 大小 47 | Size() int 48 | // TypeId 类型 49 | TypeId() TypeId 50 | } 51 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_array_readonly.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | // MakeReadonlyArray 创建只读array 23 | func MakeReadonlyArray[T any](arr []T) (Array, error) { 24 | varArr := make(Array, 0, len(arr)) 25 | 26 | for i := range arr { 27 | v, err := CastReadonlyVariant(arr[i]) 28 | if err != nil { 29 | return nil, err 30 | } 31 | varArr = append(varArr, v) 32 | } 33 | 34 | return varArr, nil 35 | } 36 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_array_serialized.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | // MakeSerializedArray 创建已序列化array 23 | func MakeSerializedArray[T any](arr []T) (ret Array, err error) { 24 | varArr := make(Array, 0, len(arr)) 25 | defer func() { 26 | if ret == nil { 27 | varArr.Release() 28 | } 29 | }() 30 | 31 | for i := range arr { 32 | v, err := CastSerializedVariant(arr[i]) 33 | if err != nil { 34 | return nil, err 35 | } 36 | varArr = append(varArr, v) 37 | } 38 | 39 | return varArr, nil 40 | } 41 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_bool.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Bool builtin bool 28 | type Bool bool 29 | 30 | // Read implements io.Reader 31 | func (v Bool) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteBool(bool(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Bool) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadBool() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Bool(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Bool) Size() int { 52 | return binaryutil.SizeofBool() 53 | } 54 | 55 | // TypeId 类型 56 | func (Bool) TypeId() TypeId { 57 | return TypeId_Bool 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Bool) Indirect() any { 62 | return bool(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_byte.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Byte builtin byte 28 | type Byte byte 29 | 30 | // Read implements io.Reader 31 | func (v Byte) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteByte(byte(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Byte) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadByte() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Byte(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Byte) Size() int { 52 | return binaryutil.SizeofByte() 53 | } 54 | 55 | // TypeId 类型 56 | func (Byte) TypeId() TypeId { 57 | return TypeId_Byte 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Byte) Indirect() any { 62 | return byte(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_bytes.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Bytes bytes 28 | type Bytes []byte 29 | 30 | // Read implements io.Reader 31 | func (v Bytes) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteBytes(v); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Bytes) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadBytes() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = val 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v Bytes) Size() int { 52 | return binaryutil.SizeofBytes(v) 53 | } 54 | 55 | // TypeId 类型 56 | func (Bytes) TypeId() TypeId { 57 | return TypeId_Bytes 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Bytes) Indirect() any { 62 | return []byte(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_double.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Double builtin double 28 | type Double float64 29 | 30 | // Read implements io.Reader 31 | func (v Double) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteDouble(float64(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Double) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadDouble() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Double(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Double) Size() int { 52 | return binaryutil.SizeofDouble() 53 | } 54 | 55 | // TypeId 类型 56 | func (Double) TypeId() TypeId { 57 | return TypeId_Double 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Double) Indirect() any { 62 | return float64(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_float.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Float builtin float 28 | type Float float32 29 | 30 | // Read implements io.Reader 31 | func (v Float) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteFloat(float32(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Float) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadFloat() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Float(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Float) Size() int { 52 | return binaryutil.SizeofFloat() 53 | } 54 | 55 | // TypeId 类型 56 | func (Float) TypeId() TypeId { 57 | return TypeId_Float 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Float) Indirect() any { 62 | return float32(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_int.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Int builtin int 28 | type Int int 29 | 30 | // Read implements io.Reader 31 | func (v Int) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteVarint(int64(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Int) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadVarint() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Int(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v Int) Size() int { 52 | return binaryutil.SizeofVarint(int64(v)) 53 | } 54 | 55 | // TypeId 类型 56 | func (Int) TypeId() TypeId { 57 | return TypeId_Int 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Int) Indirect() any { 62 | return int(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_int16.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Int16 builtin int16 28 | type Int16 int16 29 | 30 | // Read implements io.Reader 31 | func (v Int16) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteInt16(int16(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Int16) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadInt16() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Int16(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Int16) Size() int { 52 | return binaryutil.SizeofInt16() 53 | } 54 | 55 | // TypeId 类型 56 | func (Int16) TypeId() TypeId { 57 | return TypeId_Int16 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Int16) Indirect() any { 62 | return int16(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_int32.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Int32 builtin int32 28 | type Int32 int32 29 | 30 | // Read implements io.Reader 31 | func (v Int32) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteInt32(int32(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Int32) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadInt32() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Int32(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Int32) Size() int { 52 | return binaryutil.SizeofInt32() 53 | } 54 | 55 | // TypeId 类型 56 | func (Int32) TypeId() TypeId { 57 | return TypeId_Int32 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Int32) Indirect() any { 62 | return int32(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_int64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Int64 builtin int64 28 | type Int64 int64 29 | 30 | // Read implements io.Reader 31 | func (v Int64) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteVarint(int64(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Int64) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadVarint() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Int64(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v Int64) Size() int { 52 | return binaryutil.SizeofVarint(int64(v)) 53 | } 54 | 55 | // TypeId 类型 56 | func (Int64) TypeId() TypeId { 57 | return TypeId_Int64 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Int64) Indirect() any { 62 | return int64(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_int8.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Int8 builtin int8 28 | type Int8 int8 29 | 30 | // Read implements io.Reader 31 | func (v Int8) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteInt8(int8(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Int8) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadInt8() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Int8(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Int8) Size() int { 52 | return binaryutil.SizeofInt8() 53 | } 54 | 55 | // TypeId 类型 56 | func (Int8) TypeId() TypeId { 57 | return TypeId_Int8 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Int8) Indirect() any { 62 | return int8(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_null.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import "io" 23 | 24 | // Null builtin null 25 | type Null struct{} 26 | 27 | // Read implements io.Reader 28 | func (Null) Read(p []byte) (int, error) { 29 | return 0, io.EOF 30 | } 31 | 32 | // Write implements io.Writer 33 | func (Null) Write(p []byte) (int, error) { 34 | return 0, nil 35 | } 36 | 37 | // Size 大小 38 | func (Null) Size() int { 39 | return 0 40 | } 41 | 42 | // TypeId 类型 43 | func (Null) TypeId() TypeId { 44 | return TypeId_Null 45 | } 46 | 47 | // Indirect 原始值 48 | func (Null) Indirect() any { 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_string.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // String string 28 | type String string 29 | 30 | // Read implements io.Reader 31 | func (v String) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteString(string(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *String) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadString() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = String(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v String) Size() int { 52 | return binaryutil.SizeofString(string(v)) 53 | } 54 | 55 | // TypeId 类型 56 | func (String) TypeId() TypeId { 57 | return TypeId_String 58 | } 59 | 60 | // Indirect 原始值 61 | func (v String) Indirect() any { 62 | return string(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_uint.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Uint builtin uint 28 | type Uint uint 29 | 30 | // Read implements io.Reader 31 | func (v Uint) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteUvarint(uint64(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Uint) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadUvarint() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Uint(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v Uint) Size() int { 52 | return binaryutil.SizeofUvarint(uint64(v)) 53 | } 54 | 55 | // TypeId 类型 56 | func (Uint) TypeId() TypeId { 57 | return TypeId_Uint 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Uint) Indirect() any { 62 | return uint(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_uint16.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Uint16 builtin uint16 28 | type Uint16 uint16 29 | 30 | // Read implements io.Reader 31 | func (v Uint16) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteUint16(uint16(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Uint16) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadUint16() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Uint16(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Uint16) Size() int { 52 | return binaryutil.SizeofUint16() 53 | } 54 | 55 | // TypeId 类型 56 | func (Uint16) TypeId() TypeId { 57 | return TypeId_Uint16 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Uint16) Indirect() any { 62 | return uint16(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_uint32.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Uint32 builtin uint32 28 | type Uint32 uint32 29 | 30 | // Read implements io.Reader 31 | func (v Uint32) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteUint32(uint32(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Uint32) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadUint32() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Uint32(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Uint32) Size() int { 52 | return binaryutil.SizeofUint32() 53 | } 54 | 55 | // TypeId 类型 56 | func (Uint32) TypeId() TypeId { 57 | return TypeId_Uint32 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Uint32) Indirect() any { 62 | return uint32(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_uint64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Uint64 builtin uint64 28 | type Uint64 uint64 29 | 30 | // Read implements io.Reader 31 | func (v Uint64) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteUvarint(uint64(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Uint64) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadUvarint() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Uint64(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (v Uint64) Size() int { 52 | return binaryutil.SizeofUvarint(uint64(v)) 53 | } 54 | 55 | // TypeId 类型 56 | func (Uint64) TypeId() TypeId { 57 | return TypeId_Uint64 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Uint64) Indirect() any { 62 | return uint64(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/value_builtin_uint8.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // Uint8 builtin uint8 28 | type Uint8 uint8 29 | 30 | // Read implements io.Reader 31 | func (v Uint8) Read(p []byte) (int, error) { 32 | bs := binaryutil.NewBigEndianStream(p) 33 | if err := bs.WriteUint8(uint8(v)); err != nil { 34 | return bs.BytesWritten(), err 35 | } 36 | return bs.BytesWritten(), io.EOF 37 | } 38 | 39 | // Write implements io.Writer 40 | func (v *Uint8) Write(p []byte) (int, error) { 41 | bs := binaryutil.NewBigEndianStream(p) 42 | val, err := bs.ReadUint8() 43 | if err != nil { 44 | return bs.BytesRead(), err 45 | } 46 | *v = Uint8(val) 47 | return bs.BytesRead(), nil 48 | } 49 | 50 | // Size 大小 51 | func (Uint8) Size() int { 52 | return binaryutil.SizeofUint8() 53 | } 54 | 55 | // TypeId 类型 56 | func (Uint8) TypeId() TypeId { 57 | return TypeId_Uint8 58 | } 59 | 60 | // Indirect 原始值 61 | func (v Uint8) Indirect() any { 62 | return uint8(v) 63 | } 64 | -------------------------------------------------------------------------------- /net/gap/variant/variant_readonly.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/core" 25 | ) 26 | 27 | // MakeReadonlyVariant 创建只读可变类型 28 | func MakeReadonlyVariant(v ValueReader) (Variant, error) { 29 | if v == nil { 30 | return Variant{}, fmt.Errorf("%w: %w: v is nil", ErrVariant, core.ErrArgs) 31 | } 32 | return Variant{ 33 | TypeId: v.TypeId(), 34 | ReadonlyValue: v, 35 | }, nil 36 | } 37 | -------------------------------------------------------------------------------- /net/gap/variant/variant_serialized.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package variant 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/core" 25 | ) 26 | 27 | // MakeSerializedVariant 创建已序列化可变类型 28 | func MakeSerializedVariant(v ValueReader) (Variant, error) { 29 | if v == nil { 30 | return Variant{}, fmt.Errorf("%w: %w: v is nil", ErrVariant, core.ErrArgs) 31 | } 32 | 33 | sv, err := MakeSerializedValue(v) 34 | if err != nil { 35 | return Variant{}, err 36 | } 37 | 38 | return Variant{ 39 | TypeId: v.TypeId(), 40 | SerializedValue: sv, 41 | }, nil 42 | } 43 | -------------------------------------------------------------------------------- /net/gtp/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | // Package gtp Golaxy传输层协议(Golaxy Transfer Protocol),适用于长连接、实时通信的工作场景,需要工作在可靠网络协议(TCP/WebSocket)之上,支持链路加密、链路鉴权、断线续连等特性。 21 | /* 22 | - 关于加密,支持秘钥交换(ECDHE)、签名与验证,不支持证书验证,对于安全性要求极高的应用场景,应该使用TLS协议直接加密链路,并关闭本协议的数据加密选项。 23 | - 关于断线续联,支持下层协议断连后,双端缓存待发送的消息包,待使用新连接重连后继续收发消息。 24 | - 支持消息包压缩。 25 | - 支持可靠的消息包传输时序。 26 | - 支持新增自定义消息。 27 | - 协议适用于网络游戏、远程控制和传感器交互等实时性要求较高的场景。 28 | - 为了提高性能,本层所有消息中[]byte和string类型字段,应该使用ReadBytesRef和ReadStringRef读取。 29 | */ 30 | package gtp 31 | -------------------------------------------------------------------------------- /net/gtp/marshaler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/framework/utils/binaryutil" 25 | ) 26 | 27 | // Marshal 序列化 28 | func Marshal[T MsgReader](msg T) (ret binaryutil.RecycleBytes, err error) { 29 | bs := binaryutil.MakeRecycleBytes(msg.Size()) 30 | defer func() { 31 | if !bs.Equal(ret) { 32 | bs.Release() 33 | } 34 | }() 35 | 36 | if _, err := binaryutil.CopyToBuff(bs.Data(), msg); err != nil { 37 | return binaryutil.NilRecycleBytes, fmt.Errorf("%w: marshal msg(%d) failed, %w", ErrGTP, msg.MsgId(), err) 38 | } 39 | 40 | return bs, nil 41 | } 42 | 43 | // Unmarshal 反序列化 44 | func Unmarshal[T MsgWriter](msg T, data []byte) error { 45 | if _, err := msg.Write(data); err != nil { 46 | return fmt.Errorf("%w: unmarshal msg(%d) failed, %w", ErrGTP, msg.MsgId(), err) 47 | } 48 | return nil 49 | } 50 | -------------------------------------------------------------------------------- /net/gtp/method/errors.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package method 21 | 22 | import "errors" 23 | 24 | var ( 25 | ErrInvalidMethod = errors.New("gtp: invalid method") 26 | ) 27 | -------------------------------------------------------------------------------- /net/gtp/method/hash.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package method 21 | 22 | import ( 23 | "crypto/sha256" 24 | "git.golaxy.org/framework/net/gtp" 25 | "hash" 26 | "hash/fnv" 27 | ) 28 | 29 | // NewHash 创建Hash 30 | func NewHash(h gtp.Hash) (hash.Hash, error) { 31 | switch h { 32 | case gtp.Hash_Fnv1a128: 33 | return fnv.New128a(), nil 34 | case gtp.Hash_SHA256: 35 | return sha256.New(), nil 36 | default: 37 | return nil, ErrInvalidMethod 38 | } 39 | } 40 | 41 | // NewHash32 创建Hash32 42 | func NewHash32(h gtp.Hash) (hash.Hash32, error) { 43 | switch h { 44 | case gtp.Hash_Fnv1a32: 45 | return fnv.New32a(), nil 46 | default: 47 | return nil, ErrInvalidMethod 48 | } 49 | } 50 | 51 | // NewHash64 创建Hash64 52 | func NewHash64(h gtp.Hash) (hash.Hash64, error) { 53 | switch h { 54 | case gtp.Hash_Fnv1a64: 55 | return fnv.New64a(), nil 56 | default: 57 | return nil, ErrInvalidMethod 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /net/gtp/method/namedcurve.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package method 21 | 22 | import ( 23 | "crypto/ecdh" 24 | "git.golaxy.org/framework/net/gtp" 25 | ) 26 | 27 | // NewNamedCurve 创建曲线 28 | func NewNamedCurve(nc gtp.NamedCurve) (ecdh.Curve, error) { 29 | switch nc { 30 | case gtp.NamedCurve_X25519: 31 | return ecdh.X25519(), nil 32 | case gtp.NamedCurve_P256: 33 | return ecdh.P256(), nil 34 | default: 35 | return nil, ErrInvalidMethod 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /net/gtp/msg.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "errors" 24 | "io" 25 | ) 26 | 27 | var ( 28 | ErrGTP = errors.New("gtp") // 消息协议错误 29 | ) 30 | 31 | // Msg 消息接口 32 | type Msg interface { 33 | MsgReader 34 | MsgWriter 35 | } 36 | 37 | // MsgReader 读取消息 38 | type MsgReader interface { 39 | io.Reader 40 | // Size 大小 41 | Size() int 42 | // MsgId 消息Id 43 | MsgId() MsgId 44 | // Clone 克隆消息对象 45 | Clone() MsgReader 46 | } 47 | 48 | // MsgWriter 写入消息 49 | type MsgWriter interface { 50 | io.Writer 51 | // Size 大小 52 | Size() int 53 | // MsgId 消息Id 54 | MsgId() MsgId 55 | } 56 | -------------------------------------------------------------------------------- /net/gtp/msg_continue.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // MsgContinue 重连 28 | type MsgContinue struct { 29 | SendSeq uint32 // 客户端请求消息序号 30 | RecvSeq uint32 // 客户端响应消息序号 31 | } 32 | 33 | // Read implements io.Reader 34 | func (m MsgContinue) Read(p []byte) (int, error) { 35 | bs := binaryutil.NewBigEndianStream(p) 36 | if err := bs.WriteUint32(m.SendSeq); err != nil { 37 | return bs.BytesWritten(), err 38 | } 39 | if err := bs.WriteUint32(m.RecvSeq); err != nil { 40 | return bs.BytesWritten(), err 41 | } 42 | return bs.BytesWritten(), io.EOF 43 | } 44 | 45 | // Write implements io.Writer 46 | func (m *MsgContinue) Write(p []byte) (int, error) { 47 | bs := binaryutil.NewBigEndianStream(p) 48 | var err error 49 | 50 | m.SendSeq, err = bs.ReadUint32() 51 | if err != nil { 52 | return bs.BytesRead(), err 53 | } 54 | 55 | m.RecvSeq, err = bs.ReadUint32() 56 | if err != nil { 57 | return bs.BytesRead(), err 58 | } 59 | 60 | return bs.BytesRead(), nil 61 | } 62 | 63 | // Size 大小 64 | func (MsgContinue) Size() int { 65 | return binaryutil.SizeofUint32() + binaryutil.SizeofUint32() 66 | } 67 | 68 | // MsgId 消息Id 69 | func (MsgContinue) MsgId() MsgId { 70 | return MsgId_Continue 71 | } 72 | 73 | // Clone 克隆消息对象 74 | func (m MsgContinue) Clone() MsgReader { 75 | return m 76 | } 77 | -------------------------------------------------------------------------------- /net/gtp/msg_heartbeat.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import "io" 23 | 24 | // Heartbeat消息标志位 25 | const ( 26 | Flag_Ping Flag = 1 << (iota + Flag_Customize) // 心跳ping 27 | Flag_Pong // 心跳pong 28 | ) 29 | 30 | // MsgHeartbeat 心跳,消息体为空,可以不解析 31 | type MsgHeartbeat struct{} 32 | 33 | // Read implements io.Reader 34 | func (MsgHeartbeat) Read(p []byte) (int, error) { 35 | return 0, io.EOF 36 | } 37 | 38 | // Write implements io.Writer 39 | func (MsgHeartbeat) Write(p []byte) (int, error) { 40 | return 0, nil 41 | } 42 | 43 | // Size 大小 44 | func (MsgHeartbeat) Size() int { 45 | return 0 46 | } 47 | 48 | // MsgId 消息Id 49 | func (MsgHeartbeat) MsgId() MsgId { 50 | return MsgId_Heartbeat 51 | } 52 | 53 | // Clone 克隆消息对象 54 | func (m MsgHeartbeat) Clone() MsgReader { 55 | return m 56 | } 57 | -------------------------------------------------------------------------------- /net/gtp/msg_payload.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "bytes" 24 | "git.golaxy.org/framework/utils/binaryutil" 25 | "io" 26 | ) 27 | 28 | // MsgPayload 数据传输(注意:为了提高解码性能,减少内存碎片,解码string与bytes字段时均使用引用类型,引用字节池中的bytes,GC时会被归还字节池,不要直接持有此类型字段) 29 | type MsgPayload struct { 30 | Data []byte // 数据 31 | } 32 | 33 | // Read implements io.Reader 34 | func (m MsgPayload) Read(p []byte) (int, error) { 35 | bs := binaryutil.NewBigEndianStream(p) 36 | if err := bs.WriteBytes(m.Data); err != nil { 37 | return bs.BytesWritten(), err 38 | } 39 | return bs.BytesWritten(), io.EOF 40 | } 41 | 42 | // Write implements io.Writer 43 | func (m *MsgPayload) Write(p []byte) (int, error) { 44 | bs := binaryutil.NewBigEndianStream(p) 45 | var err error 46 | 47 | m.Data, err = bs.ReadBytesRef() 48 | if err != nil { 49 | return bs.BytesRead(), err 50 | } 51 | 52 | return bs.BytesRead(), nil 53 | } 54 | 55 | // Size 大小 56 | func (m MsgPayload) Size() int { 57 | return binaryutil.SizeofBytes(m.Data) 58 | } 59 | 60 | // MsgId 消息Id 61 | func (MsgPayload) MsgId() MsgId { 62 | return MsgId_Payload 63 | } 64 | 65 | // Clone 克隆消息对象 66 | func (m MsgPayload) Clone() MsgReader { 67 | return MsgPayload{ 68 | Data: bytes.Clone(m.Data), 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /net/gtp/msgcompressed.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "git.golaxy.org/framework/utils/binaryutil" 24 | "io" 25 | ) 26 | 27 | // MsgCompressed 压缩消息 28 | type MsgCompressed struct { 29 | Data []byte 30 | OriginalSize int64 31 | } 32 | 33 | // Read implements io.Reader 34 | func (m MsgCompressed) Read(p []byte) (int, error) { 35 | bs := binaryutil.NewBigEndianStream(p) 36 | if err := bs.WriteBytes(m.Data); err != nil { 37 | return bs.BytesWritten(), err 38 | } 39 | if err := bs.WriteVarint(m.OriginalSize); err != nil { 40 | return bs.BytesWritten(), err 41 | } 42 | return bs.BytesWritten(), io.EOF 43 | } 44 | 45 | // Write implements io.Writer 46 | func (m *MsgCompressed) Write(p []byte) (int, error) { 47 | bs := binaryutil.NewBigEndianStream(p) 48 | var err error 49 | 50 | m.Data, err = bs.ReadBytesRef() 51 | if err != nil { 52 | return bs.BytesRead(), err 53 | } 54 | 55 | m.OriginalSize, err = bs.ReadVarint() 56 | if err != nil { 57 | return bs.BytesRead(), err 58 | } 59 | 60 | return bs.BytesRead(), nil 61 | } 62 | 63 | // Size 大小 64 | func (m MsgCompressed) Size() int { 65 | return binaryutil.SizeofBytes(m.Data) + binaryutil.SizeofVarint(m.OriginalSize) 66 | } 67 | -------------------------------------------------------------------------------- /net/gtp/msgid.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | // MsgId 消息Id 23 | type MsgId = uint8 24 | -------------------------------------------------------------------------------- /net/gtp/msgid_builtin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | const ( 23 | MsgId_None MsgId = iota // 未设置 24 | MsgId_Hello // Hello Handshake C<->S 不加密 25 | MsgId_ECDHESecretKeyExchange // ECDHE秘钥交换 Handshake S<->C 不加密 26 | MsgId_ChangeCipherSpec // 变更密码规范 Handshake S<->C 不加密 27 | MsgId_Auth // 鉴权 Handshake C->S 加密 28 | MsgId_Continue // 重连 Handshake C->S 加密 29 | MsgId_Finished // 握手结束 Handshake S<->C 加密 30 | MsgId_Rst // 重置链路 Ctrl S->C 加密 31 | MsgId_Heartbeat // 心跳 Ctrl C<->S or S<->C 加密 32 | MsgId_SyncTime // 时钟同步 Ctrl C<->S 加密 33 | MsgId_Payload // 数据传输 Trans C<->S or S<->C 加密 34 | MsgId_Customize = 16 // 自定义消息起点 35 | ) 36 | -------------------------------------------------------------------------------- /net/gtp/sign.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package gtp 21 | 22 | import ( 23 | "crypto/rsa" 24 | "crypto/x509" 25 | "encoding/pem" 26 | "io" 27 | "os" 28 | ) 29 | 30 | // LoadPublicKeyFile 加载公钥文件 31 | func LoadPublicKeyFile(filePath string) (*rsa.PublicKey, error) { 32 | f, err := os.Open(filePath) 33 | if err != nil { 34 | return nil, err 35 | } 36 | defer f.Close() 37 | 38 | return ReadPublicKey(f) 39 | } 40 | 41 | // LoadPrivateKeyFile 加载私钥文件 42 | func LoadPrivateKeyFile(filePath string) (*rsa.PrivateKey, error) { 43 | f, err := os.Open(filePath) 44 | if err != nil { 45 | return nil, err 46 | } 47 | defer f.Close() 48 | 49 | return ReadPrivateKey(f) 50 | } 51 | 52 | // ReadPublicKey 读取公钥 53 | func ReadPublicKey(reader io.Reader) (*rsa.PublicKey, error) { 54 | bs, err := io.ReadAll(reader) 55 | if err != nil { 56 | return nil, err 57 | } 58 | 59 | block, _ := pem.Decode(bs) 60 | 61 | pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes) 62 | if err != nil { 63 | return nil, err 64 | } 65 | 66 | return pubKey, nil 67 | } 68 | 69 | // ReadPrivateKey 读取私钥 70 | func ReadPrivateKey(reader io.Reader) (*rsa.PrivateKey, error) { 71 | bs, err := io.ReadAll(reader) 72 | if err != nil { 73 | return nil, err 74 | } 75 | 76 | block, _ := pem.Decode(bs) 77 | 78 | privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) 79 | if err != nil { 80 | return nil, err 81 | } 82 | 83 | return privKey, nil 84 | } 85 | -------------------------------------------------------------------------------- /net/gtp/transport/event.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package transport 21 | 22 | import ( 23 | "errors" 24 | "fmt" 25 | "git.golaxy.org/core/utils/exception" 26 | "git.golaxy.org/framework/net/gtp" 27 | ) 28 | 29 | var ( 30 | ErrEvent = errors.New("gtp-event") // 消息事件错误 31 | ErrIncorrectMsg = fmt.Errorf("%w: incorrect msg type", ErrEvent) // 错误的消息类型 32 | ) 33 | 34 | // IEvent 消息事件接口 35 | type IEvent = Event[gtp.MsgReader] 36 | 37 | // Event 消息事件 38 | type Event[T gtp.MsgReader] struct { 39 | Flags gtp.Flags // 标志位 40 | Seq uint32 // 消息序号 41 | Ack uint32 // 应答序号 42 | Msg T // 消息 43 | } 44 | 45 | // Interface 泛化事件,转换为事件接口 46 | func (e Event[T]) Interface() IEvent { 47 | return IEvent{ 48 | Flags: e.Flags, 49 | Seq: e.Seq, 50 | Ack: e.Ack, 51 | Msg: e.Msg, 52 | } 53 | } 54 | 55 | // EventT 特化事件,转换为事件具体类型 56 | func EventT[T gtp.MsgReader](e IEvent) Event[T] { 57 | ret := Event[T]{ 58 | Flags: e.Flags, 59 | Seq: e.Seq, 60 | Ack: e.Ack, 61 | } 62 | 63 | if e.Msg == nil { 64 | return ret 65 | } 66 | 67 | msgPtr, ok := any(e.Msg).(*T) 68 | if ok { 69 | ret.Msg = *msgPtr 70 | return ret 71 | } 72 | 73 | msg, ok := any(e.Msg).(T) 74 | if ok { 75 | ret.Msg = msg 76 | return ret 77 | } 78 | 79 | exception.Panic(ErrIncorrectMsg) 80 | panic("unreachable") 81 | } 82 | -------------------------------------------------------------------------------- /net/gtp/transport/protocol.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package transport 21 | 22 | import ( 23 | "errors" 24 | "fmt" 25 | ) 26 | 27 | var ( 28 | ErrProtocol = errors.New("gtp-protocol") // 消息协议错误 29 | ErrUnexpectedMsg = fmt.Errorf("%w: unexpected msg", ErrProtocol) // 收到非预期的消息 30 | ) 31 | -------------------------------------------------------------------------------- /net/gtp/transport/rsterror.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package transport 21 | 22 | import ( 23 | "fmt" 24 | "git.golaxy.org/framework/net/gtp" 25 | ) 26 | 27 | // RstError Rst错误提示 28 | type RstError struct { 29 | Code gtp.Code // 错误码 30 | Message string // 错误信息 31 | } 32 | 33 | // Error 错误信息 34 | func (err RstError) Error() string { 35 | return fmt.Sprintf("(%d) %s", err.Code, err.Message) 36 | } 37 | 38 | // ToEvent 转换为消息事件 39 | func (err RstError) ToEvent() Event[gtp.MsgRst] { 40 | return Event[gtp.MsgRst]{ 41 | Msg: gtp.MsgRst{ 42 | Code: err.Code, 43 | Message: err.Message, 44 | }, 45 | } 46 | } 47 | 48 | // CastRstErr Rst错误事件转换为错误提示 49 | func CastRstErr(e Event[gtp.MsgRst]) *RstError { 50 | return &RstError{ 51 | Code: e.Msg.Code, 52 | Message: e.Msg.Message, 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /net/gtp/transport/synchronizer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package transport 21 | 22 | import ( 23 | "errors" 24 | "fmt" 25 | "git.golaxy.org/framework/net/gtp/codec" 26 | "io" 27 | ) 28 | 29 | var ( 30 | ErrSynchronizer = errors.New("gtp-synchronizer") // 同步器错误 31 | ErrUnexpectedSeq = fmt.Errorf("%w: unexpected sequence", ErrSynchronizer) // 收到非预期的消息序号,表示序号不连续 32 | ErrDiscardSeq = fmt.Errorf("%w: discard sequence", ErrSynchronizer) // 收到已过期的消息序号,表示次消息已收到过 33 | ) 34 | 35 | // ISynchronizer 同步器 36 | type ISynchronizer interface { 37 | io.Writer 38 | io.WriterTo 39 | codec.IValidation 40 | // Synchronize 同步对端时序,对齐缓存序号 41 | Synchronize(remoteRecvSeq uint32) error 42 | // Ack 确认消息序号 43 | Ack(ack uint32) 44 | // SendSeq 发送消息序号 45 | SendSeq() uint32 46 | // RecvSeq 接收消息序号 47 | RecvSeq() uint32 48 | // AckSeq 当前ack序号 49 | AckSeq() uint32 50 | // Cap 缓存区容量 51 | Cap() int 52 | // Cached 已缓存大小 53 | Cached() int 54 | // Dispose 释放资源 55 | Dispose() 56 | } 57 | -------------------------------------------------------------------------------- /net/gtp/transport/transceiver_retry.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package transport 21 | 22 | import ( 23 | "context" 24 | "errors" 25 | "fmt" 26 | ) 27 | 28 | // Retry 网络io超时时重试 29 | type Retry struct { 30 | Transceiver *Transceiver 31 | Times int 32 | Ctx context.Context 33 | } 34 | 35 | // Send 重试发送 36 | func (r Retry) Send(err error) error { 37 | if err == nil { 38 | return nil 39 | } 40 | if !errors.Is(err, ErrDeadlineExceeded) { 41 | return err 42 | } 43 | ctx := r.Ctx 44 | if ctx == nil { 45 | ctx = context.Background() 46 | } 47 | for i := r.Times; i > 0; i-- { 48 | select { 49 | case <-ctx.Done(): 50 | return fmt.Errorf("gtp: %w", context.Canceled) 51 | default: 52 | } 53 | if err = r.Transceiver.Resend(); err != nil { 54 | if errors.Is(err, ErrDeadlineExceeded) { 55 | continue 56 | } 57 | } 58 | break 59 | } 60 | return err 61 | } 62 | 63 | // Recv 重试接收 64 | func (r Retry) Recv(e IEvent, err error) (IEvent, error) { 65 | if err == nil { 66 | return e, nil 67 | } 68 | if !errors.Is(err, ErrDeadlineExceeded) && !errors.Is(err, ErrDiscardSeq) { 69 | return e, err 70 | } 71 | for i := r.Times; i > 0; { 72 | e, err = r.Transceiver.Recv(r.Ctx) 73 | if err != nil { 74 | if errors.Is(err, ErrDeadlineExceeded) { 75 | i-- 76 | continue 77 | } 78 | if errors.Is(err, ErrDiscardSeq) { 79 | continue 80 | } 81 | } 82 | break 83 | } 84 | return e, err 85 | } 86 | -------------------------------------------------------------------------------- /net/netpath/details.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package netpath 21 | 22 | import ( 23 | "strings" 24 | ) 25 | 26 | // Domain 域 27 | type Domain struct { 28 | Path string // 路径 29 | Sep string // 分隔符 30 | } 31 | 32 | // IsValid 是否有效 33 | func (d Domain) IsValid() bool { 34 | return d.Path != "" && d.Sep != "" 35 | } 36 | 37 | // Contains 包含路径 38 | func (d Domain) Contains(path string) bool { 39 | return InDir(d.Sep, path, d.Path) 40 | } 41 | 42 | // Equal 等于路径 43 | func (d Domain) Equal(path string) bool { 44 | return Equal(d.Sep, path, d.Path) 45 | } 46 | 47 | // Join 拼接路径 48 | func (d Domain) Join(elems ...string) string { 49 | return Join(d.Sep, append([]string{d.Path}, elems...)...) 50 | } 51 | 52 | // Relative 相对路径 53 | func (d Domain) Relative(path string) (string, bool) { 54 | if !d.Contains(path) { 55 | return "", false 56 | } 57 | return strings.TrimPrefix(strings.TrimPrefix(path, d.Path), d.Sep), true 58 | } 59 | 60 | // NodeDetails 节点地址信息 61 | type NodeDetails struct { 62 | DomainRoot Domain // 根域 63 | DomainBroadcast Domain // 广播地址子域 64 | DomainBalance Domain // 负载均衡地址子域 65 | DomainMulticast Domain // 组播地址子域 66 | DomainUnicast Domain // 单播地址子域 67 | } 68 | -------------------------------------------------------------------------------- /net/netpath/path.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package netpath 21 | 22 | import ( 23 | "strings" 24 | ) 25 | 26 | // Join 拼接路径 27 | func Join(sep string, elems ...string) string { 28 | return strings.Join(elems, sep) 29 | } 30 | 31 | // Split 分割路径 32 | func Split(sep, path string) (dir, file string) { 33 | idx := strings.LastIndex(path, sep) 34 | if idx < 0 { 35 | return "", path 36 | } 37 | return path[:idx], path[idx+len(sep):] 38 | } 39 | 40 | // Root 根目录 41 | func Root(sep, path string) string { 42 | idx := strings.Index(path, sep) 43 | if idx < 0 { 44 | return path 45 | } 46 | return path[:idx] 47 | } 48 | 49 | // Base 基础目录 50 | func Base(sep, path string) string { 51 | idx := strings.LastIndex(path, sep) 52 | if idx < 0 { 53 | return path 54 | } 55 | return path[idx+len(sep):] 56 | } 57 | 58 | // Dir 上层目录 59 | func Dir(sep, path string) string { 60 | idx := strings.LastIndex(path, sep) 61 | if idx < 0 { 62 | return "" 63 | } 64 | return path[:idx] 65 | } 66 | 67 | // InDir 在目录中 68 | func InDir(sep, path, dir string) bool { 69 | path = strings.TrimSuffix(path, sep) 70 | dir = strings.TrimSuffix(dir, sep) 71 | 72 | if !strings.HasPrefix(path, dir) { 73 | return false 74 | } 75 | 76 | return strings.HasPrefix(path[len(dir):], sep) 77 | } 78 | 79 | // Equal 路径是否相等 80 | func Equal(sep, a, b string) bool { 81 | return strings.TrimSuffix(a, sep) == strings.TrimSuffix(b, sep) 82 | } 83 | -------------------------------------------------------------------------------- /runtime_addin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | type InstallRuntimeLogger interface { 23 | InstallLogger(rt IRuntime) 24 | } 25 | 26 | type InstallRuntimeRPCStack interface { 27 | InstallRPCStack(rt IRuntime) 28 | } 29 | 30 | type InstallRuntimeDistEntityRegistry interface { 31 | InstallDistEntityRegistry(rt IRuntime) 32 | } 33 | -------------------------------------------------------------------------------- /runtime_instantiation.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core" 24 | "git.golaxy.org/core/utils/exception" 25 | "reflect" 26 | ) 27 | 28 | // IRuntimeInstantiation 运行时实例化接口 29 | type IRuntimeInstantiation interface { 30 | Instantiate() IRuntime 31 | } 32 | 33 | func newRuntimeInstantiation(rtInst any) *_RuntimeInstantiation { 34 | if rtInst == nil { 35 | exception.Panicf("%w: %w: rtInst is nil", ErrFramework, core.ErrArgs) 36 | } 37 | 38 | rtInstRT, ok := rtInst.(reflect.Type) 39 | if !ok { 40 | rtInstRT = reflect.ValueOf(rtInst).Type() 41 | } 42 | 43 | for rtInstRT.Kind() == reflect.Pointer { 44 | rtInstRT = rtInstRT.Elem() 45 | } 46 | 47 | if rtInstRT.PkgPath() == "" || rtInstRT.Name() == "" || !reflect.PointerTo(rtInstRT).Implements(reflect.TypeFor[IRuntime]()) { 48 | exception.Panicf("%w: unsupported type", ErrFramework) 49 | } 50 | 51 | return &_RuntimeInstantiation{ 52 | runtimeInstanceRT: rtInstRT, 53 | } 54 | } 55 | 56 | type _RuntimeInstantiation struct { 57 | RuntimeGeneric 58 | runtimeInstanceRT reflect.Type 59 | } 60 | 61 | func (r *_RuntimeInstantiation) Instantiate() IRuntime { 62 | if r.runtimeInstanceRT == nil { 63 | exception.Panicf("%w: runtimeInstanceRT is nil", ErrFramework) 64 | } 65 | return reflect.New(r.runtimeInstanceRT).Interface().(IRuntime) 66 | } 67 | -------------------------------------------------------------------------------- /service_addin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | type InstallServiceLogger interface { 23 | InstallLogger(svc IService) 24 | } 25 | 26 | type InstallServiceConfig interface { 27 | InstallConfig(svc IService) 28 | } 29 | 30 | type InstallServiceBroker interface { 31 | InstallBroker(svc IService) 32 | } 33 | 34 | type InstallServiceRegistry interface { 35 | InstallRegistry(svc IService) 36 | } 37 | 38 | type InstallServiceDistSync interface { 39 | InstallDistSync(svc IService) 40 | } 41 | 42 | type InstallServiceDistService interface { 43 | InstallDistService(svc IService) 44 | } 45 | 46 | type InstallServiceRPC interface { 47 | InstallRPC(svc IService) 48 | } 49 | 50 | type InstallServiceDistEntityQuerier interface { 51 | InstallDistEntityQuerier(svc IService) 52 | } 53 | -------------------------------------------------------------------------------- /service_instantiation.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package framework 21 | 22 | import ( 23 | "git.golaxy.org/core" 24 | "git.golaxy.org/core/utils/exception" 25 | "reflect" 26 | ) 27 | 28 | // IServiceInstantiation 服务实例化接口 29 | type IServiceInstantiation interface { 30 | Instantiate() IService 31 | } 32 | 33 | func newServiceInstantiation(svcInst any) *_ServiceInstantiation { 34 | if svcInst == nil { 35 | exception.Panicf("%w: %w: svcInst is nil", ErrFramework, core.ErrArgs) 36 | } 37 | 38 | svcInstRT, ok := svcInst.(reflect.Type) 39 | if !ok { 40 | svcInstRT = reflect.ValueOf(svcInst).Type() 41 | } 42 | 43 | for svcInstRT.Kind() == reflect.Pointer { 44 | svcInstRT = svcInstRT.Elem() 45 | } 46 | 47 | if svcInstRT.PkgPath() == "" || svcInstRT.Name() == "" || !reflect.PointerTo(svcInstRT).Implements(reflect.TypeFor[IService]()) { 48 | exception.Panicf("%w: unsupported type", ErrFramework) 49 | } 50 | 51 | return &_ServiceInstantiation{ 52 | serviceInstanceRT: svcInstRT, 53 | } 54 | } 55 | 56 | type _ServiceInstantiation struct { 57 | ServiceGeneric 58 | serviceInstanceRT reflect.Type 59 | } 60 | 61 | func (s *_ServiceInstantiation) Instantiate() IService { 62 | if s.serviceInstanceRT == nil { 63 | exception.Panicf("%w: serviceInstanceRT is nil", ErrFramework) 64 | } 65 | return reflect.New(s.serviceInstanceRT).Interface().(IService) 66 | } 67 | -------------------------------------------------------------------------------- /utils/binaryutil/byteswriter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package binaryutil 21 | 22 | // BytesWriter will only write bytes to the underlying writer until the limit is reached. 23 | type BytesWriter struct { 24 | N int 25 | Bytes []byte 26 | } 27 | 28 | // NewBytesWriter creates a new instance of BytesWriter. 29 | func NewBytesWriter(bs []byte) *BytesWriter { 30 | return &BytesWriter{ 31 | N: 0, 32 | Bytes: bs, 33 | } 34 | } 35 | 36 | // Write implements io.Writer 37 | func (l *BytesWriter) Write(p []byte) (int, error) { 38 | if l.N >= len(l.Bytes) { 39 | return 0, ErrLimitReached 40 | } 41 | 42 | // Write 0 bytes if the limit is to be exceeded. 43 | if len(p) > len(l.Bytes)-l.N { 44 | return 0, ErrLimitReached 45 | } 46 | 47 | copy(l.Bytes[l.N:], p) 48 | l.N += len(p) 49 | 50 | return len(p), nil 51 | } 52 | -------------------------------------------------------------------------------- /utils/binaryutil/copy.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package binaryutil 21 | 22 | import "io" 23 | 24 | func CopyToBuff[T io.Reader](p []byte, reader T) (int64, error) { 25 | n, err := reader.Read(p) 26 | if err == io.EOF { 27 | err = nil 28 | } 29 | return int64(n), err 30 | } 31 | 32 | func CopyToByteStream[T io.Reader](bs *ByteStream, reader T) (int64, error) { 33 | n, err := reader.Read(bs.wp) 34 | if err == io.EOF { 35 | err = nil 36 | } 37 | bs.wp = bs.wp[n:] 38 | return int64(n), err 39 | } 40 | -------------------------------------------------------------------------------- /utils/binaryutil/limitwriter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package binaryutil 21 | 22 | import ( 23 | "errors" 24 | "io" 25 | ) 26 | 27 | var ( 28 | ErrLimitReached = errors.New("i/o limit reached") 29 | ) 30 | 31 | // LimitWriter will only write bytes to the underlying writer until the limit is reached. 32 | type LimitWriter struct { 33 | Limit int 34 | N int 35 | W io.Writer 36 | } 37 | 38 | // NewLimitWriter creates a new instance of LimitWriter. 39 | func NewLimitWriter(w io.Writer, n int) *LimitWriter { 40 | // If anyone tries this, just make a 0 writer. 41 | if n < 0 { 42 | n = 0 43 | } 44 | return &LimitWriter{ 45 | Limit: n, 46 | N: 0, 47 | W: w, 48 | } 49 | } 50 | 51 | // Write implements io.Writer 52 | func (l *LimitWriter) Write(p []byte) (int, error) { 53 | if l.N >= l.Limit { 54 | return 0, ErrLimitReached 55 | } 56 | 57 | // Write 0 bytes if the limit is to be exceeded. 58 | if len(p) > l.Limit-l.N { 59 | return 0, ErrLimitReached 60 | } 61 | 62 | n, err := l.W.Write(p) 63 | l.N += n 64 | return n, err 65 | } 66 | -------------------------------------------------------------------------------- /utils/concurrent/futures_future.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | "github.com/elliotchance/pie/v2" 26 | "time" 27 | ) 28 | 29 | // MakeFuture 创建Future 30 | func MakeFuture[T Resp](fs *Futures, ctx context.Context, resp T, timeout ...time.Duration) Future { 31 | if ctx == nil { 32 | ctx = context.Background() 33 | } 34 | 35 | _timeout := pie.First(timeout) 36 | if _timeout <= 0 { 37 | _timeout = fs.timeout 38 | } 39 | 40 | task := newTask(fs, resp) 41 | go task.Run(ctx, _timeout) 42 | 43 | return task.Future() 44 | } 45 | 46 | // Future 异步模型Future 47 | type Future struct { 48 | Finish context.Context // 上下文 49 | Id int64 // Id 50 | futures *Futures 51 | } 52 | 53 | // Cancel 取消 54 | func (f Future) Cancel(err error) { 55 | f.futures.Resolve(f.Id, async.MakeRet(nil, err)) 56 | } 57 | 58 | // Wait 等待 59 | func (f Future) Wait(ctx context.Context) { 60 | if ctx == nil { 61 | ctx = context.Background() 62 | } 63 | 64 | select { 65 | case <-ctx.Done(): 66 | case <-f.Finish.Done(): 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /utils/concurrent/futures_resp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import "git.golaxy.org/core/utils/async" 23 | 24 | // Resp 响应接口 25 | type Resp interface { 26 | // Push 填入返回结果 27 | Push(ret async.Ret) error 28 | } 29 | -------------------------------------------------------------------------------- /utils/concurrent/futures_respasyncret.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | "time" 26 | ) 27 | 28 | // MakeRespAsyncRet 创建接收响应返回值的异步调用结果 29 | func MakeRespAsyncRet() RespAsyncRet { 30 | return async.MakeAsyncRet() 31 | } 32 | 33 | // MakeFutureRespAsyncRet 创建future与接收响应返回值的异步调用结果 34 | func MakeFutureRespAsyncRet(fs *Futures, ctx context.Context, timeout ...time.Duration) (Future, RespAsyncRet) { 35 | resp := MakeRespAsyncRet() 36 | future := MakeFuture(fs, ctx, resp, timeout...) 37 | return future, resp 38 | } 39 | 40 | // RespAsyncRet 接收响应返回值的异步调用结果 41 | type RespAsyncRet chan async.Ret 42 | 43 | // Push 填入返回结果 44 | func (resp RespAsyncRet) Push(ret async.Ret) error { 45 | async.Return(resp, ret) 46 | return nil 47 | } 48 | 49 | // ToAsyncRet 转换为异步调用结果 50 | func (resp RespAsyncRet) ToAsyncRet() async.AsyncRet { 51 | return chan async.Ret(resp) 52 | } 53 | -------------------------------------------------------------------------------- /utils/concurrent/futures_respasyncrett.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | "git.golaxy.org/core/utils/types" 26 | "time" 27 | ) 28 | 29 | // MakeRespAsyncRetT 创建接收响应返回值的异步调用结果 30 | func MakeRespAsyncRetT[T any]() RespAsyncRetT[T] { 31 | return async.MakeAsyncRetT[T]() 32 | } 33 | 34 | // MakeFutureRespAsyncRetT 创建future与接收响应返回值的异步调用结果 35 | func MakeFutureRespAsyncRetT[T any](fs *Futures, ctx context.Context, timeout ...time.Duration) (Future, RespAsyncRetT[T]) { 36 | resp := MakeRespAsyncRetT[T]() 37 | future := MakeFuture(fs, ctx, resp, timeout...) 38 | return future, resp 39 | } 40 | 41 | // RespAsyncRetT 接收响应返回值的channel 42 | type RespAsyncRetT[T any] chan async.RetT[T] 43 | 44 | // Push 填入返回结果 45 | func (resp RespAsyncRetT[T]) Push(ret async.Ret) error { 46 | retT, ok := async.AsRetT[T](ret) 47 | if !ok { 48 | async.ReturnT(resp, async.MakeRetT[T](types.ZeroT[T](), ErrFutureRespIncorrectType)) 49 | return nil 50 | } 51 | 52 | async.ReturnT(resp, retT) 53 | return nil 54 | } 55 | 56 | // ToAsyncRetT 转换为异步调用结果 57 | func (resp RespAsyncRetT[T]) ToAsyncRetT() async.AsyncRetT[T] { 58 | return chan async.RetT[T](resp) 59 | } 60 | -------------------------------------------------------------------------------- /utils/concurrent/futures_respfunc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/async" 24 | "git.golaxy.org/core/utils/generic" 25 | "git.golaxy.org/core/utils/types" 26 | ) 27 | 28 | // RespFunc 接收响应返回值的函数 29 | type RespFunc[T any] generic.Action1[async.RetT[T]] 30 | 31 | // Push 填入返回结果 32 | func (resp RespFunc[T]) Push(ret async.Ret) error { 33 | if !ret.OK() { 34 | generic.CastAction1(resp).UnsafeCall(async.MakeRetT[T](types.ZeroT[T](), ret.Error)) 35 | return nil 36 | } 37 | 38 | retT, ok := async.AsRetT[T](ret) 39 | if !ok { 40 | generic.CastAction1(resp).UnsafeCall(async.MakeRetT[T](types.ZeroT[T](), ErrFutureRespIncorrectType)) 41 | return nil 42 | } 43 | 44 | generic.CastAction1(resp).UnsafeCall(retT) 45 | return nil 46 | } 47 | 48 | // RespDelegate 接收响应返回值的委托 49 | type RespDelegate[T any] generic.DelegateVoid1[async.RetT[T]] 50 | 51 | // Push 填入返回结果 52 | func (resp RespDelegate[T]) Push(ret async.Ret) error { 53 | if !ret.OK() { 54 | generic.DelegateVoid1[async.RetT[T]](resp).UnsafeCall(nil, async.MakeRetT[T](types.ZeroT[T](), ret.Error)) 55 | return nil 56 | } 57 | 58 | retT, ok := async.AsRetT[T](ret) 59 | if !ok { 60 | generic.DelegateVoid1[async.RetT[T]](resp).UnsafeCall(nil, async.MakeRetT[T](types.ZeroT[T](), ErrFutureRespIncorrectType)) 61 | return nil 62 | } 63 | 64 | generic.DelegateVoid1[async.RetT[T]](resp).UnsafeCall(nil, retT) 65 | return nil 66 | } 67 | -------------------------------------------------------------------------------- /utils/concurrent/locked.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/generic" 24 | "sync" 25 | ) 26 | 27 | func MakeLocked[T any](obj T) Locked[T] { 28 | return Locked[T]{ 29 | object: obj, 30 | } 31 | } 32 | 33 | func NewLocked[T any](obj T) *Locked[T] { 34 | return &Locked[T]{ 35 | object: obj, 36 | } 37 | } 38 | 39 | type Locked[T any] struct { 40 | object T 41 | mutex sync.RWMutex 42 | } 43 | 44 | func (l *Locked[T]) AutoLock(fun generic.Action1[*T]) { 45 | l.mutex.Lock() 46 | defer l.mutex.Unlock() 47 | 48 | fun.UnsafeCall(&l.object) 49 | } 50 | -------------------------------------------------------------------------------- /utils/concurrent/rwlocked.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "git.golaxy.org/core/utils/generic" 24 | "sync" 25 | ) 26 | 27 | func MakeRWLocked[T any](obj T) RWLocked[T] { 28 | return RWLocked[T]{ 29 | object: obj, 30 | } 31 | } 32 | 33 | func NewRWLocked[T any](obj T) *RWLocked[T] { 34 | return &RWLocked[T]{ 35 | object: obj, 36 | } 37 | } 38 | 39 | type RWLocked[T any] struct { 40 | object T 41 | mutex sync.RWMutex 42 | } 43 | 44 | func (l *RWLocked[T]) AutoLock(fun generic.Action1[*T]) { 45 | l.mutex.Lock() 46 | defer l.mutex.Unlock() 47 | 48 | fun.UnsafeCall(&l.object) 49 | } 50 | 51 | func (l *RWLocked[T]) AutoRLock(fun generic.Action1[*T]) { 52 | l.mutex.RLock() 53 | defer l.mutex.RUnlock() 54 | 55 | fun.UnsafeCall(&l.object) 56 | } 57 | -------------------------------------------------------------------------------- /utils/concurrent/watcher.go: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Golaxy Distributed Service Development Framework. 3 | * 4 | * Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 2.1 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with Golaxy Distributed Service Development Framework. If not, see . 16 | * 17 | * Copyright (c) 2024 pangdogs. 18 | */ 19 | 20 | package concurrent 21 | 22 | import ( 23 | "context" 24 | "git.golaxy.org/core/utils/async" 25 | ) 26 | 27 | // IWatcher 监听器接口 28 | type IWatcher interface { 29 | context.Context 30 | Terminate() async.AsyncRet 31 | Terminated() async.AsyncRet 32 | } 33 | --------------------------------------------------------------------------------