Didn't know which title to set, because I don't exactly know what is the problem. I had a working c#
code, i wrapped it into dll
to separate client from other functions, but now it doesn't work like intended. It is a client-server over udp
. Here is my code:
HandlerFactory factory = new HandlerFactory();
factory.RegisterHandler(RequestType.Options, () => new OptionsHandler());
factory.RegisterHandler(RequestType.Unauthorized, () => new UnauthorizedHandler());
factory.RegisterHandler(RequestType.Ok, () => new OkHandler());
factory.RegisterHandler(RequestType.Invite, () => new InviteHandler());
factory.RegisterHandler(RequestType.Bye, () => new ByeHandler());
factory.RegisterHandler(RequestType.Error, () => new ErrorHandler());
Application.SetCompatibleTextRenderingDefault(false);
Client client = new Client(factory);
client.Start();
client.Register();
And in client:
private void Listen()
{
while (!_stopping)
{
try
{
var asyncResult = MyUdpClient.BeginReceive(HandleIncomingUdpRequest, null);
WaitHandle.WaitAny(new[] { _stopHandle, asyncResult.AsyncWaitHandle });
}
catch (Exception ex)
{
using (var sw = new StreamWriter("log.txt"))
{
sw.WriteLine("Date:" + DateTime.Now + "\r\nError:" + ex.Message + "\r\n");
}
}
}
MyUdpClient.Close();
}
private void HandleIncomingUdpRequest(IAsyncResult ar)
{
var received = MyUdpClient.EndReceive(ar, ref _serverAddress);
var requestString = Encoding.ASCII.GetString(received);
var type = Helper.GetRequestType(requestString);
if (type != RequestType.Trying && type != RequestType.Unknown)
{
ProcessRequest(type, requestString);
}
}
So, i wrapped it into dll, and now use it like this:
var factory = new Factory.HandlerFactory();
factory.RegisterHandler(RequestType.Options, ()=> new OptionsHandler());
factory.RegisterHandler(RequestType.Unauthorized, () => new UnauthorizedHandler());
factory.RegisterHandler(RequestType.Ok, () => new OkHandler());
factory.RegisterHandler(RequestType.Invite, () => new InviteHandler());
factory.RegisterHandler(RequestType.Bye, () => new ByeHandler());
factory.RegisterHandler(RequestType.Error, () => new ErrorHandler());
Client cl = new Client(factory);
cl.Start();
cl.Register();
cl.Call("2");
The problem is, on cl.Call("2")
i get "An unhandled exception of type 'System.ObjectDisposedException' occurred in System.dll" in
var received = MyUdpClient.EndReceive(ar, ref _serverAddress);
And in Register(), client should send Register Request, get response that he is not Unauthorized and call UnauthorizedHandler, but this is not happening.
仅从指定的行和您的描述开始,我将检查ref _serverAddress
是否正确初始化,因为它似乎是全局变量,该变量很可能是在主应用程序中初始化的,但是您已将用法移到了dll中。
The problem was - I don't know how it happened - in this code:
private void Listen()
{
while (!_stopping)
{
try
{
var asyncResult = MyUdpClient.BeginReceive(HandleIncomingUdpRequest, null);
WaitHandle.WaitAny(new[] { _stopHandle, asyncResult.AsyncWaitHandle });
}
catch (Exception ex)
{
using (var sw = new StreamWriter("log.txt"))
{
sw.WriteLine("Date:" + DateTime.Now + "\r\nError:" + ex.Message + "\r\n");
}
}
MyUdpClient.Close();
}
}
MyUdpClient.Close()
was called inside the loop. I must have changed try-catch
blocks and accidentally put that inside the loop, and when I asked this question i copypasted it from different source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.