Reading in JSON-RPC for LSP msg returning Null???

So long story short Im building an LSP and it has been a process. I finally got it hooked up to read in stdin from vscode only for my message to never return anything but null. Its not hat at some points I dont have the full message but as you'll see from my custom scanner and Split function the whole purpose is to scan until I am certain I have a finished message from vscode once I do for right now I just send the actual message to the logger to a text file.
2 Replies
Humoroussix2799
Humoroussix27992mo ago
So here is the log.txt:
Humoroussix2799
Humoroussix27992mo ago
ignore the json really just pay attention to the logs and here is my main program:
static void Main(string[] args)
{
if (Debugger.IsAttached) Debugger.Launch();

logger = GetLogger("C:/Users/Admin/Desktop/test-log.txt");
logger.Log("Hey I started");
// byte[] incomingMsg = Encoding.UTF8.GetBytes("Content-Length: 15\r\n\r\n{\"Method\":\"hi\"}");
// Read input from stdin

using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
{
logger.Log("We Opened Stdout");
CustomScanner scanner = new CustomScanner(reader.BaseStream);
logger.Log("Okay We Just Made It Past Custom Scanner");
while (scanner.Scan())
{
logger.Log("Currently Scanning!!!");
byte[] msg = scanner.Bytes();
if (msg != null)
{
logger.Log("The MSG was not null");
string message = System.Text.Encoding.UTF8.GetString(msg);
HandleMessage(logger, message);
}
else
{
logger.Log("Msg Was Null I.e it failed :(");
}
}
}

}




private static object HandleMessage(Logger loger, object message)
{
loger.Log(message.ToString());
return null;
}
static void Main(string[] args)
{
if (Debugger.IsAttached) Debugger.Launch();

logger = GetLogger("C:/Users/Admin/Desktop/test-log.txt");
logger.Log("Hey I started");
// byte[] incomingMsg = Encoding.UTF8.GetBytes("Content-Length: 15\r\n\r\n{\"Method\":\"hi\"}");
// Read input from stdin

using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
{
logger.Log("We Opened Stdout");
CustomScanner scanner = new CustomScanner(reader.BaseStream);
logger.Log("Okay We Just Made It Past Custom Scanner");
while (scanner.Scan())
{
logger.Log("Currently Scanning!!!");
byte[] msg = scanner.Bytes();
if (msg != null)
{
logger.Log("The MSG was not null");
string message = System.Text.Encoding.UTF8.GetString(msg);
HandleMessage(logger, message);
}
else
{
logger.Log("Msg Was Null I.e it failed :(");
}
}
}

}




private static object HandleMessage(Logger loger, object message)
{
loger.Log(message.ToString());
return null;
}
and here is the CustomScanner Class:
public class CustomScanner
{
private readonly Stream stream;
//private byte[] buffer = new byte[0];

public CustomScanner(Stream inputStream)
{
stream = inputStream;
}

public bool Scan()
{
Program.logger.Log("We are in the Scan process");
// Simulate splitting logic using Rpc.Split
try
{

(int advance, byte[] token) = Rpc.Split(ReadStream(stream), true);
if (token != null)
{
return true;
}
}catch (Exception e)
{
Program.logger.Log("Ran Into a Problem trying to access the Split funciton ERR: " + e.Message);

}


return false;
}

public byte[] Bytes()
{
// Return the current token/message
Program.logger.Log("We Made it to the Bytes process and the current stream is: " + Encoding.UTF8.GetString(ReadStream(stream)));
return Rpc.Split(ReadStream(stream), true).token;

}

private byte[] ReadStream(Stream inputStream)
{
// Read all bytes from the stream
using (MemoryStream ms = new MemoryStream())
{
inputStream.CopyTo(ms);
return ms.ToArray();
}
}
}
public class CustomScanner
{
private readonly Stream stream;
//private byte[] buffer = new byte[0];

public CustomScanner(Stream inputStream)
{
stream = inputStream;
}

public bool Scan()
{
Program.logger.Log("We are in the Scan process");
// Simulate splitting logic using Rpc.Split
try
{

(int advance, byte[] token) = Rpc.Split(ReadStream(stream), true);
if (token != null)
{
return true;
}
}catch (Exception e)
{
Program.logger.Log("Ran Into a Problem trying to access the Split funciton ERR: " + e.Message);

}


return false;
}

public byte[] Bytes()
{
// Return the current token/message
Program.logger.Log("We Made it to the Bytes process and the current stream is: " + Encoding.UTF8.GetString(ReadStream(stream)));
return Rpc.Split(ReadStream(stream), true).token;

}

private byte[] ReadStream(Stream inputStream)
{
// Read all bytes from the stream
using (MemoryStream ms = new MemoryStream())
{
inputStream.CopyTo(ms);
return ms.ToArray();
}
}
}
As you can see it logs the message right before it reaches the bytes func... but once it does suddenly the message eviscerates gone just like that its null???? hence why it ''fails'' and yet just before the actual check in the log you can clearly see the message is complete? there is a couple of weird things I noticed which is that the length doesnt match. Im not really sure what thats about but its out of the scope of this Q since it should'nt affect this I needed a Buffer