private void UClientEndReadWS(IAsyncResult ar) { // Fetch the state object[] args = (object[])ar.AsyncState; MeshMapper mm = (MeshMapper)args[0]; webSocketClient wc = (webSocketClient)args[1]; UdpClient uclient = (UdpClient)args[2]; int counter = wc.id; byte[] buf = null; try { // Read the data if (uclient != null) { buf = uclient.EndReceive(ar, ref wc.endpoint); } } catch (Exception) { ShutdownClients(null, uclient, wc, counter); return; } if (buf != null) { mm.bytesToServer += buf.Length; mm.bytesToServerCompressed += wc.SendBinary(buf, 0, buf.Length); // TODO: Do Async try { uclient.BeginReceive(new AsyncCallback(UClientEndReadWS), new object[] { mm, wc, uclient }); } catch (Exception) { } } }
// Read from the local client private void ClientEndReadWS(IAsyncResult ar) { // Fetch the state object[] args = (object[])ar.AsyncState; MeshMapper mm = (MeshMapper)args[0]; webSocketClient wc = (webSocketClient)args[1]; TcpClient client = (TcpClient)args[2]; byte[] buf = (byte[])args[3]; int counter = wc.id; int len = 0; try { // Read the data if (client != null && client.Connected == true) { len = client.GetStream().EndRead(ar); } } catch (Exception) { ShutdownClients(client, null, wc, counter); return; } //Debug("#" + counter + ": ClientEndRead(" + len + ") - Local Read"); if (len > 0) { // Forward the data & read again try { mm.bytesToServer += len; mm.bytesToServerCompressed += wc.SendBinary(buf, 0, len); // TODO: Do Async try { client.GetStream().BeginRead(buf, 0, buf.Length, new AsyncCallback(ClientEndReadWS), new object[] { mm, wc, client, buf }); } catch (Exception) { } } catch (Exception) { ShutdownClients(client, null, wc, counter); return; } } else { ShutdownClients(client, null, wc, counter); return; } }