コード例 #1
0
    void HandlePlayer(int id)
    {
        SGCommand playerCommand = m_connection.ReceiveStream(id);

        if (playerCommand != null)
        {
            // Execute the command
            if (playerCommand.GetID() == SGCommandID.REFRESH_TIMEOUT)
            {
                m_playerTimeouts[id] = CONNECTION_TIMEOUT_LOOPS;
            }
            else
            {
                playerCommand.ExecuteCommand(m_cgManager);
                m_playerTimeouts[id]--;
            }
        }
        else
        {
            // Ask player for a no-timeout packet
            CGC_RefreshTimeout timeout = new CGC_RefreshTimeout(id);
            m_connection.TransmitStream(timeout.PackCommand(), id);
            m_playerTimeouts[id]--;
        }
    }
コード例 #2
0
        protected override RESULT Process(StringBuilder messager)
        {
            if (Connected != CONNECTSTATUS.CONNECTED)
            {
                Connect();
            }
            else
            {
                switch (LoginStatus)
                {
                case LOGINSTATUS.LOGOUT:
                    Login();
                    break;

                case LOGINSTATUS.LOGINING:
                    break;

                case LOGINSTATUS.LOGIN:
                    //1.Add/Remove Command
                    SGCommand command = null;
                    if (CommandQueue.TryDequeue(out command) && command != null)
                    {
                        if (!command.Continuous)
                        {
                            ProcessCommand(command);
                        }
                        else
                        {
                            if (command.Continuous && !ContinuousCommands.Any(cmd => cmd.Command == command.Command && cmd.RequestorID.Equals(command.RequestorID)))
                            {
                                ContinuousCommands.Add(command);
                            }
                        }
                    }
                    //2.Process Continuous Commands
                    foreach (SGCommand cmd in ContinuousCommands)
                    {
                        ProcessCommand(cmd);
                    }

                    //3.Send Message to Server
                    var list = this.GetData <SGCommand>();
                    if (list.Count > 0)
                    {
                        foreach (SGCommand cmd in list)
                        {
                            SendCommand(MakeMSGCommand(cmd.RequestorID, cmd.Message));
                        }
                    }
                    break;
                }
            }

            return(RESULT.OK);
        }
コード例 #3
0
        private void Proc_RTM(SGCommand command)
        {
            var code = (command.Parameters.ContainsKey("code")) ? command.Parameters["code"].ToString() : null;

            if (StockInfoEntity.IsValidCode(code))
            {
                var plug = this.FindPlug <PlugIn>(code);
                if (plug != null && plug.DataCount <= 0)
                {
                    this.PutData <SGCommand>(code, command);
                }
            }
        }
コード例 #4
0
        private void OnMessage(object sender, MessageEventArgs e)
        {
            var command = Commander.Deserialize(e.Data);

            if (command != null)
            {
                switch (command.COMMAND)
                {
                case COMMAND.KEY:
                    key = command.Requestor.ID;
                    Commander.Initial(key);
                    this.logger.Write(Log.common.enums.TYPE.INFO, string.Format("key:{0}", key));
                    Login();
                    Connected = CONNECTSTATUS.CONNECTED;
                    break;

                case COMMAND.LOGIN:
                    var status = Convert.ToInt32(command.Parameters["status"]);
                    if (status == 2)
                    {
                        user        = command.Requestor;
                        LoginStatus = LOGINSTATUS.LOGIN;
                        this.logger.Write(Log.common.enums.TYPE.INFO, string.Format(Messager.MSG_SOCKET_LOGIN_OK, user.NickName));
                    }
                    else
                    {
                        LoginStatus = LOGINSTATUS.LOGOUT;
                        this.logger.Write(Log.common.enums.TYPE.INFO, string.Format(Messager.MSG_SOCKET_LOGIN_NG, status));
                    }
                    break;

                case COMMAND.SG:
                    var sgcmd = SGCommand.ConvertFromCommand(command);
                    if (sgcmd != null)
                    {
                        CommandQueue.Enqueue(sgcmd);
                    }
                    else
                    {
                        SendCommand(MakeMSGCommand(command.Requestor.ID, Messager.MSG_SOCKET_BAD_COMMAND));
                    }
                    break;
                }
            }
        }
コード例 #5
0
        private void ProcessCommand(SGCommand command)
        {
            if (command == null)
            {
                return;
            }

            switch (command.Command)
            {
            case SGCMD.RTM:
                Proc_RTM(command);
                //if(!Proc_RTM(command)){
                //    SendCommand(MakeMSGCommand(command.RequestorID, "bad command."));
                //}
                break;

            case SGCMD.CANCEL:
                Proc_CANCEL(command);
                break;
            }
        }
