Merge branch 'master' of https://github.com/mobratil/sockets-for-cordova
This commit is contained in:
commit
5f5b0264a1
25
README.md
25
README.md
@ -4,6 +4,8 @@ This Cordova plugin provides JavaScript API, that allows you to communicate with
|
||||
|
||||
Currently we support these platforms: iOS, Android, WP8.
|
||||
|
||||
You can also get information about this plugin from our blog post http://www.blocshop.cz/blog/?p=6
|
||||
|
||||
## Installation
|
||||
|
||||
Install this plugin simply by:
|
||||
@ -131,3 +133,26 @@ Closes the connection. `onClose` event handler is called when connection is succ
|
||||
| ----------- |-----------------------------|--------------|
|
||||
| `onSuccess` | `() => void` | Success callback, called after connection is successfully closed. `onClose` event handler is called before that callback. (optional)|
|
||||
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during this procedure. (optional)|
|
||||
|
||||
## BSD License
|
||||
Copyright (c) 2015, Blocshop s.r.o.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by the Blocshop s.r.o..
|
||||
4. Neither the name of the Blocshop s.r.o. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY Blocshop s.r.o. ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <math.h>
|
||||
#import "SocketAdapter.h"
|
||||
|
||||
CFReadStreamRef readStream;
|
||||
@ -28,6 +29,8 @@ NSOutputStream *outputStream;
|
||||
|
||||
BOOL wasOpenned = FALSE;
|
||||
|
||||
int const WRITE_BUFFER_SIZE = 10 * 1024;
|
||||
|
||||
@implementation SocketAdapter
|
||||
|
||||
- (void)open:(NSString *)host port:(NSNumber*)port {
|
||||
@ -186,14 +189,27 @@ BOOL wasOpenned = FALSE;
|
||||
}
|
||||
|
||||
- (void)write:(NSArray *)dataArray {
|
||||
uint8_t buf[dataArray.count];
|
||||
for (int i = 0; i < dataArray.count; i++) {
|
||||
buf[i] = (unsigned char)[[dataArray objectAtIndex:i] integerValue];
|
||||
int numberOfBatches = ceil((float)dataArray.count / (float)WRITE_BUFFER_SIZE);
|
||||
for (int i = 0; i < (numberOfBatches - 1); i++) {
|
||||
[self writeSubarray:dataArray offset:i * WRITE_BUFFER_SIZE length:WRITE_BUFFER_SIZE];
|
||||
}
|
||||
NSInteger bytesWritten = [outputStream write:buf maxLength:dataArray.count];
|
||||
int lastBatchPosition = (numberOfBatches - 1) * WRITE_BUFFER_SIZE;
|
||||
[self writeSubarray:dataArray offset:lastBatchPosition length:(dataArray.count - lastBatchPosition)];
|
||||
}
|
||||
|
||||
- (void)writeSubarray:(NSArray *)dataArray offset:(long)offset length:(long)length {
|
||||
uint8_t buf[length];
|
||||
for (long i = 0; i < length; i++) {
|
||||
unsigned char byte = (unsigned char)[[dataArray objectAtIndex:(offset + i)] integerValue];
|
||||
buf[i] = byte;
|
||||
}
|
||||
NSInteger bytesWritten = [outputStream write:buf maxLength:length];
|
||||
if (bytesWritten == -1) {
|
||||
@throw [NSException exceptionWithName:@"SocketException" reason:[outputStream.streamError localizedDescription] userInfo:nil];
|
||||
}
|
||||
if (bytesWritten != length) {
|
||||
[self writeSubarray:dataArray offset:(offset + bytesWritten) length:(length - bytesWritten)];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
@ -206,4 +222,4 @@ BOOL wasOpenned = FALSE;
|
||||
[self closeInputStream];
|
||||
}
|
||||
|
||||
@end
|
||||
@end
|
||||
|
Loading…
Reference in New Issue
Block a user