FreeDSx / SNMP

A Pure PHP SNMP Library.
MIT License
57 stars 13 forks source link

Do I need to close SnmpClient? #11

Closed decadence closed 2 years ago

decadence commented 2 years ago

Hello. Do I need to close SnmpClient after usage? For example if I have several instances of this class. I don't see any corresponding method in SnmpClient.php.

ChadSikorra commented 2 years ago

Since SNMP operates over UDP, it should not be strictly necessary to close it I would think. Are you seeing issues because of it?

decadence commented 2 years ago

I have no issues, just think about it because PHP native library has close method

https://www.php.net/manual/en/snmp.close.php

ChadSikorra commented 2 years ago

The SNMP extension functions very differently from this library. I assume the extension uses some wrapper around a C based SNMP library.

I added a close() method on the SnmpClient class that will close the underlying socket connection. Though I would only use it if you find a need for it. The only circumstances I can see it being useful is if you changed the transport option of this library to use tcp instead of udp. Or if you have a long running PHP process with several SnmpClient classes instantiated that do not go out of scope and thus need to cleanup resources.

Otherwise, with normal usage PHP would end up just cleaning up the socket resource once it goes out of scope. So in a short lived script It wouldn't really be necessary to close the connection, IMO. There is no such message for a "close" in the SNMP protocol.

I tagged a new version that adds the close() method if you would like to use it. If you have any issues with it let me know!

decadence commented 2 years ago

Thanks! Yes, I am using long running PHP console script with multiple SnmpClient instances. Without issues so far but explicit close will be helpful anyway. Maybe I'll switch transport to tcp (didn't know about this possibility)

decadence commented 2 years ago

transport option

I don't see it in current documentation 🤔

decadence commented 2 years ago

image Found it in source code. Seems to be it can be udp or tcp

ChadSikorra commented 2 years ago

image Found it in source code. Seems to be it can be udp or tcp

Sorry, I lost track of this since I had marked the issue as closed!

I did not officially document the ability to use the client over TCP as I'm not sure I have it all implemented correctly. I think this is the document that officially describes how SNMP should work over TCP. I'd have to take a closer look at it to "officially" support it.

https://datatracker.ietf.org/doc/html/rfc6353