Exemple #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            var end      = new IPEndPoint(IPAddress.Any, 0);
            var bytes    = srecvClient.EndReceive(ar, ref end);
            var callback = ar.AsyncState as Action <Protocol, bool, int>;
            var self     = false;

            if (end.Address.Equals(me))
            {
                self = true;
            }
            int from = GetIp(end.Address);

            srecvClient.BeginReceive(ReceiveCallback, callback);

            if (OnLog != null)
            {
                OnLog(ProtocolFactory.GetProtocol(bytes), end);
                return;
            }

            if (!self && host != 0 && host != from)
            {
                return;
            }

            var p = ProtocolFactory.GetProtocol(bytes);

            if (p.host != 0 && p.host != _ip && p.host != host)
            {
                return;
            }

            callback(p, self, from);
        }
Exemple #2
0
        private void LogReceiveCallback(IAsyncResult ar)
        {
            var end   = new IPEndPoint(IPAddress.Any, 0);
            var bytes = brecvClient.EndReceive(ar, ref end);

            brecvClient.BeginReceive(LogReceiveCallback, null);
            OnLog(ProtocolFactory.GetProtocol(bytes), end);
        }
Exemple #3
0
        public void Find(int type, Action <BroadcastProtocol> callback, int findTimeout = 1000)
        {
            var client = brecvClient;
            var wait   = true;
            var ia     = client.BeginReceive(ar =>
            {
                var end   = new IPEndPoint(IPAddress.Any, 0);
                var bytes = client.EndReceive(ar, ref end);
                if (OnLog != null)
                {
                    client.BeginReceive(LogReceiveCallback, null);
                    OnLog(ProtocolFactory.GetProtocol(bytes), end);
                    return;
                }

                if (!wait)
                {
                    return;
                }
                if (end.Address.Equals(me))
                {
                    Debug.WriteLine("me");
                    callback(null);
                    return;
                }

                var b = ProtocolFactory.GetProtocol <BroadcastProtocol>(bytes);
                if (b.type == type)
                {
                    callback(b);
                }
                else
                {
                    callback(null);
                }
            }, null);

            if (!ia.AsyncWaitHandle.WaitOne(findTimeout))
            {
                wait = false;
                callback(null);
            }
        }