public override async Task OnReceiveBinary(byte[] data, int length) { //If we're not authenticated, handle that first. Log("RECEIVE", "GOT MESSAGE of length " + length); if (!authenticated) { await HandleAuthentication(data, length); return; } //Read the opcode and index ushort opcode = BinaryTool.ReadUInt16(data, 0); ulong index = BinaryTool.ReadUInt64(data, 2); //Switch on this opcode Log("RECEIVE", "Got message with opcode " + opcode + " / index " + index); if (opcode == 1) { await HandleRPCMessage(data, length, index); } else { await WriteResponse(index, 404); } }
public byte[] OnDestroyCommand(CoreNetworkServer server, CoreNetworkOpcode opcode, byte[] payload) { //Get requested ID ushort id = BinaryTool.ReadUInt16(payload, 0); //Find requested server ID ManagerInstance instance = null; foreach (var s in Program.server_types) { foreach (var p in s.Value.instances) { if (p.settings.server_id == id) { instance = p; } } } //Check if failed if (instance == null) { return(CreateFailResponse("Could not find requested instance.")); } //End instance.RemoveInstance().GetAwaiter().GetResult(); //Return OK status return(new byte[] { 0x00 }); }
/// <summary> /// Handles an RPC message: opcode 1. This will fire events to clients by a filter /// </summary> /// <param name="data"></param> /// <param name="length"></param> /// <returns></returns> private async Task HandleRPCMessage(byte[] data, int length, ulong index) { //This follows the following format, starting at byte 10 // 4 bytes: int - Payload length "x" // x bytes: string - Payload // 2 bytes: ushort - Filter type // [Dependant on filter type] //Read the length of the payload and read the payload int payloadLength = BinaryTool.ReadInt32(data, 10); byte[] payload = BinaryTool.CopyFromArray(data, 14, payloadLength); Log("RPC-MESSAGE", "Payload length " + payloadLength); //Read the filter code ushort filterCode = BinaryTool.ReadUInt16(data, 14 + payloadLength); Log("RPC-MESSAGE", "Filter type " + filterCode); //Pack contents PackedWebSocketMessage packed = new PackedWebSocketMessage(payload, payloadLength, WebSocketMessageType.Text); //Read the filter and send messages Task pending; if (filterCode == 0) { //USER_ID - Read the user ID // 12 bytes: MongoDB ID - User ID ObjectId userId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); pending = RPCEventDispatcher.SendDataToUserById(RPCType.RPCSession, userId, packed); } else if (filterCode == 1) { //SERVER - Sends to all members of a server // 12 bytes: MongoDB ID - Server ID ObjectId serverId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); pending = RPCEventDispatcher.SendDataToServerById(RPCType.RPCSession, serverId, packed); } else if (filterCode == 2) { //SERVER_TRIBE - Sends to a tribe of a server // 12 bytes: MongoDB ID - Server ID // 4 bytes: Int - ARK Tribe ID ObjectId serverId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); int tribeId = BinaryTool.ReadInt32(data, 14 + payloadLength + 2 + 12); pending = RPCEventDispatcher.SendDataToServerTribeById(RPCType.RPCSession, serverId, tribeId, packed); } else { await WriteResponse(index, 400); return; } //Wait for sending to complete Log("RPC-MESSAGE", "Sending message....pending..."); await pending; Log("RPC-MESSAGE", "Message sent OK!"); //Send OK await WriteResponse(index, 0); }