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
        public void Notify <T>(T data)
        {
            ssendClient.Connect(IPAddress.Broadcast, serverPort);

            var buf = ProtocolFactory.CreateProtocol(data, host == 0 ? _ip : host);

            ssendClient.Send(buf, buf.Length);
        }
Exemple #3
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 #4
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);
            }
        }
Exemple #5
0
        public static byte[] CreateProtocol <T>(T data, int host)
        {
            var attrs = typeof(T).GetCustomAttributes(typeof(ProtocolIdAttribute), false);
            var pid   = attrs[0] as ProtocolIdAttribute;

            var p = ProtocolFactory.CreateProtocol(pid.id, host);

            using (var stream = new MemoryStream())
            {
                stream.Position = 0;
                ProtoBuf.Serializer.Serialize(stream, data);
                stream.Flush();
                p.data = stream.ToArray();

                stream.Seek(0, SeekOrigin.Begin);
                ProtoBuf.Serializer.Serialize(stream, p);
                stream.Flush();

                return(stream.ToArray());
            }
        }
Exemple #6
0
        public void Host(int type)
        {
            if (this.host != 0)
            {
                return;
            }

            this.type = type;

            var client = bsendClient;

            client.Connect(IPAddress.Broadcast, broadcastPort);

            BroadcastProtocol data = new BroadcastProtocol();

            data.ip   = _ip;
            data.port = serverPort;
            data.type = type;

            var buf = ProtocolFactory.CreateProtocol(data, 0);

            client.Send(buf, buf.Length);
        }