/// <summary> /// Writes a message to the IPC /// </summary> /// <param name="message"></param> public void Write(IpcMessage message) { if (disposedValue) { return; } if (useSerializer) { Write(IpcMessageSerializer.Serialize(message)); } else { Write(message.ToBytes()); } }
private void ReadCallback(IAsyncResult ar) { Stream recv = (Stream)ar.AsyncState; try { bytesIn = recv.EndRead(ar); //Make sure we read all 4 bytes do { bytesIn += recv.Read(readBuffer, bytesIn, 4 - bytesIn); } while (bytesIn < 4); int header = BitConverter.ToInt32(readBuffer, 0); //TODO - implement a better way to deal with messages larger than buffer if (header + 4 > readBuffer.Length && header < 1024 * 1024 * 50) //max 50mb { readBuffer = new byte[header + 4]; } bytesIn = recv.Read(readBuffer, 4, header); //Keep reading bytes until full packet has been read while (bytesIn != header) { bytesIn += recv.Read(readBuffer, bytesIn + 4, header - bytesIn); } byte[] packet = new byte[header]; Buffer.BlockCopy(readBuffer, 4, packet, 0, header); try { if (!useSerializer) { PreProcessMessage(packet); } else { PreProcessMessage(IpcMessageSerializer.DeSerialize(packet)); } }catch (Exception ex) { ISLogger.Write("IpcBase: An error occurred while handling message: " + ex.Message); ISLogger.Write(ex.StackTrace); Thread.Sleep(2000); Process.GetCurrentProcess().Kill(); } //Reset buffer size if changed if (readBuffer.Length != 1024) { readBuffer = new byte[1024]; } recv.BeginRead(readBuffer, 0, 4, ReadCallback, recv); } catch (ObjectDisposedException) { return; } catch (Exception ex) { OnError(ex); } }