Example #1
0
        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);
            }
        }
Example #2
0
        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);
            }
        }