コード例 #6
0
    public SGCommand ReceiveStream(int player)
    {
        NetworkStream stream;

        if (m_clients[player] != null && m_clients[player].Connected)
        {
            stream = m_clients[player].GetStream();
        }
        else
        {
            Debug.LogWarning("Trying to receive stream from disconnected player " + player);
            return(null);
        }

        if (stream.DataAvailable)
        {
            // Decode the first 32 bits to find the length of the remaining data
            byte[] dataSizeBytes = new byte[4];
            stream.Read(dataSizeBytes, 0, 4);
            int dataSize = BitConverter.ToInt32(dataSizeBytes, 0);
            dataSize = IPAddress.NetworkToHostOrder(dataSize);
            //Debug.Log("Received data of size " + dataSize + " bytes");

            // Receive the data
            // Convert size to bytes
            byte[] data = new byte[dataSize];
            stream.Read(data, 0, dataSize);

            // Put the data into a BitStream
            BKSystem.IO.BitStream clientStream = new BKSystem.IO.BitStream(dataSize * 8);
            clientStream.Write(data);

            if (clientStream.Length > 0)
            {
                return(SGCommand.CreateCommandFromPacket(clientStream, player));
            }
        }
        return(null);
    }
コード例 #7
0
        private void ProcessCommand(SGCommand command)
        {
            if (command == null)
            {
                return;
            }

            switch (command.Command)
            {
            case SGCMD.RTM:
                //var data = balancer.StockInfo;
                //if (data != null)
                //{
                //    command.Message = data.ToString();
                //    this.PutData<SGCommand>(SocketClient.ID, command);
                //}
                break;

            default:
                break;
            }
        }
コード例 #8
0
        private void Proc_CANCEL(SGCommand command)
        {
            var lx_temp = (command.Parameters.ContainsKey("cmd")) ? command.Parameters["cmd"] : null;
            var ls_cmd  = (lx_temp != null) ? lx_temp.ToString().Trim().ToUpper() : null;

            if (string.IsNullOrWhiteSpace(ls_cmd))
            {
                return;
            }
            SGCMD lsg_cmd = SGCMD.NONE;

            if (!Enum.TryParse <SGCMD>(ls_cmd, out lsg_cmd) || lsg_cmd == SGCMD.NONE)
            {
                return;
            }

            var lsg_ccmd = ContinuousCommands.FirstOrDefault(cmd => cmd.Command == lsg_cmd && cmd.RequestorID.Equals(command.RequestorID));

            if (lsg_ccmd != null)
            {
                ContinuousCommands.Remove(lsg_ccmd);
            }
        }
コード例 #9
0
        static void Main(string[] args)
        {
            int port;

            ParseArgs(out port, args);

            ServerConnectionManager.Instance.InitializeServer(IPAddress.Any, port);

            Debug.Log("Waiting for connection...");
            ServerConnectionManager.Instance.ConnectToClient(0);
            Debug.Log("Waiting for connection...");
            ServerConnectionManager.Instance.ConnectToClient(1);
            Debug.Log("Both players connected! Starting game...");

            CGC_SetPlayerID setPlayer0 = new CGC_SetPlayerID(0);

            ServerConnectionManager.Instance.TransmitStream(setPlayer0.PackCommand(), 0);
            CGC_SetPlayerID setPlayer1 = new CGC_SetPlayerID(1);

            ServerConnectionManager.Instance.TransmitStream(setPlayer1.PackCommand(), 1);

            bool playing = true;

            CardGameManager cgManager = new CardGameManager();

            cgManager.RunGameLogic();

            while (playing)
            {
                SGCommand player1Command = ServerConnectionManager.Instance.ReceiveStream(0);
                if (player1Command != null)
                {
                    player1Command.ExecuteCommand(cgManager);
                }

                SGCommand player2Command = ServerConnectionManager.Instance.ReceiveStream(1);
                if (player2Command != null)
                {
                    player2Command.ExecuteCommand(cgManager);
                }

                bool connected = ServerConnectionManager.Instance.Connected();
                playing = connected;
            }


            /* TO-DO list
             *  1. Replace all "CGVisualManager.instance.AddCommand(command)" calls with transmitting
             *      the appropriate bitstream to each client
             *  2. Replace all the scripts in the unity project with these ones
             *  3. Re-build this project using the scripts in the unity folder so both projects use the same files
             *  4. Write a servercommand system similar to CGCommand for
             *     sending requests back to the server
             *  5. Document how everything works!
             *  6. Add wait in main server loop
             *
             *
             *
             *
             */
        }