public static bool JoinRoom(Socket s, int roomNum) { byte[] joinRoomRequest = PacketMaker.CreatePacket(PacketMaker.CommandCode.JOIN_ROOM, Convert.ToUInt16(roomNum), Connection.StringToByte("0")); Connection.ClientToServer(s, joinRoomRequest); ChatProtocol joinResult = Connection.ServerToClient(s); if (joinResult.command == PacketMaker.CommandCode.JOIN_ROOM_RESULT) { if (BitConverter.ToInt32(joinResult.variableLengthField, 0) == 1) { Console.WriteLine("Join to Room # : " + roomNum); Chat.StartChat(roomNum); return(true); } else if (BitConverter.ToInt32(joinResult.variableLengthField, 0) == -1) { Console.WriteLine("Join Room Failed"); return(false); } } else { Console.WriteLine("Invalid Message from Server"); return(false); } return(false); }
public static void LogIn(Socket s) { string id = String.Empty; string pw = String.Empty; int idMaxSize = 12; //Get correct login info do { Console.Write("Enter your ID : "); id = Console.ReadLine(); }while (!IsValidID(id, idMaxSize)); do { Console.Write("Enter your PW : "); pw = Console.ReadLine(); }while (!IsValidPW(pw, idMaxSize)); //Send login info to server string loginInfo = id + "#" + pw; byte[] loginData = PacketMaker.CreatePacket(PacketMaker.CommandCode.LOGIN, Convert.ToUInt16(loginInfo.Length), Connection.StringToByte(loginInfo)); Connection.ClientToServer(s, loginData); }
public static void BuildInput() { ConsoleKeyInfo holdKey = new ConsoleKeyInfo(); //command = null; do { inputString = command.ToString(); command.Clear(); //When the command wasn't a 'stop', clear the request and continue collecting the new input do { holdKey = Console.ReadKey(); switch (holdKey.KeyChar) { case '\r': break; case '\b': if (command.Length > 0) { command.Remove(command.Length - 1, 1); } break; default: command.Append(holdKey.KeyChar); break; } wasCharWritten = true; } while (holdKey.KeyChar != '\r'); if (!command.ToString().ToUpper().Equals("LEAVE")) { wasStringSended = true; } //On a return key(=enter), stop the loop to check for a "LEAVE" command } while (!command.ToString().ToUpper().Equals("LEAVE")); //Run this build input until a stop command is received wasStopChatted = true;//Need to lock byte[] leaveChat = PacketMaker.CreatePacket(PacketMaker.CommandCode.LEAVE_ROOM, 0, Connection.StringToByte("0")); Connection.ClientToServer(Connection.cliSocket, leaveChat); ChatProtocol leaveResult = Connection.ServerToClient(Connection.cliSocket); //signal that a stop command has been fired }
public void GetHeartBeat() { while (true) { ChatProtocol heartBeatPacket = Connection.ServerToClient(Connection.cliSocket); if (heartBeatPacket.command == 60) { Console.Write("gotcha hb!"); byte[] heartBeatResponse = PacketMaker.CreatePacket(PacketMaker.CommandCode.HEARTBEAT_RESULT, 0, Connection.StringToByte("0")); Connection.ClientToServer(Connection.cliSocket, heartBeatResponse); Console.Write("response to hb"); } } }
public static void SendMessage() { string inputMessage = String.Empty; ushort messageStringSize = 0; byte[] msgArray = new byte[1024]; //create Packet inputMessage = KeyInputController.inputString; Array.Copy(Encoding.UTF8.GetBytes(inputMessage), msgArray, Encoding.UTF8.GetBytes(inputMessage).Length); messageStringSize = Convert.ToUInt16(Encoding.UTF8.GetBytes(inputMessage).Length); byte[] sendMessagePacket = PacketMaker.CreatePacket(PacketMaker.CommandCode.MESSAGE_TO_SERVER, messageStringSize, msgArray); //send Packet to server Connection.ClientToServer(Connection.cliSocket, sendMessagePacket); }
public static int CreateRoom(Socket s) { int roomNumber = -1; int maxRoomNameLength = 20; string roomName; do { Console.WriteLine("Enter Room Name"); roomName = Console.ReadLine(); if (roomName.Length > maxRoomNameLength) { Console.WriteLine("방 이름이 너무 길어양"); } } while (roomName.Length > maxRoomNameLength); ushort roomNameLength = Convert.ToUInt16(roomName.Length); byte[] newRoomRequest = PacketMaker.CreatePacket(PacketMaker.CommandCode.CREATE_ROOM, roomNameLength, Connection.StringToByte(roomName)); Connection.ClientToServer(s, newRoomRequest); Console.WriteLine(Chat.KeyInputController.wasStopChatted); ChatProtocol newRoom = Connection.ServerToClient(s); if (newRoom.command == PacketMaker.CommandCode.CREATE_ROOM_RESULT) { roomNumber = BitConverter.ToInt32(newRoom.variableLengthField, 0); } else { Console.WriteLine("command error"); } return(roomNumber); }
public static void LogOut(Socket s) { byte[] logoutData = PacketMaker.CreatePacket(PacketMaker.CommandCode.LOGOUT, 0, Connection.StringToByte("0")); Connection.ClientToServer(s, logoutData); }