示例#1
0
 /// <summary>
 /// Initializes the server.
 /// Only use this if you've set a main channel with <see cref="UNetServer(IServerChannel)"/>
 /// </summary>
 /// <remarks>
 /// May generate a <see cref="ServerInitializationException"/> if used incorrectly
 /// </remarks>
 public void Initialize()
 {
     if (_channelMgr.GetChannel <IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel) == null)
     {
         throw new ServerInitializationException("Attempt to initialize server without a main channel");
     }
     _channelMgr.GetChannel <IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel)
     .Start();
     _channelMgr.GetChannel <IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel)
     .OnPeerConnected +=
         (sender, e) => OnPeerConnected.Raise(sender, e);
 }
示例#2
0
 /// <summary>
 /// Adds a channel to this server
 /// </summary>
 /// <remarks>
 /// May throw a <see cref="ChannelOperationException"/> if used incorrectly
 /// </remarks>
 /// <param name="channel">The channel to add</param>
 /// <returns>Returns true if channel was added successfully</returns>
 public bool AddChannel(IServerChannel channel)
 {
     if (!_channelMgr.AddChannel(channel))
     {
         return(false);
     }
     OnChannelCreated.Raise(this, new ChannelEventArgs(channel));
     channel.HostServer       = this;
     channel.OnPeerConnected += (sender, e) => OnPeerConnected.Raise(this, e);
     channel.PacketProcessor  = _channelMgr.GetMainChannel().PacketProcessor;
     return(true);
 }
示例#3
0
 /// <summary>
 /// Initializes the server.
 /// Only use this if you haven't set a main channel with <see cref="UNetServer()"/>
 /// </summary>
 /// <param name="mainChannel">The server main channel to use</param>
 public void Initialize(IServerChannel mainChannel)
 {
     if (_channelMgr.GetChannel <IServerChannel>(ch => ch.IsMainChannel) != null)
     {
         return;
     }
     mainChannel.IsMainChannel = true;
     mainChannel.HostServer    = this;
     _channelMgr.UnsafeAddChannel(mainChannel);
     _channelMgr.GetChannel <IServerChannel>(mainChannel.Id).Start();
     _channelMgr.GetChannel <IServerChannel>(mainChannel.Id).OnPeerConnected +=
         (sender, e) => OnPeerConnected.Raise(sender, e);
 }
示例#4
0
 private void AcceptPeer(Peer.Peer newPeer)
 {
     newPeer.OnPeerSynchronized +=
         (sender, e) => OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer));
     newPeer.OnPeerDisconnected +=
         (sender, e) =>
     {
         e.Peer.Dispose();
         lock (_lockObj)
             ConnectedPeers.Remove(e.Peer);
     };
     newPeer.OnRawPacketReceived        += RawPacketReceived;
     newPeer.OnSequenceFragmentReceived += (sender, e) => OnSequenceFragmentReceived.Raise(sender, e);
     lock (_lockObj)
         ConnectedPeers.Add(newPeer);
 }
示例#5
0
        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);
        }