/// <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); }