private static void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; TcpClient client = state.workClient; // Read data from the remote device. int bytesRead = client.GetStream().EndRead(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. lock (receiveBufferLock) { KSP.IO.MemoryStream ms = new KSP.IO.MemoryStream(); ms.Write(receiveBuffer, 0, receiveIndex); ms.Write(state.buffer, 0, bytesRead); receiveBuffer = ms.ToArray(); receiveIndex += bytesRead; handleReceive(); // Get the rest of the data. client.GetStream().BeginRead(state.buffer, 0, StateObject.BufferSize, new AsyncCallback(ReceiveCallback), state); } } else { // // All the data has arrived // if (receiveIndex > 1) { // //LogAndShare("Done:" + System.Text.Encoding.ASCII.GetString(receiveBuffer)); // // } } } catch (InvalidOperationException e) { Log.Debug("Exception thrown in ReceiveCallback(), catch 1, Exception: {0}", e.ToString()); } catch (ThreadAbortException e) { Log.Debug("Exception thrown in ReceiveCallback(), catch 2, Exception: {0}", e.ToString()); } catch (Exception e) { Log.Debug("Exception thrown in ReceiveCallback(), catch 3, Exception: {0}", e.ToString()); passExceptionToMain(e); } }
//Plugin Interop static bool writePluginInterop() { bool success = false; lock (interopOutQueueLock) { if (interopOutQueue.Count > 0) { try { while (interopOutQueue.Count > 0) { byte[] message; message = interopOutQueue.Dequeue(); KSP.IO.MemoryStream ms = new KSP.IO.MemoryStream(); ms.Write(KMPCommon.intToBytes(KMPCommon.FILE_FORMAT_VERSION), 0, 4); ms.Write(message, 0, message.Length); gameManager.acceptClientInterop(ms.ToArray()); } success = true; } catch (Exception e) { KMP.Log.Debug("Exception thrown in writePluginInterop(), catch 1, Exception: {0}", e.ToString()); } } } return success; }