void Adafruit_WINC1500Client::copyFrom(const Adafruit_WINC1500Client& other) { _socket = other._socket; _flag = other._flag; _head = other._head; _tail = other._tail; if (_head > _tail) { memcpy(_buffer + _tail, other._buffer + _tail, (_head - _tail)); } for (int sock = 0; sock < TCP_SOCK_MAX; sock++) { if (WiFi._client[sock] == this) WiFi._client[sock] = 0; } if (_socket > -1) { WiFi._client[_socket] = this; // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_buffer); // Enable receive buffer: recv(_socket, _buffer, SOCKET_BUFFER_MTU, 0); } m2m_wifi_handle_events(NULL); }
/* Start Adafruit_WINC1500UDP socket, listening at local port PORT */ uint8_t Adafruit_WINC1500UDP::begin(uint16_t port, uint32_t multicastAddr) { struct sockaddr_in addr; uint32 u32EnableCallbacks = 0; _flag = 0; _head = 0; _tail = 0; _rcvSize = 0; _rcvPort = 0; _rcvIP = 0; _sndSize = 0; // Initialize socket address structure. addr.sin_family = AF_INET; addr.sin_port = _htons(port); addr.sin_addr.s_addr = 0; // Open TCP server socket. if ((_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return 0; } // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_recvBuffer); setsockopt(_socket, SOL_SOCKET, SO_SET_UDP_SEND_CALLBACK, &u32EnableCallbacks, 0); // Set multicast address option if a multicast address was specified. if (multicastAddr != 0) { multicastAddr = _htonl(multicastAddr); if (setsockopt(_socket, SOL_SOCKET, IP_ADD_MEMBERSHIP, &multicastAddr, sizeof(multicastAddr)) < 0) { // Failed to set the multicast address option. close(_socket); _socket = -1; return 0; } } // Bind socket: if (bind(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { close(_socket); _socket = -1; return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (!READY && millis() - start < 2000) { m2m_wifi_handle_events(NULL); } if (!READY) { close(_socket); _socket = -1; return 0; } _flag &= ~SOCKET_BUFFER_FLAG_BIND; return 1; }
int WiFiClient::connect(IPAddress ip, uint16_t port, uint8_t opt, const uint8_t *hostname) { struct sockaddr_in addr; // Initialize socket address structure: addr.sin_family = AF_INET; addr.sin_port = _htons(port); addr.sin_addr.s_addr = ip; // Create TCP socket: _flag = 0; _head = 0; _tail = 0; if ((_socket = socket(AF_INET, SOCK_STREAM, opt)) < 0) { return 0; } if (opt & SOCKET_FLAGS_SSL && hostname) { setsockopt(_socket, SOL_SSL_SOCKET, SO_SSL_SNI, hostname, m2m_strlen((uint8_t *)hostname)); } // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_buffer); // Connect to remote host: if (connectSocket(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { close(_socket); _socket = -1; return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (!IS_CONNECTED && millis() - start < 20000) { m2m_wifi_handle_events(NULL); } if (!IS_CONNECTED) { close(_socket); _socket = -1; return 0; } WiFi._client[_socket] = this; return 1; }
WiFiClient::WiFiClient(const WiFiClient& other) { _socket = other._socket; _flag = other._flag; _head = 0; _tail = 0; for (int sock = 0; sock < TCP_SOCK_MAX; sock++) { if (WiFi._client[sock] == this) WiFi._client[sock] = 0; } WiFi._client[_socket] = this; // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_buffer); // Enable receive buffer: recv(_socket, _buffer, SOCKET_BUFFER_MTU, 0); m2m_wifi_handle_events(NULL); }
WiFiClient::WiFiClient(uint8_t sock, uint8_t parentsock) { // Spawn connected TCP client from TCP server socket: _socket = sock; _flag = SOCKET_BUFFER_FLAG_CONNECTED; if (parentsock) { _flag |= (parentsock - 1) << SOCKET_BUFFER_FLAG_PARENT_SOCKET_POS; } _head = 0; _tail = 0; for (int sock = 0; sock < TCP_SOCK_MAX; sock++) { if (WiFi._client[sock] == this) WiFi._client[sock] = 0; } WiFi._client[_socket] = this; // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_buffer); // Enable receive buffer: recv(_socket, _buffer, SOCKET_BUFFER_MTU, 0); m2m_wifi_handle_events(NULL); }