/// <summary>
        /// Do handshake for client side.
        /// </summary>
        /// <param name="clientSocket"> Socket from client side to server side</param>
        /// <returns>null if handShake fail otherwise return instance of AProtocol</returns>
        public async Task <AProtocol> HandShakeClient(NetworkStream clientSocket)
        {
            HelloCommand hello = new HelloCommand(ClientConfig.SUPPORTED_PROTOCOLS);
            await hello.SendAsync(clientSocket);

            ACommand shouldBeOLLEH = GetCommand(await clientSocket.ReadLineAsync());

            if (shouldBeOLLEH is OllehCommand)
            {
                OllehCommand olleh    = (OllehCommand)shouldBeOLLEH;
                AProtocol    protocol = ClientConfig.PROTOCOL_FACTORY.GetProtocol(olleh.PROTOCOL);
                if (protocol != null)
                {
                    AckCommand ack = new AckCommand();
                    await ack.SendAsync(clientSocket);

                    return(protocol);
                }
                ErrorCommand error = new ErrorCommand($"Unsupported protocols '{olleh.PROTOCOL}'. Handshake failed.");
                await error.SendAsync(clientSocket);
            }
            else
            {
                printIfErrorElseSendMessage(shouldBeOLLEH, "Handshake error. Expected OllehCommand but receive " + shouldBeOLLEH.GetType().Name, clientSocket);
            }
            return(null);
        }
        /// <summary>
        /// Do handshake for server side.
        /// </summary>
        /// <param name="serverSocket"> Socket from client side to server side</param>
        /// <returns>null if handShake fail otherwise retur instance of AProtocol</returns>
        public async Task <AProtocol> HandShakeServer(SuperNetworkStream serverSocket)
        {
            ACommand shoudBeHELLO = GetCommand(await serverSocket.ReadLineAsync());

            if (shoudBeHELLO is HelloCommand)
            {
                String       selectedProtocolLabel;
                HelloCommand hello    = (HelloCommand)shoudBeHELLO;
                AProtocol    protocol = ServerConfig.PROTOCOL_FACTORY.GetProtocol(out selectedProtocolLabel, hello.SUPPORTED_PROTOCOLS);
                if (protocol != null)
                {
                    OllehCommand olleh = new OllehCommand(selectedProtocolLabel);
                    await olleh.SendAsync(serverSocket);

                    ACommand shoudBeACK = GetCommand(await serverSocket.ReadLineAsync());
                    if (shoudBeACK is AckCommand)
                    {
                        return(protocol);
                    }
                    else
                    {
                        printIfErrorElseSendMessage(shoudBeACK, "Handshake error. Expected HelloCommand but receive:" + shoudBeACK.GetType().Name, serverSocket);
                    }
                }
                await serverSocket.SendCommandAsync(new ErrorCommand(String.Format("Unsupported protocols '{0}'. Handshake failed.", hello.SUPPORTED_PROTOCOLS)));
            }
            else
            {
                printIfErrorElseSendMessage(shoudBeHELLO, "Handshake error. Expected HelloCommand but receive:" + shoudBeHELLO.GetType().Name, serverSocket);
            }

            return(null);
        }
예제 #3
0
        /// <summary>
        /// Do handshake at client client side.
        /// </summary>
        private void handShake()
        {
            HandShakeProtocol handShakeProtocol = new HandShakeProtocol();
            AProtocol         protocol          = handShakeProtocol.HandShakeClient(COMMUNICATION).Result;

            if (protocol == null)
            {
                disconnect("Handshake fail.");
            }
            else
            {
                COMMUNICATION.PROTOCOL = protocol;
            }
        }
예제 #4
0
        private static KeyValuePair <string, AProtocol>[] getSupportedProcotols()
        {
            List <KeyValuePair <string, AProtocol> > supportedProtocols = new List <KeyValuePair <string, AProtocol> >();

            Type [] protocolTypes = (Type[])Assembly.GetAssembly(typeof(AProtocol))
                                    .GetTypes()
                                    .Where(t =>
                                           t.IsClass &&
                                           typeof(AProtocol).IsAssignableFrom(t) &&
                                           t.GetCustomAttribute(typeof(ProtocolDescription)) != null)
                                    .ToArray();
            foreach (Type protocolType in protocolTypes)
            {
                AProtocol protocol = (AProtocol)Activator.CreateInstance(protocolType);
                string    name     =
                    ((ProtocolDescription)protocolType.GetCustomAttribute(typeof(ProtocolDescription))).NAME;
                KeyValuePair <string, AProtocol> keyValuePair = new KeyValuePair <string, AProtocol>(name, protocol);
                supportedProtocols.Add(keyValuePair);
            }
            return(supportedProtocols.ToArray());
        }
예제 #5
0
        protected async void handshake(NetworkStream sns)
        {
            HandShakeProtocol handshakeProtocol = new HandShakeProtocol();
            await Task.Yield();

            AProtocol protocol = await handshakeProtocol.HandShakeServer(sns);

            if (protocol == null)
            {
                disconnect(sns, "Handshake fail.");
            }
            else
            {
                sns.PROTOCOL = protocol;
                await sns.SendCommandAsync(GetGameTypeCommand(Battlefield));

                if (!Battlefield.AddRobot(sns))
                {
                    disconnect(sns, "Arena is full.");
                }
            }
        }