Files
esengine/packages/framework/server/src/distributed/adapters/IDistributedAdapter.ts

258 lines
7.3 KiB
TypeScript
Raw Normal View History

feat(server): add Schema validation system and binary encoding optimization (#421) * feat(server): add distributed room support - Add DistributedRoomManager for multi-server room management - Add MemoryAdapter for testing and standalone mode - Add RedisAdapter for production multi-server deployments - Add LoadBalancedRouter with 5 load balancing strategies - Add distributed config option to createServer - Add $redirect message for cross-server player redirection - Add failover mechanism for automatic room recovery - Add room:migrated and server:draining event types - Update documentation (zh/en) * feat(server): add Schema validation system and binary encoding optimization ## Schema Validation System - Add lightweight schema validation system (s.object, s.string, s.number, etc.) - Support auto type inference with Infer<> generic - Integrate schema validation into API/message handlers - Add defineApiWithSchema and defineMsgWithSchema helpers ## Binary Encoding Optimization - Add native WebSocket binary frame support via sendBinary() - Add PacketType.Binary for efficient binary data transmission - Optimize ECSRoom.broadcastBinary() to use native binary ## Architecture Improvements - Extract BaseValidator to separate file to eliminate code duplication - Add ECSRoom export to main index.ts for better discoverability - Add Core.worldManager initialization check in ECSRoom constructor - Remove deprecated validate field from ApiDefinition (use schema instead) ## Documentation - Add Schema validation documentation in Chinese and English * fix(rpc): resolve ESLint warnings with proper types - Replace `any` with proper WebSocket type in connection.ts - Add IncomingMessage type for request handling in index.ts - Use Record<string, Handler> pattern instead of `any` casting - Replace `any` with `unknown` in ProtocolDef and type inference
2026-01-02 17:18:13 +08:00
/**
* @zh
* @en Distributed adapter interface
*
* @zh
* @en Defines the storage and communication layer abstraction for distributed room system
*/
import type {
ServerRegistration,
RoomRegistration,
RoomQuery,
RoomSnapshot,
DistributedEvent,
DistributedEventType,
DistributedEventHandler,
Unsubscribe
} from '../types.js';
/**
* @zh
* @en Distributed adapter interface
*
* @zh Redis
* @en All distributed backends (Redis, message queue, etc.) must implement this interface
*/
export interface IDistributedAdapter {
// =========================================================================
// 生命周期 | Lifecycle
// =========================================================================
/**
* @zh
* @en Connect to distributed backend
*/
connect(): Promise<void>;
/**
* @zh
* @en Disconnect from backend
*/
disconnect(): Promise<void>;
/**
* @zh
* @en Check if connected
*/
isConnected(): boolean;
// =========================================================================
// 服务器注册 | Server Registry
// =========================================================================
/**
* @zh
* @en Register server
*
* @param server - | Server registration info
*/
registerServer(server: ServerRegistration): Promise<void>;
/**
* @zh
* @en Unregister server
*
* @param serverId - ID | Server ID
*/
unregisterServer(serverId: string): Promise<void>;
/**
* @zh
* @en Update server heartbeat
*
* @param serverId - ID | Server ID
*/
heartbeat(serverId: string): Promise<void>;
/**
* @zh 线
* @en Get all online servers
*/
getServers(): Promise<ServerRegistration[]>;
/**
* @zh
* @en Get specific server
*
* @param serverId - ID | Server ID
*/
getServer(serverId: string): Promise<ServerRegistration | null>;
/**
* @zh
* @en Update server info
*
* @param serverId - ID | Server ID
* @param updates - | Updates
*/
updateServer(serverId: string, updates: Partial<ServerRegistration>): Promise<void>;
// =========================================================================
// 房间注册 | Room Registry
// =========================================================================
/**
* @zh
* @en Register room
*
* @param room - | Room registration info
*/
registerRoom(room: RoomRegistration): Promise<void>;
/**
* @zh
* @en Unregister room
*
* @param roomId - ID | Room ID
*/
unregisterRoom(roomId: string): Promise<void>;
/**
* @zh
* @en Update room info
*
* @param roomId - ID | Room ID
* @param updates - | Updates
*/
updateRoom(roomId: string, updates: Partial<RoomRegistration>): Promise<void>;
/**
* @zh
* @en Get room info
*
* @param roomId - ID | Room ID
*/
getRoom(roomId: string): Promise<RoomRegistration | null>;
/**
* @zh
* @en Query room list
*
* @param query - | Query criteria
*/
queryRooms(query: RoomQuery): Promise<RoomRegistration[]>;
/**
* @zh joinOrCreate
* @en Get available room of type (for joinOrCreate)
*
* @param roomType - | Room type
*/
findAvailableRoom(roomType: string): Promise<RoomRegistration | null>;
/**
* @zh
* @en Get all rooms of a server
*
* @param serverId - ID | Server ID
*/
getRoomsByServer(serverId: string): Promise<RoomRegistration[]>;
// =========================================================================
// 房间状态 | Room State
// =========================================================================
/**
* @zh
* @en Save room state snapshot
*
* @param snapshot - | State snapshot
*/
saveSnapshot(snapshot: RoomSnapshot): Promise<void>;
/**
* @zh
* @en Load room state snapshot
*
* @param roomId - ID | Room ID
*/
loadSnapshot(roomId: string): Promise<RoomSnapshot | null>;
/**
* @zh
* @en Delete room state
*
* @param roomId - ID | Room ID
*/
deleteSnapshot(roomId: string): Promise<void>;
// =========================================================================
// 发布/订阅 | Pub/Sub
// =========================================================================
/**
* @zh
* @en Publish event
*
* @param event - | Distributed event
*/
publish(event: DistributedEvent): Promise<void>;
/**
* @zh
* @en Subscribe to events
*
* @param pattern - '*' | Event type pattern (supports '*' wildcard)
* @param handler - | Event handler
* @returns | Unsubscribe function
*/
subscribe(
pattern: DistributedEventType | '*',
handler: DistributedEventHandler
): Promise<Unsubscribe>;
/**
* @zh
* @en Send message to specific room (cross-server)
*
* @param roomId - ID | Room ID
* @param messageType - | Message type
* @param data - | Message data
* @param playerId - ID | Sender player ID (optional)
*/
sendToRoom(roomId: string, messageType: string, data: unknown, playerId?: string): Promise<void>;
// =========================================================================
// 分布式锁 | Distributed Lock
// =========================================================================
/**
* @zh
* @en Acquire distributed lock
*
* @param key - | Lock key
* @param ttlMs - | Lock TTL (ms)
* @returns | Whether lock was acquired
*/
acquireLock(key: string, ttlMs: number): Promise<boolean>;
/**
* @zh
* @en Release distributed lock
*
* @param key - | Lock key
*/
releaseLock(key: string): Promise<void>;
/**
* @zh
* @en Extend lock TTL
*
* @param key - | Lock key
* @param ttlMs - | New TTL (ms)
* @returns | Whether extension was successful
*/
extendLock(key: string, ttlMs: number): Promise<boolean>;
}