public static MessageBase Read(MessageBase messageable, byte[] bytes) { NetworkReader reader = new NetworkReader(bytes); messageable.Deserialize(reader); return(messageable); }
public static void HandleNetworkDataEvent(byte[] dataBuffer, int dataSize) { NetworkReader reader = new NetworkReader(dataBuffer); EMessageType type = (EMessageType)reader.ReadInt16(); Debug.Log("Received message type " + (int)type); // things that change on adding a new message: // add to types, create new event, create new case to launch that event // without switching for type: // deserialize the messagebase = [type, inst] MessageBase msg = typeToInstance[type]; msg.Deserialize(reader); messageDelegates[type](msg); // deserialize and raise events based on message type //switch (type) //{ // case EMessageType.Connect: // ConnectMessage cMsg = new ConnectMessage(); // cMsg.Deserialize(reader); // // raise the event // //OnConnectMessage(cMsg); // dels[0](cMsg); // break; //} }
public async Task <DisenrollResponse> DisEnrollChild(Guid uuid) { DisenrollResponse returnModel = new DisenrollResponse(); DisenrollRequest request = new DisenrollRequest(); request.Id = Guid.NewGuid(); request.FingerprintRecord = new DsdAfis.Core.FingerBiometrics.FingerprintRecord(); request.FingerprintRecord.Id = 0; request.FingerprintRecord.Active = false; request.FingerprintRecord.DateTime = DateTime.Now; request.FingerprintRecord.Uuid = uuid; using (var client = new HttpClient()) { //Passing service base url client.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebApiUrl"]); client.DefaultRequestHeaders.Clear(); //Define request data format client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsync( "api/afis/disenroll", new StringContent(request.Json, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { return(MessageBase.Deserialize <DisenrollResponse>(response.Content.ReadAsStringAsync().Result)); } else { return(returnModel); } } }
static public IEnumerable <MessageBase> Receive(Socket client, int bytesReceived, StateObject state) { // Total buffered count is the bytes received this read // plus any unprocessed bytes from the last receive int bufferLen = bytesReceived + state.readOffset; // Reset the next read offset in the case // this recieve lands on a message boundary state.readOffset = 0; // Make sure there are bytes to read if (bufferLen >= 0) { // Initialize the current read position int readOffset = 0; // Process the receive buffer while (readOffset < bufferLen) { // Get the message size int length = BitConverter.ToInt32(state.Buffer, readOffset); bool mustDecrypt = BitConverter.ToBoolean(state.Buffer, readOffset + 4); // Increment the current read position by the length header readOffset += 8; // Change the buffer size if necessary if (length + readOffset > state.Buffer.Length) { byte[] oldBuffer = new byte[state.BufferSize]; Buffer.BlockCopy(state.Buffer, 0, oldBuffer, 0, state.BufferSize); // Array.Copy(state.Buffer, oldBuffer, state.BufferSize); state.BufferSize = length + readOffset; Buffer.BlockCopy(oldBuffer, 0, state.Buffer, 0, oldBuffer.Length); // Array.Copy(oldBuffer, state.Buffer, oldBuffer.Length); } // Ensure there are enough bytes to process the message if (readOffset + length <= bufferLen) { yield return(MessageBase.Deserialize(state.Buffer, readOffset, length, mustDecrypt)); } else { // Add back the message length readOffset -= 8; // Reorder the receive buffer so unprocessed // bytes are moved to the start of the array Buffer.BlockCopy(state.Buffer, 0, state.Buffer, 0, bufferLen - readOffset); // Array.Copy(state.Buffer, state.Buffer, bufferLen - readOffset); // Set the receive position to the current read position // This is the offset where the next socket read will start state.readOffset = bufferLen - readOffset; break; } // Update the read position by the message length readOffset += length; } } }
static public MessageBase Deserialize(byte[] buffer) { int length = BitConverter.ToInt32(buffer, 0); bool mustDecrypt = BitConverter.ToBoolean(buffer, 4); MessageBase b = null; try { b = MessageBase.Deserialize(buffer, 8, length, mustDecrypt); } catch { } return(b); }
public void HandleMessage(byte[] message, TcpClient client) { using BigEndianReader reader = new BigEndianReader(message); ushort packetId = reader.ReadUShort(); if (!_messageTypes.ContainsKey(packetId)) { Logger.Error(string.Format("Received PacketId({0}) from {1} doesn't correspond to a message type", packetId, client.EndPoint)); return; } if (!_messageHandlers.ContainsKey(packetId)) { Logger.Error(string.Format("Received PacketId({0}) from {1} not handled", packetId, client.EndPoint)); return; } MessageBase msg = Activator.CreateInstance(_messageTypes[packetId]) as MessageBase; msg.Deserialize(reader); Logger.Debug(string.Format("new packet data [{0}]{1}", packetId, msg.GetType().Name)); _messageHandlers[packetId](msg, client.ClientData); }
public override void Deserialize(NetworkReader reader) { ID = reader.ReadInt16(); CreateMessage(); Message.Deserialize(reader); }
public async Task <IdentifyResponse> IdentifyChild(FPCaptureRs vm) { IdentifyResponse returnModel = new IdentifyResponse(); IdentifyRequest request = new IdentifyRequest(); request.Id = Guid.NewGuid(); request.FingerprintRecord = new DsdAfis.Core.FingerBiometrics.FingerprintRecord(); request.FingerprintRecord.Id = 0; request.FingerprintRecord.Active = false; request.FingerprintRecord.DateTime = DateTime.Now; request.FingerprintRecord.FingerprintSet = new DsdAfis.Core.FingerBiometrics.FingerprintSet(); request.FingerprintRecord.FingerprintSet.Dpi = 500; request.FingerprintRecord.FingerprintSet.ImageEncoding = "WSQ"; request.FingerprintRecord.FingerprintSet.ImageHeight = 512; request.FingerprintRecord.FingerprintSet.ImageWidth = 512; List <Fingerprint> fingers = new List <Fingerprint>(); Fingerprint blankFinger = null; fingers.Add(blankFinger); foreach (Finger f in vm.Fingers) { if (f.Sequence > 0) { Fingerprint finger = new Fingerprint(); switch (f.Name) { case "1": finger.Code = FingerCode.RightThumb; break; case "6": finger.Code = FingerCode.LeftThumb; break; } finger.EncodedImage = f.Print; fingers.Add(finger); } else { fingers.Add(null); } } request.FingerprintRecord.FingerprintSet.Fingerprints = fingers.ToArray(); using (var client = new HttpClient()) { //Passing service base url client.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebApiUrl"]); client.DefaultRequestHeaders.Clear(); //Define request data format client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsync( "api/afis/identify", new StringContent(request.Json, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { return(MessageBase.Deserialize <IdentifyResponse>(response.Content.ReadAsStringAsync().Result)); } else { return(returnModel); } } }