public Message SendMessage(Message msg) { byte[] body = msg.GetBody(); int msgIndex = GetMessageIndex(); EventWaitHandle waiter = new ManualResetEvent(false); // Have to add waithandle before we send data, incase the response comes back before we add it. lock (waitHandles) { waitHandles.Add(msgIndex, waiter); } lock (stream) { writer.Write((int)msg.Type); writer.Write(msgIndex); writer.Write(body.Length); writer.Flush(); stream.Write(body, 0, body.Length); } waiter.WaitOne(); lock (waitHandles) { waitHandles.Remove(msgIndex); } lock (responses) { if (responses.ContainsKey(msgIndex)) { Message answer = responses[msgIndex]; responses.Remove(msgIndex); return answer; } else throw new IOException("Send failed."); } }
public void PostResponse(Message msg, int messageNumber) { byte[] body = msg.GetBody(); lock (stream) { writer.Write((int)msg.Type); writer.Write(-messageNumber); writer.Write(body.Length); writer.Flush(); stream.Write(body, 0, body.Length); } }