/// <summary> /// Send a buffer model to all clients /// </summary> /// <param name="message"></param> public void SendDataObjectToAll(DataByteType type, DataBufferModel message) { foreach (Socket s in clients.Keys) { try { SendDataObjectToSocket(type, s, message); } catch { } } }
/// <summary> /// Function that checks for the client you selected and then according to that sends the object. /// </summary> /// <param name="type"></param> /// <param name="message"></param> public void SendDataObjectToSelectedClient(DataByteType type, DataBufferModel message) { if (allClientsSelected) { SendDataObjectToAll(type, message); } else { SendDataObjectToSocket(type, GetSocketByClient(SelectedClient), message); } }
/// <summary> /// Sends a text message to the specified /// socket. /// </summary> /// <param name="s">The socket.</param> /// <param name="message">The message.</param> public void SendDataObjectToSocket(DataByteType type, Socket s, DataBufferModel message) { BConsole.WriteLine("Sending data with id: " + message.DataId.ToString()); byte[] lengthByteArray = BitConverter.GetBytes(message.SeriesLength); foreach (KeyValuePair <int, byte[]> item in message.BufferedData) { byte[] seriesByteArray = BitConverter.GetBytes(item.Key); byte[] sendArray = new byte[] { (byte)type, lengthByteArray[0], lengthByteArray[1], seriesByteArray[0], seriesByteArray[1] }; sendArray = sendArray.Concat(message.DataId.ToByteArray()).Concat(item.Value).ToArray(); SendBytesToSocket(s, sendArray); } }
/// <summary> /// Function that handles the converted data /// </summary> /// <param name="dObj"></param> /// <param name="client"></param> /// <param name="type"></param> /// <returns></returns> private bool HandleIncomingData(TransferCommandObject dObj, Client client, DataByteType type) { MessageReceived?.Invoke(client, dObj, type); return(true); }
/// <summary> /// The async callback of receivedata /// </summary> /// <param name="result"></param> private void ReceiveData(IAsyncResult result) { try { Socket clientSocket = (Socket)result.AsyncState; Client client = GetClientBySocket(clientSocket); if (client == null) { return; } int bytesReceived = clientSocket.EndReceive(result); DataByteType type = (DataByteType)client.Data[SharedProperties.TypeByte]; if (bytesReceived == 0) { CloseSocket(clientSocket); serverSocket.BeginAccept(new AsyncCallback(HandleIncomingConnection), serverSocket); } else if (Enum.IsDefined(typeof(DataByteType), (DataByteType)client.Data[SharedProperties.TypeByte])) { int length = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.LengthByte1], client.Data[SharedProperties.LengthByte2], 0, 0 }, 0); int series = BitConverter.ToInt32(new byte[] { client.Data[SharedProperties.SeriesByte1], client.Data[SharedProperties.SeriesByte2], 0, 0 }, 0); Guid guid = new Guid(client.Data.SubArray(SharedProperties.GuidStartByte, 16)); DataBufferModel buffer = Buffers.FirstOrDefault(n => n.DataId == guid); if (buffer != null) { buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.LatestSeries = series; } else { buffer = new DataBufferModel(); buffer.BufferedData.Add(series, client.Data.SubArray(SharedProperties.HeaderByteSize, SharedProperties.DataLength)); buffer.DataId = guid; buffer.SeriesLength = length; buffer.LatestSeries = series; Buffers.Add(buffer); } BConsole.WriteLine($"Received data with id: {guid.ToString()}"); if (buffer.BufferedData.Count == buffer.SeriesLength) { if (HandleIncomingData(ClientServerPipeline.BufferDeserialize(buffer), client, type)) { Buffers.Remove(buffer); } } } clientSocket.BeginReceive(client.Data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientSocket); } catch (SocketException) { Socket clientSocket = (Socket)result.AsyncState; Client client = GetClientBySocket(clientSocket); KickClient(client); } catch (Exception e) { BConsole.WriteLine("Recieve error occured: " + e.Message, ConsoleColor.Red); } }