Exemplo n.º 1
0
        private void HandleAction(SocketMessage sm, Socket argClient)
        {
            //Before we connect we request an id to the master server...
            if (sm.msg is SocketCommand)
            {
                SocketCommand cmd = sm.TryGetObject <SocketCommand>();
                if (cmd != null)
                {
                    switch (cmd.Command)
                    {
                    case SocketCommands.CreateConnId:
                        myLogger.Log("Starting new CLIENT connection with ID: {0}", sm.id);
                        Id = sm.id;

                        Send(SocketManager.ConfirmConnId(Id));     //???
                        OnConnectedCallback?.Invoke();
                        break;

                    case SocketCommands.CloseInstance:
                        myLogger.Log("Client is closing connection...");
                        Stop(false);
                        break;

                    default:
                        myLogger.Log("Unknown ClientCallbackion to take! Case: {0}", cmd);
                        break;
                    }
                }
                else
                {
                    myLogger.Log("Empty string received by client!");
                }
            }
            else
            {
                ReceivedServerMessageCallback?.Invoke(sm.msg, argClient);
            }
        }
Exemplo n.º 2
0
        private void HandleAction(SocketMessage sm, Socket handler)
        {
            //string val = sm.StringValue;
            if (sm.msg is SocketCommand)
            {
                SocketCommand cmd = sm.TryGetObject <SocketCommand>();
                if (cmd != null)
                {
                    switch (cmd.Command)
                    {
                    case SocketCommands.Conn:
                        //Para que algo se añade aqui debe ocurrir algo...
                        //Give an id for a client before we add it to the routing table
                        //and create a request id for the next action that needs it

                        //First, we have to assure that there are free id on the current KeyValuePair to optimize the process...
                        ulong genID = 1;

                        //Give id in a range...
                        bool b = routingTable.Keys.FindFirstMissingNumberFromSequenceUlong(out genID, new MinMax <ulong>(1, (ulong)routingTable.Count));
                        myLogger.Log("Adding #{0} client to routing table!", genID);     //Esto ni parece funcionar bien

                        SendToClient(SocketManager.SendConnId(genID), handler);
                        break;

                    case SocketCommands.ConfirmConnId:
                        routingTable.Add(sm.id, handler);
                        break;

                    case SocketCommands.CloseClients:
                        CloseAllClients(sm.id);
                        break;

                    case SocketCommands.ClosedClient:
                        //closedClients.Add(sm.id);
                        SocketManager.PoliteClose(sm.id);     //Tell to the client that it has been disconnected from it
                        routingTable.Remove(sm.id);
                        CloseServerAfterClientsClose(dispose);
                        break;

                    case SocketCommands.Stop:
                        CloseAllClients(sm.id);
                        break;

                    case SocketCommands.UnpoliteStop:
                        object d = cmd.Metadata["Dispose"];
                        Stop(d != null && ((bool)d));
                        break;

                    default:
                        DoServerError(string.Format("Cannot de-encrypt the message! Unrecognized 'enum' case: {0}", cmd.Command), sm.id);
                        break;
                    }
                }
            }
            else
            {
                //If not is a command, then send the object to other clients...
                SendToClient(sm, sm.DestsId);
            }
        }