Просмотр исходного кода

thread safe connection getter with method documentation

OlehKulykov 10 лет назад
Родитель
Сommit
c273eb356c

+ 58 - 0
contrib/objc/RWSSocketObjc.h

@@ -25,31 +25,89 @@
 
 @class RWSSocketObjc;
 
+/**
+ @brief Websocket delegate protocol.
+ All methods called from main thread.
+ */
 @protocol RWSSocketObjcDelegate <NSObject>
 
 @required
+/**
+ @brief Websocket connected.
+ @detailed Connection extablished and handshake done. Ready to send and receive.
+ @param socket Socket object.
+ */
 - (void) onRWSSocketConnected:(nonnull RWSSocketObjc *) socket;
 
+
+/**
+ @brief Socket client disconnected.
+ @detailed Internal socket already freed and dealocated. Reconnect once again.
+ @param socket Socket object.
+ @param error Disconnect error.
+ */
 - (void) onRWSSocketDisconnected:(nonnull RWSSocketObjc *) socket
 					   withError:(nullable NSError *) error;
 
 @optional
+/**
+ @brief Socket received non empty text.
+ @param socket Socket object.
+ @param text Non empty text.
+ */
 - (void) onRWSSocket:(nonnull RWSSocketObjc *) socket receivedText:(nonnull NSString *) text;
 
+
+
+/**
+ @brief Socket received non empty binary data.
+ @param socket Socket object.
+ @param text Non binary data.
+ */
 - (void) onRWSSocket:(nonnull RWSSocketObjc *) socket receivedData:(nonnull NSData *) data;
 
 @end
 
+
+/**
+ @brief Objective-C web socket wrapper.
+ */
 @interface RWSSocketObjc : NSObject
 
+
+/**
+ @brief Read/write delegate object.
+ */
 @property (nonatomic, weak) id<RWSSocketObjcDelegate> delegate;
 
+
+/**
+ @brief Check websocket exists and connected.
+ */
 @property (nonatomic, readonly) BOOL isConnected;
 
+
+/**
+ @brief Send text to connected websocket.
+ @param text Text for sending.
+ @return YES - socket exist, connected and text not empty, othervice NO.
+ */
 - (BOOL) sendText:(nonnull NSString *) text;
 
+
+/**
+ @brief Connect to server.
+ @detailed Remove prev. socket object and make new connection.
+ @return YES - started connection sequence, othervice NO.
+ */
 - (BOOL) connect;
 
+
+/**
+ @brief Initialize web socket with destination url.
+ @param url Connection URL. Should not be nil.
+ @return Websocket object or nil on error.
+ */
 - (nullable id) initWithURL:(nonnull NSURL *) url;
 
 

+ 2 - 3
contrib/objc/RWSSocketObjc.m

@@ -103,7 +103,6 @@ static void onRWSSocketObjcRecvdBin(rws_socket socket, const void * data, const
 {
 	if (_socket)
 	{
-		rws_socket_set_on_disconnected(_socket, NULL);
 		rws_socket_set_user_object(_socket, NULL);
 		rws_socket_disconnect_and_release(_socket);
 		_socket = NULL;
@@ -112,8 +111,8 @@ static void onRWSSocketObjcRecvdBin(rws_socket socket, const void * data, const
 
 - (BOOL) connect
 {
-	if (!_url) return NO;
 	[self cleanup];
+	if (!_url) return NO;
 
 	_socket = rws_socket_create();
 	rws_socket_set_scheme(_socket, [[_url scheme] UTF8String]);
@@ -131,7 +130,7 @@ static void onRWSSocketObjcRecvdBin(rws_socket socket, const void * data, const
 	rws_socket_set_on_received_text(_socket, &onRWSSocketObjcRecvdText);
 	rws_socket_set_on_received_bin(_socket, &onRWSSocketObjcRecvdBin);
 
-	return (rws_socket_connect(_socket)) ? YES : NO;
+	return rws_socket_connect(_socket) ? YES : NO;
 }
 
 - (BOOL) isConnected

+ 2 - 0
librws.h

@@ -291,6 +291,7 @@ RWS_API(void) rws_socket_disconnect_and_release(rws_socket socket);
 
 /**
  @brief Check is socket has connection to host and handshake(sucessfully done).
+ @detailed Thread safe getter.
  @param socket Socket object.
  @return trw_true - connected to host and handshacked, otherwice rws_false.
  */
@@ -299,6 +300,7 @@ RWS_API(rws_bool) rws_socket_is_connected(rws_socket socket);
 
 /**
  @brief Send text to connect socket.
+ @detailed Thread safe method.
  @param socket Socket object.
  @param text Text string for sending.
  @return rws_true - socket and text exists and placed to send queue, otherwice rws_false.

+ 6 - 1
src/socketpub.c

@@ -303,6 +303,11 @@ void rws_socket_set_on_received_bin(rws_socket socket, rws_on_socket_recvd_bin c
 rws_bool rws_socket_is_connected(rws_socket socket)
 {
 	_rws_socket * s = (_rws_socket *)socket;
-	return s ? s->is_connected : rws_false;
+	rws_bool r = rws_false;
+	if (!s) return r;
+	rws_mutex_lock(s->send_mutex);
+	r = s->is_connected;
+	rws_mutex_unlock(s->send_mutex);
+	return r;
 }
 

BIN
test/librws_test.xcodeproj/project.xcworkspace/xcuserdata/residentevil.xcuserdatad/UserInterfaceState.xcuserstate