public void CheckSubscription(Client client) { List <Subscription> subs = new List <Subscription>(client.ActiveSubscriptions); foreach (Subscription sub in subs) { if (sub.ExpirationDateTime >= DateTime.Now) { client.ActiveSubscriptions.Remove(sub); client.ExpiredSubscription.Add(sub); PendingNotification notification = new PendingNotification() { ClientID = client.ID, IPAddress = client.IpAddress.ToString(), Port = client.Port, NotificationType = NotificationType.SubscriptionExpired, SubscriptionID = sub.ID }; //send notification try { Authentication auth = new Authentication(SocketType.Stream, AddressFamily.InterNetwork, ProtocolType.Tcp); auth.NotifyServerSide(notification, client.IpAddress.ToString(), 50000); } catch (Exception) { _pendingNotifications.Add(notification); } } } }
public void NotifyServerSide(PendingNotification notification, string ipAdd, int port) { _addressFamily = AddressFamily.InterNetwork; _ipAddress = IPAddress.Parse(ipAdd); _ipEndPoint = new IPEndPoint(_ipAddress, port); _socket = new Socket(_addressFamily, _socketType, _protocolType); _socket.Connect(_ipEndPoint); if (_socket == null) { throw new Exception("Socket is null"); } //send command string command = "NOTIFICATION"; _socket.Send(BitConverter.GetBytes(command.Length * 2)); _socket.Send(Encoding.Unicode.GetBytes(command)); byte[] status = new byte[4]; _socket.Receive(status); if (Encoding.Unicode.GetString(status) == "OK") { MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, notification); byte[] buffer = ms.GetBuffer(); _socket.Send(BitConverter.GetBytes(buffer.Length)); _socket.Send(buffer); _socket.Receive(status); if (Encoding.Unicode.GetString(status) == "OK") { command = "TERMINATE"; _socket.Send(BitConverter.GetBytes(command.Length * 2)); _socket.Send(Encoding.Unicode.GetBytes(command)); } } }
public void NotifyClientSide(Socket socket) { byte[] ok = Encoding.Unicode.GetBytes("OK"); byte[] er = Encoding.Unicode.GetBytes("ER"); socket.Send(ok); byte[] buffer = new byte[4]; socket.Receive(buffer); buffer = new byte[BitConverter.ToInt32(buffer, 0)]; socket.Receive(buffer); BinaryFormatter formatter = new BinaryFormatter(); PendingNotification notification = (PendingNotification)formatter.Deserialize(new MemoryStream(buffer)); socket.Send(ok); _state.SetNotification(notification); }
public void SetNotification(PendingNotification notification) { _pendingNotification = true; _notifications.Add(notification); }