private void ReceiveCallback(IAsyncResult result) { ConnectionInfo connection = result.AsyncState as ConnectionInfo; try { Socket publisherSocket = connection.socket; // socketul de la publisher SocketError response; int buffSize = publisherSocket.EndReceive(result, out response); // terminam primirea datelor if (response == SocketError.Success) { byte[] packet = new byte[buffSize]; // alocam memorie pentru packet Array.Copy(connection.data, packet, packet.Length); // copiem datele din packetul transmis var data = PacketHandler.Handle(packet); // aflam portul si mesajul RedirectData(data.message, data.portNumber); // rederictionam datele } } catch (Exception e) { Console.WriteLine($"Can't receive data from Client, {e.Message}"); } finally { try { connection.socket.BeginReceive(connection.data, 0, connection.data.Length, SocketFlags.None, ReceiveCallback, connection); //incepem sa primi datele in continuare } catch (Exception e) { Console.WriteLine($"{e.Message}"); connection.socket.Close(); } } }
private void ReceiveCallback(IAsyncResult result) { ConnectionInfo connection = result.AsyncState as ConnectionInfo; try { Socket senderSocket = connection.socket; // socketul de la publisher SocketError response; int buffSize = senderSocket.EndReceive(result, out response); // terminam primirea datelor IPEndPoint remoteIpEndPoint = senderSocket.RemoteEndPoint as IPEndPoint; if (response == SocketError.Success) { byte[] packet = new byte[buffSize]; // alocam memorie pentru packet Array.Copy(connection.data, packet, packet.Length); // copiem datele din packetul transmis var data = PacketHandler.Handle(packet); // aflam id-ul si mesajul if (remoteIpEndPoint.Port != 9001) { connection.port = (ushort)remoteIpEndPoint.Port; if (data.message == "subscribe") { string key = data.themeId + "#" + connection.port; if (!subscribers.TryAdd(key, connection)) { ConnectionInfo temp; var j = subscribers.TryRemove(key, out temp); subscribers.TryAdd(key, connection); subscribers[key].lostMessages = temp.lostMessages; } if (subscribers[key].lostMessages != null && subscribers[key].lostMessages.Any()) { while (subscribers[key].lostMessages.Any()) { string message = ""; subscribers[key].lostMessages.TryDequeue(out message); try { subscribers[key].socket.Send(Encoding.ASCII.GetBytes(message)); } catch (Exception e) { Console.WriteLine($"Could not send lostMessage data to {key}. Contact Ernest x_X ..."); } } } } else if (data.message == "unsubscribe") { ConnectionInfo temp; subscribers.TryRemove(data.themeId + "#" + connection.port, out temp); } } else { Theme t = DbUtility.GetThemeFromDb(data.themeId); if (t != null) { Console.WriteLine(t.ToString()); } foreach (var x in subscribers) { if (x.Key.Split('#')[0] == data.themeId.ToString()) { try { x.Value.socket.Send(Encoding.ASCII.GetBytes(data.message)); } catch (Exception e) { Console.WriteLine($"Could not send data to {x.Key}. Adding to SendLater..."); x.Value.lostMessages.Enqueue(data.message); } } } } } } catch (Exception e) { Console.WriteLine($"Can't receive data from Client, {e.Message}"); } finally { try { connection.socket.BeginReceive(connection.data, 0, connection.data.Length, SocketFlags.None, ReceiveCallback, connection); //incepem sa primi datele in continuare } catch (Exception e) { Console.WriteLine($"{e.Message}"); //connection.socket.Close(); } } }