public ISystemPipeline Connect(IPEndPoint _remote) { if (peer == null) { throw new Exception("not init peer."); } //lock (this) //{ var linkid = peer.Connect(_remote); if (this.linkedIP.ContainsKey(linkid) == false) { lock (_remote)//这个锁比较粗糙 { var remotestr = _remote.ToString(); this.linkedIP[linkid] = remotestr; //主动连接成功,创建一个systemRef RefSystemRemote remote = new RefSystemRemote(this, peer, _remote, linkid, false); remote.linked = false; this.refSystems[remotestr] = remote; return(remote); } } else { var remotestr = this.linkedIP[linkid]; return(this.refSystems[remotestr]); } //} }
public PipelineRefRemote(ISystemPipeline usersystem, string userPath, RefSystemRemote remotesystem, string path) { this._usersystem = usersystem; this.userpath = userPath; this._remotesystem = remotesystem; this.path = path; }
public unsafe void OpenNetwork(PeerOption option) { if (peer != null) { throw new Exception("already have init peer."); } peer = Peer.Tcp.PeerV2.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(id); } 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) => { 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); } else { lock (endpoint) //这个锁比较粗糙,临时增加,因为有可能Connect之后执行了一半,进入这里 //后面考虑从更底层做操作 { 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); } } //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); }; }