private bool GetSyncResponse(string id, int timeoutMs, out byte[] response) { response = null; DateTime start = DateTime.Now; int iterations = 0; while (true) { PendingResponse pendingResp = null; if (_PendingResponses.ContainsKey(id)) { if (!_PendingResponses.TryGetValue(id, out pendingResp)) { return(false); } Message respMsg = pendingResp.ResponseMessage; DateTime expiration = pendingResp.Expiration; if (DateTime.Now > expiration) { return(false); } int dataLen = 0; if (respMsg.Data != null) { dataLen = respMsg.Data.Length; } response = new byte[dataLen]; if (dataLen > 0) { Buffer.BlockCopy(respMsg.Data, 0, response, 0, dataLen); } _PendingResponses.TryRemove(id, out pendingResp); return(true); } // Check if timeout exceeded TimeSpan ts = DateTime.Now - start; if (ts.TotalMilliseconds > timeoutMs) { response = null; _PendingResponses.TryRemove(id, out pendingResp); return(false); } iterations++; continue; } }
private bool GetSyncResponse(string id, int timeoutMs, out long contentLength, out Stream stream) { contentLength = 0; stream = null; DateTime start = DateTime.Now; int iterations = 0; while (true) { PendingResponse pendingResp = null; if (_PendingResponses.ContainsKey(id)) { if (!_PendingResponses.TryGetValue(id, out pendingResp)) { return(false); } Message respMsg = pendingResp.ResponseMessage; DateTime expiration = pendingResp.Expiration; if (DateTime.Now > expiration) { return(false); } contentLength = respMsg.ContentLength; stream = respMsg.DataStream; _PendingResponses.TryRemove(id, out pendingResp); return(true); } // Check if timeout exceeded TimeSpan ts = DateTime.Now - start; if (ts.TotalMilliseconds > timeoutMs) { contentLength = 0; stream = null; _PendingResponses.TryRemove(id, out pendingResp); return(false); } iterations++; continue; } }
private bool MeshServerMessageReceived(string ipPort, byte[] data) { try { Message currMsg = new Message(data, true); // Console.WriteLine(currMsg.ToString()); Peer currPeer = GetPeerByIpPort(currMsg.SourceIp, currMsg.SourcePort); if (currPeer == null || currPeer == default(Peer)) { // Console.WriteLine("Unable to find peer " + currMsg.SourceIp + ":" + currMsg.SourcePort); return(false); } if (currMsg.SyncRequest) { if (SyncMessageReceived != null) { SyncResponse syncResponse = SyncMessageReceived(currPeer, currMsg.Data); Message responseMsg = new Message(_Self.Ip, _Self.Port, currPeer.Ip, currPeer.Port, currMsg.TimeoutMs, false, true, currMsg.Type, syncResponse.Data); responseMsg.Id = currMsg.Id; MeshClient currClient = GetMeshClientByIpPort(currPeer.Ip, currPeer.Port); return(SendSyncResponseInternal(currClient, responseMsg)); } } else if (currMsg.SyncResponse) { // add to sync responses PendingResponse pendingResp = new PendingResponse(DateTime.Now.AddMilliseconds(currMsg.TimeoutMs), currMsg); _PendingResponses.TryAdd(currMsg.Id, pendingResp); return(true); } else { if (AsyncMessageReceived != null) { return(AsyncMessageReceived(currPeer, currMsg.Data)); } } return(true); } catch (Exception e) { Console.WriteLine("ClientMessageReceived exception: " + Environment.NewLine + Common.SerializeJson(e, true)); return(false); } }
private bool MeshClientStreamReceived(Peer peer, long contentLength, Stream stream) { try { Message currMsg = new Message(stream, _Settings.ReadStreamBufferSize); if (currMsg.SyncRequest) { if (SyncMessageReceived != null) { SyncResponse syncResponse = SyncStreamReceived(peer, currMsg.ContentLength, currMsg.DataStream); Console.WriteLine("ServerStreamReceived received sync response"); Message responseMsg = new Message(_Self.Ip, _Self.Port, peer.Ip, peer.Port, currMsg.TimeoutMs, false, true, currMsg.Type, syncResponse.Data); Console.WriteLine("ServerStreamReceived built message"); responseMsg.Id = currMsg.Id; MeshClient currClient = GetMeshClientByIpPort(peer.Ip, peer.Port); return(SendSyncResponseInternal(currClient, responseMsg)); } } else if (currMsg.SyncResponse) { // add to sync responses PendingResponse pendingResp = new PendingResponse(DateTime.Now.AddMilliseconds(currMsg.TimeoutMs), currMsg); _PendingResponses.TryAdd(currMsg.Id, pendingResp); } else { if (AsyncStreamReceived != null) { return(AsyncStreamReceived(peer, currMsg.ContentLength, currMsg.DataStream)); } } return(true); } catch (Exception e) { Console.WriteLine("ServerStreamReceived exception: " + Environment.NewLine + Common.SerializeJson(e, true)); return(false); } }