/// <summary>
        /// Recibir información inicial del cliente
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool getClientInitialInfo(TgcSocketClientInfo clientInfo, Socket socket)
        {
            try
            {
                //Recibir info inicial del cliente
                TgcSocketRecvMsg msg = TgcSocketMessages.receiveMessage(socket, TgcSocketMessageHeader.MsgType.InitialMessage);
                if (msg == null)
                {
                    return(false);
                }

                //Guardar sus datos y cambiar su estado
                TgcSocketInitialInfoClient clientInitInfo = (TgcSocketInitialInfoClient)msg.readNext();
                clientInfo.Name   = clientInitInfo.clientName;
                clientInfo.Status = TgcSocketClientInfo.ClientStatus.WaitingClientOk;

                //Asignar Player ID a cliente
                TgcSocketInitialInfoServer serverInitInfo = new TgcSocketInitialInfoServer();
                serverInitInfo.serverName = serverName;
                serverInitInfo.playerId   = playerIdCounter;
                clientInfo.PlayerId       = serverInitInfo.playerId;
                playerIdCounter++;

                //Enviar info inicial del server
                TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                sendMsg.write(serverInitInfo);
                TgcSocketMessages.sendMessage(socket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);

                return(true);
            }
            catch (SocketException)
            {
                return(false);
            }
        }
        /// <summary>
        /// Empezar el handshake con el server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool doHandShake()
        {
            try
            {
                int    serverHandshakeLength = Encoding.ASCII.GetBytes(TgcSocketServer.SERVER_HANDSHAKE).Length;
                byte[] data = new byte[serverHandshakeLength];
                int    recv = clientSocket.Receive(data, data.Length, SocketFlags.None);
                if (recv > 0 && recv == serverHandshakeLength)
                {
                    string msg = Encoding.ASCII.GetString(data, 0, recv);
                    if (msg.Equals(TgcSocketServer.SERVER_HANDSHAKE))
                    {
                        //Server correcto, enviar informacion inicial
                        status = TgcSocketClientInfo.ClientStatus.RequireInitialInfo;
                        TgcSocketInitialInfoClient clientInitInfo = new TgcSocketInitialInfoClient();
                        clientInitInfo.clientName = clientName;

                        TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                        sendMsg.write(clientInitInfo);
                        TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);

                        return(true);
                    }
                }
            }
            catch (SocketException)
            {
                //Handshake incorrecto
                return(false);
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Empezar el handshake con el server
        /// </summary>
        /// <returns>True si todo salio bien</returns>
        private bool doHandShake()
        {
            try
            {
                int serverHandshakeLength = Encoding.ASCII.GetBytes(TgcSocketServer.SERVER_HANDSHAKE).Length;
                byte[] data = new byte[serverHandshakeLength];
                int recv = clientSocket.Receive(data, data.Length, SocketFlags.None);
                if (recv > 0 && recv == serverHandshakeLength)
                {
                    string msg = Encoding.ASCII.GetString(data, 0, recv);
                    if (msg.Equals(TgcSocketServer.SERVER_HANDSHAKE))
                    {
                        //Server correcto, enviar informacion inicial
                        status = TgcSocketClientInfo.ClientStatus.RequireInitialInfo;
                        TgcSocketInitialInfoClient clientInitInfo = new TgcSocketInitialInfoClient();
                        clientInitInfo.clientName = clientName;

                        TgcSocketSendMsg sendMsg = new TgcSocketSendMsg();
                        sendMsg.write(clientInitInfo);
                        TgcSocketMessages.sendMessage(clientSocket, sendMsg, TgcSocketMessageHeader.MsgType.InitialMessage);

                        return true;
                    }
                }
            }
            catch (SocketException)
            {
                //Handshake incorrecto
                return false;
            }

            return false;
        }