private void AcceptCallback(IAsyncResult res)
        {
            if (!IsActive)
            {
                return;
            }
            var newSock = ChannelSocket.EndAccept(res);

            Peer.Peer newPeer;
            if (IsMainChannel)
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId(), SocketIdentity.GenerateGuid()), newSock, BufferSize, this);
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                AcceptPeer(newPeer);
            }
            else
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId()), newSock, BufferSize, this);
                PendingConnections.Add(new PendingPeerConnection(newPeer.Identity.Guid, newPeer));
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                newPeer.OnPeerRelocationRequest += (sender, e) =>
                {
                    if (PendingConnections.FirstOrDefault(pc => pc.Guid == e.PeerGuid && !pc.IsCancelled) != null)
                    {
                        AcceptPeer(newPeer);
                        OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer));

                        lock (_lockObj)
                            PendingConnections.Remove(PendingConnections.First(pc => pc.Guid == e.PeerGuid));
                    }
                };
            }
            ChannelSocket.BeginAccept(AcceptCallback, null);
        }
Exemple #2
0
        //TODO: optimize this
        public ISequencePacket[] CreateSequence(IDataPacket packet, byte[] completeBuff, int buffSize,
                                                out SequenceInitPacket initPacket, out Guid seqGuid)
        {
            //  if (!IsSequenceRequired(buffSize, completeBuff.Length))
            //TODO: do proper shit..
            // throw new Exception();

            var modRest    = completeBuff.Length % (buffSize - 33);
            var outList    = CreateSequence(completeBuff, buffSize - 33, modRest, completeBuff.Length).ToArray();
            var curSeqGuid = seqGuid = SocketIdentity.GenerateGuid();

            initPacket = new SequenceInitPacket
            {
                PartsCount       = outList.Length,
                SequenceGuid     = seqGuid,
                FullSequenceSize = completeBuff.Length
            };
            for (var i = 0; i < outList.Length; i++)
            {
                outList[i].SeqGuid = curSeqGuid;
            }
            return(outList.ToArray());
        }
 protected SocketOperationBase()
 {
     OperationGuid = SocketIdentity.GenerateGuid();
 }