public void SetClient(ClientData <T> newClient) { clientData = newClient; }
/// <summary> /// The work done on the receive loop. It assumes we are receiving MessageData types unless /// a MessageData received indicates a file to be received. Then it switches over to receiving /// byte[]. /// </summary> /// <param name="client">The client data is received from</param> private void ClientReceive(Client client) { if ((client == null) || (client.ClientSocket == null)) { return; } ReceiveData rcvData = new ReceiveData(); try { client.ClearData(); var res = TcpLibExtensions.ReceiveAsync(client.ClientSocket, client.ClientData(), 0, client.DataSize, SocketFlags.None, client.CancelSource.Token); if (res.IsFaulted || (res.Result == null) || res.Result.Failure) { // Just close the socket. Look into trying to reconnect Console.WriteLine("Receive Fault. Closing socket {0} to client.", client.ClientSocket.Handle); ClientStore.RemoveClient((long)client.ClientSocket.Handle); } else { System.Diagnostics.Debug.WriteLine("Receiving Data From Client: {0}", client.ClientSocket.Handle); try { MessageData value = null; if (receivingFile) { value = fileMsg; value.message = client.ClientData(); value.length = res.Result.Value; rcvData.clientData = fileMsg; totalRcv += res.Result.Value; System.Diagnostics.Debug.WriteLine("Received: Size:[{0}] - [{1}] out of [{2}]", res.Result.Value, totalRcv, fileSize); } else { value = ClientData <MessageData> .DeserializeFromByteArray <MessageData>(client.ClientData()); rcvData.clientData = value; } rcvData.clientHandle = (long)client.ClientSocket.Handle; if (value != null) { // This indicates a response from the server to a client of which there could be many if (value.response) { // Client received OnClientDataReceived(new AsyncCompletedEventArgs(null, false, rcvData)); } else { if (rcvData.clientData.id == 100) { if (!receivingFile) { receivingFile = true; fileSize = rcvData.clientData.length; fileMsg.handle = rcvData.clientData.handle; fileMsg.id = rcvData.clientData.id; fileMsg.name = rcvData.clientData.name; fileMsg.response = rcvData.clientData.response; } else if (totalRcv >= fileSize) { receivingFile = false; totalRcv = 0; Console.WriteLine("Received all file."); } } // Server received OnServerDataReceived(new AsyncCompletedEventArgs(null, false, rcvData)); } } else { // Send null message to client and server so they can detect disconnection OnClientDataReceived(new AsyncCompletedEventArgs(null, false, rcvData)); OnServerDataReceived(new AsyncCompletedEventArgs(null, false, rcvData)); } } catch (Exception e) { Console.WriteLine(e.Message); } } } catch (TaskCanceledException tc) { looper.LoopDone = true; System.Diagnostics.Debug.WriteLine("receive Task Cancelled: " + tc.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Receive Exception: " + ex.Message); } }
public ServiceController(ClientData <T> client) { clientData = client; }