Exemplo n.º 1
0
 public void CloseNetwork()
 {
     if (peer == null)
     {
         return;
     }
     peer.Close();
     peer = null;
 }
Exemplo n.º 2
0
        public unsafe void OpenNetwork(PeerOption option)
        {
            if (peer != null)
            {
                throw new Exception("already have init peer.");
            }
            peer = AllPet.peer.tcp.PeerV2.CreatePeer(logger);
            peer.Start(option);
            peer.OnClosed += (id) =>
            {
                if (this.linkedIP.TryRemove(id, out string _remotestr))
                {
                    if (this.refSystems.TryRemove(_remotestr, out ISystemPipeline remote))
                    {
                        (remote as RefSystemRemote).Close();
                    }
                    Console.WriteLine("close line=" + id);
                }
            };
            peer.OnLinkError += (id, err) =>
            {
                Console.WriteLine("OnLinkError line=" + id + " ,err=" + err.ToString());
                //var remotestr = linkedIP[id];
                //if (this.refSystems.TryRemove(remotestr, out ISystemPipeline remote))
                //{
                //    (remote as RefSystemRemote).Close(); ;
                //}
            };
            peer.OnRecv += (id, data) =>
            {
                //if (data.Length == 0)
                //{
                //    throw new Exception("err h01");
                //}
                int    seek    = 0;
                var    fromlen = data[seek]; seek++;
                string from    = System.Text.Encoding.UTF8.GetString(data, seek, fromlen); seek += fromlen;
                var    tolen   = data[seek]; seek++;
                string to      = System.Text.Encoding.UTF8.GetString(data, seek, tolen); seek += tolen;
                //if (from == "" || to =="")
                //{
                //    throw new Exception("err h02");
                //}
                var remotestr = linkedIP[id];
                var refsys    = this.refSystems[remotestr];
                var pipe      = this.GetPipelineByFrom(refsys, "@" + id + "/" + from, "this/" + to) as PipelineRefLocal;
                //var pipe = this.GetPipeline(user, "this/" + to);
                var outbytes = new byte[data.Length - seek];
                fixed(byte *pdata = data, pout = outbytes)
                {
                    Buffer.MemoryCopy(pdata + seek, pout, outbytes.Length, outbytes.Length);
                }

                pipe.TellDirect(outbytes);
            };

            peer.OnAccepted += (ulong id, IPEndPoint endpoint) =>
            {
                var remotestr = endpoint.ToString();
                linkedIP[id] = remotestr;
                RefSystemRemote remote = new RefSystemRemote(this, peer, endpoint, id, true);
                //remote.linked = true;
                (remote as RefSystemRemote).Linked(id, true, endpoint);
                this.refSystems[remotestr] = remote;

                Console.WriteLine("on accepted." + id + " = " + endpoint);
            };

            peer.OnConnected += (ulong id, IPEndPoint endpoint) =>
            {
                //lock (endpoint)//这个锁比较粗糙,临时增加,因为有可能Connect之后执行了一半,进入这里
                //               //后面考虑从更底层做操作
                //{
                //    if (this.linkedIP.ContainsKey(id) == false)
                //    {

                //        var __remotestr = endpoint.ToString();
                //        this.linkedIP[id] = __remotestr;
                //        RefSystemRemote __remote = new RefSystemRemote(this, peer, endpoint, id, false);
                //        __remote.linked = true;
                //        this.refSystems[__remotestr] = __remote;
                //        __remote.Linked(id, false, endpoint);
                //        logger.Info("==link now== systemid" + id);

                //    }
                //    else
                {
                    var remotestr = this.linkedIP[id];
                    if (this.refSystems.ContainsKey(remotestr) == false)
                    {
                        logger.Warn("意外的值");
                    }
                    var remote = this.refSystems[remotestr] as RefSystemRemote;
                    //remote.linked = true;
                    (remote as RefSystemRemote).Linked(id, false, endpoint);
                    logger.Info("==link delay== systemid" + id);
                }
                //}
                //this.linkedIP[id] = endpoint.ToString();

                ////主动连接成功,创建一个systemRef
                //var remotestr = this.linkedIP[id];
                //RefSystemRemote remote = new RefSystemRemote(peer, remotestr, id);
                //remote.linked = true;
                //this.refSystems[remotestr] = remote;
                Console.WriteLine("on OnConnected." + id + " = " + endpoint);
            };
        }