Esempio n. 1
0
        /// <summary>
        /// Method called when a asynchronous sending  is complete.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">SocketAsyncEventArgs used to perform the sending stuff.</param>
        protected internal void OnSendingOutgoingDataComplete(object sender, SocketAsyncEventArgs e)
        {
            ServerSideClient networkEntitySender = (ServerSideClient)e.UserToken;

            if (e.SocketError == SocketError.Success)
            {
                if (e.BytesTransferred > 0)
                {
                    networkEntitySender.MessageSent(networkEntitySender, null);
                }
            }

            ///Checking if the SocketAsyncEventArgs pool has not been released and set to null.
            ///If the situtation mentioned above we have to dispose the SocketAsyncEventArgs by hand.
            if (networkEntitySender.TCPOutSocketAsyncEventArgsPool == null)
            {
                e.Dispose();
                e = null;
            }
            else
            {
                ///Recycling the SocketAsyncEventArgs used by this process.
                networkEntitySender.TCPOutSocketAsyncEventArgsPool.Recycle(e);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Creates a ServerSideCliente object from a NetworkEntity reference and then disclose the network entity.
 /// </summary>
 /// <param name="baseNetworkEntity">Reference (ref) to the NetwrokEntity used as a base to create the new ServerSideClient object.</param>
 /// <param name="ssClient">New server side clint out reference.</param>
 /// <returns></returns>
 public static Error.ErrorType CreateFromNetworkEntity(NetworkEntity baseNetworkEntity, out ServerSideClient ssClient)
 {
     ssClient = null;
     if (baseNetworkEntity == null)
     {
         return(Error.ErrorType.InvalidNetworkEntity);
     }
     ssClient = new ServerSideClient();
     baseNetworkEntity.ownerNetworkCollection.Clone(out ssClient.ownerNetworkCollection);
     ssClient.id = baseNetworkEntity.Id;
     baseNetworkEntity.Dispose();
     ssClient.InitializeUDPConnection();
     return(Error.ErrorType.Ok);
 }
Esempio n. 3
0
        /// <summary>
        /// Handles the commands passed by the UI or the console if is it one implemented.
        /// </summary>
        protected void HandleLocalCommandsThreadMethod()
        {
            Message          messageToProcess          = null;
            Message          responseMessage           = null;
            ManagedMessage   managedMessageReference   = null;
            ServerSideClient newClientAttempt          = null;
            ServerSideClient serverSideClientReference = null;
            GameUser         referredUser = null;

            if (!this.ableToRun)
            {
                KSPMGlobals.Globals.Log.WriteTo(Error.ErrorType.ServerUnableToRun.ToString());
            }
            try
            {
                KSPMGlobals.Globals.Log.WriteTo("-Starting to handle local commands[ " + this.alive + " ]");
                while (this.alive)
                {
                    this.localCommandsQueue.DequeueCommandMessage(out messageToProcess);
                    if (messageToProcess != null)
                    {
                        //KSPMGlobals.Globals.Log.WriteTo(messageToProcess.Command.ToString());
                        managedMessageReference = (ManagedMessage)messageToProcess;
                        switch (messageToProcess.Command)
                        {
                        case Message.CommandType.NewClient:
                            if (this.clientsHandler.ConnectedClients < this.lowLevelOperationSettings.maxConnectedClients)
                            {
                                if (this.defaultUserManagementSystem.Query(managedMessageReference.OwnerNetworkEntity))
                                {
                                    if (ServerSideClient.CreateFromNetworkEntity(managedMessageReference.OwnerNetworkEntity, out newClientAttempt) == Error.ErrorType.Ok)
                                    {
                                        newClientAttempt.RegisterUserConnectedEvent(this.UserConnected);
                                        if (newClientAttempt.StartClient())
                                        {
                                            this.clientsHandler.AddNewClient(newClientAttempt);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //Creates the reject message and set the callback to the RejectMessageToClient. Performed in that way because it is needed to send the reject message before to proceed to disconnect the client.
                                Message.ServerFullMessage(managedMessageReference.OwnerNetworkEntity, out responseMessage);
                                PacketHandler.EncodeRawPacket(ref responseMessage.bodyMessage);
                                ((ManagedMessage)responseMessage).OwnerNetworkEntity.SetMessageSentCallback(this.RejectMessageToClient);
                                this.priorityOutgoingMessagesQueue.EnqueueCommandMessage(ref responseMessage);
                            }
                            break;

                        case Message.CommandType.StopServer:
                            this.ShutdownServer();
                            break;

                        case Message.CommandType.Authentication:
                            User.InflateUserFromBytes(messageToProcess.bodyMessage, ((BufferedMessage)messageToProcess).StartsAt, messageToProcess.MessageBytesSize, out referredUser);
                            serverSideClientReference          = (ServerSideClient)managedMessageReference.OwnerNetworkEntity;
                            serverSideClientReference.gameUser = referredUser;
                            if (this.usersAccountManager.Query(managedMessageReference.OwnerNetworkEntity))
                            {
                                /*
                                 * Message.AuthenticationSuccessMessage(messageOwner, out responseMessage);
                                 * this.outgoingMessagesQueue.EnqueueCommandMessage(ref responseMessage);
                                 */
                                serverSideClientReference.RemoveAwaitingState(ServerSideClient.ClientStatus.Authenticated);
                            }
                            else
                            {
                                ///Need to improve this code for a only one if.
                                ///And to check if is it needed to send a disconnect message before release the socket.
                                if ((serverSideClientReference.gameUser.AuthencticationAttempts++) < this.lowLevelOperationSettings.maxAuthenticationAttempts)
                                {
                                    ///There is still a chance to authenticate again.
                                    Message.AuthenticationFailMessage(managedMessageReference.OwnerNetworkEntity, out responseMessage);
                                    PacketHandler.EncodeRawPacket(ref responseMessage.bodyMessage);
                                }
                                else
                                {
                                    ///There is no chance to try it again.
                                    ((ManagedMessage)responseMessage).OwnerNetworkEntity.SetMessageSentCallback(this.RejectMessageToClient);
                                }
                                this.priorityOutgoingMessagesQueue.EnqueueCommandMessage(ref responseMessage);
                            }
                            break;

                        case Message.CommandType.Disconnect:
                            ///Disconnects either a NetworkEntity or a ServerSideClient.
                            this.DisconnectClient(managedMessageReference.OwnerNetworkEntity);
                            break;

                        case Message.CommandType.Unknown:
                        default:
                            KSPMGlobals.Globals.Log.WriteTo("Unknown command: " + messageToProcess.Command.ToString());
                            break;
                        }

                        ///Recyles and releases the message.
                        this.priorityMessagesPool.Recycle(messageToProcess);
                    }

                    Thread.Sleep(1);
                }
            }
            catch (ThreadAbortException)
            {
                this.alive = false;
            }
        }