public void SendLIST(NetworkStream stream, HashSet <User> users) { string listResponse = EnumCaster.MessageTypeToString(MessageType); foreach (User u in users) { if (u.Status == UserStatus.Available) { listResponse += ":" + u.Name; } } Send(stream, listResponse); }
public void SendHANG(NetworkStream stream) { string hang = EnumCaster.MessageTypeToString(MessageType); Send(stream, hang); }
public void SendCONN(NetworkStream stream, HashSet <User> users) { string confirm = EnumCaster.MessageTypeToString(MessageType) + ":" + Informations[0] + ":" + GetUserIPAddress(Informations[0], users) + ":" + Informations[1]; Send(stream, confirm); }
public void SendDENY(NetworkStream stream) { string deny = EnumCaster.MessageTypeToString(MessageType) + ":" + Informations[0] + ":" + Informations[1]; Send(stream, deny); }
public void SendCALL(NetworkStream stream, HashSet <User> users) { string call = EnumCaster.MessageTypeToString(MessageType) + ":" + Informations[0] + ":" + Informations[1] + ":" + GetUserIPAddress(Informations[1], users) + ":" + Informations[2]; Send(stream, call); }
protected override void BeginDataTransmission(NetworkStream stream) { byte[] buffer = new byte[1024]; string portResponse = "PORT:"; string decline = "NACK"; Message msg; string name = ""; bool running = true; byte[] declineByte = new ASCIIEncoding().GetBytes(decline); while (running) { var data = GetData(stream, buffer); if (data != null) { if (data[0] == "HELL") { if (data.Length < 4) { Console.WriteLine("Invalid data: HELL"); stream.Write(declineByte, 0, declineByte.Length); } else { User u = new User(data[3], data[1], UserStatus.Available, stream); Console.WriteLine(u.Name + " connected"); lock (usersLock) { users.Add(u); } name = u.Name; int portNumber = FreeTcpPort(); portResponse += portNumber.ToString(); byte[] portResponseByte = new ASCIIEncoding().GetBytes(portResponse); stream.Write(portResponseByte, 0, portResponseByte.Length); portResponse = "PORT:"; msg = new Message(null, MessageType.LIST); lock (messagesLock) { messages.Add(msg); } } } else if (data[0] == "CALL") { if (data.Length < 4) { Console.WriteLine("Invalid data: CALL"); stream.Write(declineByte, 0, declineByte.Length); } else { msg = new Message(data.Skip(1).ToArray(), EnumCaster.MessageTypeFromString(data[0])); lock (messagesLock) { messages.Add(msg); } } } else if (data[0] == "CONN") { if (data.Length < 3) { Console.WriteLine("Invalid data: CONN"); stream.Write(declineByte, 0, declineByte.Length); } else { lock (usersLock) { foreach (User user in users) { if (user.Name == data[1] || user.Name == name) { user.Status = UserStatus.Busy; } } } lock (messagesLock) { msg = new Message(data.Skip(1).ToArray(), EnumCaster.MessageTypeFromString(data[0])); messages.Add(msg); msg = new Message(null, MessageType.LIST); messages.Add(msg); } } } else if (data[0] == "DENY") { if (data.Length < 3) { Console.WriteLine("Invalid data: DENY"); stream.Write(declineByte, 0, declineByte.Length); } else { msg = new Message(data.Skip(1).ToArray(), EnumCaster.MessageTypeFromString(data[0])); lock (messagesLock) { messages.Add(msg); } } } else if (data[0] == "LIST") { if (data.Length < 2) { Console.WriteLine("Invalid data: LIST"); stream.Write(declineByte, 0, declineByte.Length); } else { msg = new Message(null, EnumCaster.MessageTypeFromString(data[0])); msg.SendLIST(stream, users); } } else if (data[0] == "HANG") { if (data.Length < 3) { Console.WriteLine("Invalid data: HANG"); stream.Write(declineByte, 0, declineByte.Length); } else { lock (usersLock) { foreach (User user in users) { if (user.Name == data[1] || user.Name == data[2]) { user.Status = UserStatus.Available; } } } lock (messagesLock) { msg = new Message(data.Skip(1).ToArray(), EnumCaster.MessageTypeFromString(data[0])); messages.Add(msg); msg = new Message(null, MessageType.LIST); messages.Add(msg); } } } else if (data[0] == "EXIT") { if (data.Length < 2) { Console.WriteLine("Invalid data: EXIT"); stream.Write(declineByte, 0, declineByte.Length); } else { lock (usersLock) { foreach (User user in users) { if (user.Name == data[1]) { users.Remove(user); msg = new Message(null, MessageType.LIST); lock (messagesLock) { messages.Add(msg); } break; } } } Console.WriteLine(name + " disconnected"); running = false; } } } lock (messagesLock) { var listUpdates = messages.Where(x => x.MessageType == MessageType.LIST); if (listUpdates.Count() != 0) { Message tmp = listUpdates.First(); foreach (User user in users) { tmp.SendLIST(user.UserStream, users); } messages.Remove(tmp); } } lock (messagesLock) { var calls = messages.Where(x => x.MessageType == MessageType.CALL && x.Informations[0] == name); if (calls.Count() != 0) { //CALL:KEY:VALUE Message temp = calls.First(); temp.SendCALL(stream, users); messages.Remove(temp); } } lock (messagesLock) { var conns = messages.Where(x => x.MessageType == MessageType.CONN && x.Informations[0] == name); if (conns.Count() != 0) { //CONN:NAME:IP:PORT Message temp = conns.First(); temp.SendCONN(stream, users); messages.Remove(temp); } } lock (messagesLock) { var denys = messages.Where(x => x.MessageType == MessageType.DENY && x.Informations[0] == name); if (denys.Count() != 0) { //DENY:NAME_TO:NAME_FROM Message temp = denys.First(); temp.SendDENY(stream); messages.Remove(temp); } } lock (messagesLock) { var hangs = messages.Where(x => x.MessageType == MessageType.HANG && x.Informations[1] == name); if (hangs.Count() != 0) { //HANG:NAME_FROM:NAME_TO Message temp = hangs.First(); temp.SendHANG(stream); messages.Remove(temp); foreach (User user in users) { if (user.Name == name) { user.Status = UserStatus.Available; } } } } } }