예제 #1
0
        public SharpStarServerClient(SharpStarClient plrClient)
        {
            PlayerClient        = plrClient;
            PlayerClient.Server = this;

            _packetHandlers = new List <IPacketHandler>();
        }
예제 #2
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            SocketAsyncEventArgs readEventArgs = new SocketAsyncEventArgs();

            readEventArgs.UserToken = new AsyncUserToken();

            byte[] buffer = new byte[1024];
            readEventArgs.SetBuffer(buffer, 0, buffer.Length);

            ((AsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket;

            if (!e.AcceptSocket.Connected)
            {
                StartAccept(e);

                return;
            }

            SharpStarLogger.DefaultLogger.Info("Connection from {0}", e.AcceptSocket.RemoteEndPoint);

            Interlocked.Increment(ref m_numConnectedSockets);

            StartAccept(e);

            try
            {
                SharpStarClient client = new SharpStarClient(readEventArgs, Direction.Client);
                client.InternalClientDisconnected += PlayerClient_ClientDisconnected;

                SharpStarServerClient ssc = new SharpStarServerClient(client);
                ssc.SClientConnected += ssc_SClientConnected;
                ssc.ClientId          = m_numConnectedSockets;

                foreach (IPacketHandler packetHandler in DefaultPacketHandlers)
                {
                    ssc.RegisterPacketHandler(packetHandler);
                }

                ssc.Connect(sbServerEndPoint);
            }
            catch (Exception ex)
            {
                ex.LogError();
            }
        }
예제 #3
0
 public ClientDisconnectedEventArgs(SharpStarClient client)
 {
     Client = client;
 }
예제 #4
0
        private void connectArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success && ((AsyncUserToken)e.UserToken).Socket.Connected)
            {
                ConnectionTime = DateTime.Now;

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = e.RemoteEndPoint;
                args.UserToken      = e.UserToken;

                byte[] buffer = new byte[1024];

                args.SetBuffer(buffer, 0, buffer.Length);

                ServerClient             = new SharpStarClient(args, Direction.Server);
                ServerClient.Server      = this;
                ServerClient.OtherClient = PlayerClient;

                PlayerClient.OtherClient = ServerClient;

                if (SClientConnected != null)
                {
                    SClientConnected(this, new ClientConnectedEventArgs(ServerClient));
                }

                PlayerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);
                ServerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);
            }
            else
            {
                if (PlayerClient == null)
                {
                    return;
                }

                new SocketException((int)e.SocketError).LogError();

                //simulate the connection process so we can return an error back to the client
                var packetRecv       = Observable.FromEventPattern <PacketEventArgs>(p => PlayerClient.PacketReceived += p, p => PlayerClient.PacketReceived -= p);
                var clientConnPacket = (from p in packetRecv where p.EventArgs.Packet.PacketId == (int)KnownPacket.ClientConnect select p);
                var subscribeConn    = clientConnPacket.Subscribe(args =>
                {
                    args.EventArgs.Packet.Ignore = true;

                    SharpStarClient client = args.EventArgs.Client;

                    if (client != null)
                    {
                        client.SendPacket(new HandshakeChallengePacket
                        {
                            Claim  = String.Empty,
                            Rounds = StarboundConstants.Rounds,
                            Salt   = SharpStarSecurity.GenerateSalt()
                        }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);
                    }
                }, args => { }, () => { });

                var handshakeRespPacket = (from p in packetRecv where p.EventArgs.Packet.PacketId == (int)KnownPacket.HandshakeResponse select p);
                var subscribeHandshake  = handshakeRespPacket.Subscribe(args =>
                {
                    SharpStarClient client = args.EventArgs.Client;

                    args.EventArgs.Packet.Ignore = true;

                    if (client != null)
                    {
                        client.SendPacket(new ConnectionResponsePacket
                        {
                            Success         = false,
                            RejectionReason = SharpStarMain.Instance.Config.ConfigFile.ServerOfflineError,
                            ClientId        = 1
                        }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);
                    }
                }, args => { }, () => { });

                PlayerClient.StartReceive().ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);
                PlayerClient.SendPacket(new ProtocolVersionPacket
                {
                    ProtocolVersion = StarboundConstants.ProtocolVersion
                }).ContinueWith(t => t.Exception.LogError(), TaskContinuationOptions.OnlyOnFaulted);

                Task.Run(() =>
                {
                    Thread.Sleep(5000);

                    subscribeConn.Dispose();
                    subscribeHandshake.Dispose();
                });
            }

            e.Dispose();
        }
예제 #5
0
 public PacketEventArgs(SharpStarClient client, IPacket packet)
 {
     Client = client;
     Packet = packet;
 }