private void IdentifyMessageType(byte[] payload) { if (_identifiedMessageType) { return; } try { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload); _messageType = ChoUDPMessageType.Binary; } catch { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload)); _messageType = ChoUDPMessageType.Xml; } catch { Trace.WriteLine("Failed to identity message type. Messages will be discarded."); } } } finally { _identifiedMessageType = true; } }
private void ReceiveCallback(IAsyncResult ar) { try { Byte[] payload = _serverSocket.EndReceive(ar, ref _localEP); IdentifyMessageType(payload); if (_messageType == null || _messageType.Value == ChoNetMessageType.Raw) { EventHandler <ChoNetMessageEventArgs> rawMessageReceived = RawMessageReceived; if (rawMessageReceived != null) { rawMessageReceived(this, new ChoNetMessageEventArgs(null, payload)); } } else if (_messageType.Value == ChoNetMessageType.Xml) { try { ChoNetMessage msg = (ChoNetMessage)_encoding.GetString(payload).ToObjectFromXml(); // ChoObject.XmlDeserialize<ChoNetMessage>(_encoding.GetString(payload)); if (!PreProcessMessage(msg)) { EventHandler <ChoNetMessageEventArgs> messageReceived = MessageReceived; if (messageReceived != null) { messageReceived(this, new ChoNetMessageEventArgs(msg, null)); } } } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } else if (_messageType.Value == ChoNetMessageType.Binary) { try { ChoNetMessage msg = (ChoNetMessage)ChoObject.Deserialize(payload); if (!PreProcessMessage(msg)) { EventHandler <ChoNetMessageEventArgs> messageReceived = MessageReceived; if (messageReceived != null) { messageReceived(this, new ChoNetMessageEventArgs(msg, null)); } } } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } } finally { _recieveResult = _serverSocket.BeginReceive(ReceiveCallback, null); } }
private void ReceiveCallback(IAsyncResult ar) { IPEndPoint ep = null; var args = (object[])ar.AsyncState; var session = (UdpClient)args[0]; var local = (IPEndPoint)args[1]; try { Byte[] payload = session.EndReceive(ar, ref ep); EventHandler <ChoUDPMessageEventArgs> rawMessageReceived = RawMessageReceived; EventHandler <ChoUDPMessageEventArgs> messageReceived = MessageReceived; if (rawMessageReceived != null) { rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload)); } else if (messageReceived != null) { IdentifyMessageType(payload); if (_messageType == null) { if (rawMessageReceived != null) { rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload)); } } else if (_messageType.Value == ChoUDPMessageType.Xml) { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload)); MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null)); } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } else { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload); MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null)); } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } } } finally { _recieveResult = session.BeginReceive(ReceiveCallback, args); } }
public object Deserialize(byte[] payload) { if (payload == null) { return(null); } else { return(ChoObject.Deserialize(payload)); } }
private void ReceiveCallback(IAsyncResult ar) { try { Byte[] payload = _listener.EndReceive(ar, ref _groupEP); EventHandler <ChoUDPMessageEventArgs> rawMessageReceived = RawMessageReceived; EventHandler <ChoUDPMessageEventArgs> messageReceived = MessageReceived; if (rawMessageReceived != null) { rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload)); } else if (messageReceived != null) { IdentifyMessageType(payload); if (_messageType == null) { if (rawMessageReceived != null) { rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload)); } } else if (_messageType.Value == ChoUDPMessageType.Xml) { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload)); MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null)); } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } else { try { ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload); MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null)); } catch (Exception ex) { ChoTrace.Error(ex.ToString()); } } } } finally { _recieveResult = _listener.BeginReceive(ReceiveCallback, null); } }
internal virtual object GetElement(int index) { if (index < _internalQueue.Count) { return(_commonQueue[index]); } if (index - _internalQueue.Count < _persistanceQueue.Count) { return(ChoObject.Deserialize(Path.Combine(_storageDir, _commonQueue[index] as string))); } return(null); }
public T GetData() { try { if (!_event.WaitOne(5000)) { return(default(T)); } using (MemoryMappedViewStream stream = mf.CreateViewStream(0, 0, MemoryMappedFileAccess.Read)) { if (_msgFormatter == ChoMsgFormatter.Binary) { return(ChoObject.Deserialize <T>(stream)); } else { return(ChoObject.XmlDeserialize <T>(stream)); } } } catch (FileNotFoundException) { return(default(T)); } catch (WaitHandleCannotBeOpenedException) { return(default(T)); } catch (SerializationException) { return(default(T)); } catch (AbandonedMutexException) { return(default(T)); } }
public virtual object Dequeue() { if (_internalQueue.Count == 1) { if (_persistanceQueue.Count > 0) { int index = 0; while (index < _capacity) { if (_persistanceQueue.Count == 0) { break; } string fileName = _persistanceQueue.Dequeue() as string; _internalQueue.Enqueue(ChoObject.Deserialize(Path.Combine(_storageDir, fileName))); File.Delete(Path.Combine(_storageDir, fileName)); index++; } } } _commonQueue.RemoveAt(0); return(_internalQueue.Dequeue()); }
private void IdentifyMessageType(byte[] payload) { if (_identifiedMessageType) { return; } try { if (_messageType == null) { _messageType = ChoNetMessageType.Raw; try { ChoNetMessage scalarObject = (ChoNetMessage)ChoObject.Deserialize(payload); _messageType = ChoNetMessageType.Binary; } catch { try { ChoNetMessage scalarObject = (ChoNetMessage)(ChoNetMessage)_encoding.GetString(payload).ToObjectFromXml(); // ChoObject.XmlDeserialize<ChoNetMessage>(_encoding.GetString(payload)); _messageType = ChoNetMessageType.Xml; } catch { Trace.WriteLine("Failed to identity message type. Setting the message format as raw payload mode."); } } } } finally { _identifiedMessageType = true; } }