chkaufmann 17 Posted October 24 I try to use my own subclass for TIdServerContext to monitor the requests to my server. Now I sent a simple GET request with Postman to my server and noticed, that two context objects were created, but the OnCommandGet event is only called once. What is the reason for that? Then I plan to iterate all current requests like this: tmpList := IdHTTPServer1.Contexts.LockList; for i := 0 to tmpList.Count -1 do begin tmp := TMyContext(tmpList[i]); // read some info from tmp end; IdHTTPServer1.Contexts.UnLockList; Can I be sure, that all items will be alive. Or is it possible, that one of the context items was freed before I could read info from it? Christian Share this post Link to post
Remy Lebeau 1417 Posted October 24 (edited) 21 hours ago, chkaufmann said: Now I sent a simple GET request with Postman to my server and noticed, that two context objects were created, but the OnCommandGet event is only called once. What is the reason for that? A new Context is not created until a client is accepted. Only 1 Context is created per client. So, there must have been multiple clients accepted. But maybe a client didn't send a request. Quote Then I plan to iterate all current requests like this You should put that loop inside a try..finally block to ensure the list is always unlocked. Quote Can I be sure, that all items will be alive. Or is it possible, that one of the context items was freed before I could read info from it? The list will never hold a freed object, ie the pointers in it are always valid while inside of the lock. Make sure you are not accessing the pointers outside of the lock. There is no guarantee that a client assigned to a Context is still connected to the server while you are accessing the list. But the objects in a Context will still be alive while the list is locked. Edited October 25 by Remy Lebeau Share this post Link to post
chkaufmann 17 Posted October 25 Thanks for the details. It seems, there are two connections when I send a GET request with postman. But in the first one I end method TIdIOHandler.RaiseConnClosedGracefully. When I test with the Chrome browser, there is only one context / connection. Christian Share this post Link to post
Remy Lebeau 1417 Posted October 25 2 hours ago, chkaufmann said: It seems, there are two connections when I send a GET request with postman. That is a known issue with postman. 2 hours ago, chkaufmann said: But in the first one I end method TIdIOHandler.RaiseConnClosedGracefully. And how are you doing that, when there is no OnCommandGet event fired for that connection? The only option would be to use the OnConnect event, in which case you could instead just assign a ReadTimeout to the connection and let TIdHTTPServer close the connection when no request arrives. Share this post Link to post
chkaufmann 17 Posted October 25 1 hour ago, Remy Lebeau said: And how are you doing that, when there is no OnCommandGet event fired for that connection? The only option would be to use the OnConnect event, in which case you could instead just assign a ReadTimeout to the connection and let TIdHTTPServer close the connection when no request arrives. You mean get that exception? Maybe it's only when run the code with break points in the debugger. Anyway, it works fine now and get the details for my monitor functionality. Share this post Link to post
Remy Lebeau 1417 Posted October 25 7 hours ago, chkaufmann said: You mean get that exception? You said: "But in the first one I end method TIdIOHandler.RaiseConnClosedGracefully". That implied to me that you are directly calling RaiseConnClosedGracefully() yourself. If that is not the case, then ignore what I said. Indy will raise an EIdConnClosedGracefully exception when the extra client disconnects if it does not send a request. Share this post Link to post