private void receiveCallback(IAsyncResult AR) { try { IPEndPoint iPEndPoint = null; byte[] data = _client.EndReceive(AR, ref iPEndPoint); NetData reader = new NetData(data); int cmd = reader.GetInt(); if (cmd == (int)Events.Connect && !_connectedToServer) { int res = reader.GetInt(); if (res == 1) { _connectedToServer = true; if (ConnectionAcceptedEvent != null) { ConnectionAcceptedEvent(); } else { throw new Exception("ConnectionAcceptedEvent has not been set"); } } } else if (_connectedToServer) { if (DataReceivedEvent != null) { DataReceivedEvent(new NetData(data)); } else { throw new Exception("DataReceivedEvent has not been set"); } } _client.BeginReceive(new AsyncCallback(receiveCallback), null); } catch (Exception ex) { throw new Exception(ex.ToString()); } }
private void receiveCallback(IAsyncResult AR) { NetClient client = null; try { IPEndPoint fromIP = null; byte[] data = _udpServer.EndReceive(AR, ref fromIP); client = GetNetClientFromEndPoint(fromIP); if (client == null) { client = new NetClient(fromIP); } NetData reader = new NetData(data); int cmd = reader.GetInt(); if (cmd == (int)Events.Connect && !_connectedClients.Contains(client)) { string key = reader.GetString(); if (key == _connectionKey) { AcceptClient(client); } else { DeclineClient(client); } } else { if (DataReceivedEvent != null) { DataReceivedEvent(client, new NetData(data)); } else { throw new Exception("DataReceivedEvent has not been set"); } } _udpServer.BeginReceive(new AsyncCallback(receiveCallback), null); } catch (Exception ex) { if (client != null) { if (ClientDisconnectedEvent != null) { ClientDisconnectedEvent(client); } } } }