/// <summary> /// Wait for received data asynchronously. Used by <see cref="LoopListening(TcpClient)"/>. /// </summary> /// <param name="client">The socket to listen to.</param> /// <returns>The received <see cref="NetObject"/></returns> protected async Task <NetObject> ReceiveDataAsync(Socket client) { NetObject netobj = null; await Task.Run(() => { byte[] sizeInfo = new byte[sizeof(int)]; int totalread = 0, currentread = 0; currentread = totalread = client.Receive(sizeInfo); while (totalread < sizeInfo.Length && currentread > 0) { currentread = client.Receive(sizeInfo, totalread, //offset into the buffer sizeInfo.Length - totalread, //max amount to read SocketFlags.None); totalread += currentread; } int messageSize = 0; //could optionally call BitConverter.ToInt32(sizeinfo, 0); messageSize |= sizeInfo[0]; messageSize |= (((int)sizeInfo[1]) << 8); messageSize |= (((int)sizeInfo[2]) << 16); messageSize |= (((int)sizeInfo[3]) << 24); byte[] data = new byte[messageSize]; //read the first chunk of data totalread = 0; currentread = totalread = client.Receive(data, totalread, //offset into the buffer data.Length - totalread, //max amount to read SocketFlags.None); //if we didn't get the entire message, read some more until we do while (totalread < messageSize && currentread > 0) { currentread = client.Receive(data, totalread, //offset into the buffer data.Length - totalread, //max amount to read SocketFlags.None); totalread += currentread; } LoopResetEvent.Set(); try { netobj = NetObject.Receive(NetData <NetDummy> .Encoding.GetString(NetData <NetDummy> .Decompress(data)), client); } catch (Exception e) { Debug.LogError("Transmission error or decompressing error."); netobj = new NetDummy(); } }).ConfigureAwait(true); return(netobj); }
/// <summary> /// Reads all incoming data from a client while <see cref="Running"/> is set to true. /// <br>Adds received data to <see cref="PendingData"/></br> /// </summary> /// <param name="client">The connected client to listen to.</param> protected void LoopListening(TcpClient client) { Task.Run(async() => { while (this.Running) { NetObject obj = new NetDummy();; try { obj = await NetData <NetDummy> .ReceiveDataAsync(client.Client).ConfigureAwait(true); } catch (NullReferenceException ne) { //Debug.LogError("Unknown error while receiving data from client: " + ne); break; } catch (ObjectDisposedException obje) { //Debug.LogError("Unknown error while receiving data from client: " + obje); break; } catch (SocketException se) { //Debug.LogError("Unknown error while receiving data from client: " + se); break; } catch (System.IO.InvalidDataException de) { } catch (Exception e) { Debug.LogError("Unknown error while receiving data from client: " + e); } if (obj is NetDummy) { continue; } else if (client != null && this.Running) { OnClientDataReceived?.Invoke(client, obj); lock (PendingDataLocker) { PendingData.Add(new PendingData(client, obj)); } } /*try * { * NetObject obj = await NetData<NetDummy>.ReceiveDataAsync(client.Client).ConfigureAwait(true); * * if (obj is NetDummy) * { * continue; * } * * if (client != null && this.Running) * { * lock (PendingDataLocker) * { * OnClientDataReceived?.Invoke(client, obj); * PendingData.Add(new PendingData(client, obj)); * } * } * } * catch(SocketException skte) {} * catch(ObjectDisposedException obje) {} * catch(Exception e) * { * Debug.LogError("Error on receiving infos from client: " + e); * break; * }*/ } }); }