public void sendToSocket(string id, string msg) { // Get buffer data bool[] bufferState = globalDataSet.getBufferState(); string[] sendBuffer = globalDataSet.getSendBuffer(); // Set message to local buffer sendBuffer[cntr] = ":" + id + ":" + msg + ";"; bufferState[cntr] = true; if (globalDataSet.DebugMode) { Debug.Write("sendBuffer[cntr]: " + sendBuffer[cntr]); } // Set local buffer to global buffer globalDataSet.setBufferState(bufferState); globalDataSet.setSendBuffer(sendBuffer); cntr += 1; if (cntr >= bufferState.Length - 1) { cntr = 0; } }
private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) { var remoteAddress = args.Socket.Information.RemoteAddress.ToString(); if (globalDataSet.DebugMode) { Debug.Write("Client is connected. \n"); } // Set flag to begin the sending of sensor data (in MainPage) when client is connected globalDataSet.clientIsConnected = true; Stream outStream = args.Socket.OutputStream.AsStreamForWrite(); StreamWriter writer = new StreamWriter(outStream); while (true) { try { bool[] bufferState = globalDataSet.getBufferState(); string[] sendbuffer = globalDataSet.getSendBuffer(); if (bufferState[i]) { writer.Write(sendbuffer[i]); writer.Flush(); } sendbuffer[i] = ""; bufferState[i] = false; globalDataSet.setBufferState(bufferState); globalDataSet.setSendBuffer(sendbuffer); if (i < sendbuffer.Length - 1) { i++; } else { writer.Write(""); writer.Flush(); i = 0; } } catch (Exception ex) { if (globalDataSet.DebugMode) { Debug.Write("Exception in sending \n"); } globalDataSet.clientIsConnected = false; return; } } }