private void ReceiveCommand(byte[] recevedData) { ITransmittedObject command = Utilits.DeserializeFromByte <ITransmittedObject>(recevedData); if (command is NetworkAuthTransmitted) { string result = (command as NetworkAuthTransmitted).name; _authResultDelegate.Invoke(result != "error"); if (result == "error") { _errorsDelegate?.Invoke("Пользователь с таким ником уже подключен"); } return; } if (command is TransmittedInfoObject) { var obj = command as TransmittedInfoObject; byte[] data = new byte[obj.length]; int readed = _clientSocket.Receive(data); if (obj.type == TransmittedDataType.Bytes) { _receiveBytesDelegate?.Invoke(data); } else { _receiveObjectDelegate?.Invoke(Utilits.DeserializeFromByte <ITransmittedObject>(data)); } return; } _errorsDelegate?.Invoke("Приняты неизвестные данные"); }
private void ReceiveCommand(byte[] recevedData) { ITransmittedObject command = Utilits.DeserializeFromByte <ITransmittedObject>(recevedData); if (command is NetworkAuthTransmitted) { string name = (command as NetworkAuthTransmitted).name; byte[] data; if (_setNameDelegate.Invoke(name)) { userName = name; data = recevedData; } else { data = Utilits.SerializeToBytes(new NetworkAuthTransmitted("error")); } _clientSocket.Send(data); return; } if (command is TransmittedInfoObject) { TransmittedInfoObject obj = command as TransmittedInfoObject; byte[] data = new byte[obj.length]; int readed = _clientSocket.Receive(data); if (obj.to == "") { _sendToAllDelegate?.Invoke(recevedData, data); } else { _sendToUserDelegate?.Invoke(recevedData, data, obj.to); } } }
/// <summary> /// Send Object to all or client /// </summary> /// <param name="obj"></param> /// <param name="to"></param> /// <returns></returns> public bool SendObject(ITransmittedObject obj, string to = "") { if (!IsConnected) { _errorsDelegate?.Invoke("Подключение не открыто"); return(false); } byte[] data = Utilits.SerializeToBytes(obj); if (data == null) { _errorsDelegate?.Invoke("Ошибка сериализации данных\n" + Utilits.info); return(false); } TransmittedInfoObject header = new TransmittedInfoObject(to, data.Length, TransmittedDataType.TransmittedObject); _clientSocket.Send(Utilits.SerializeToBytes(header)); _clientSocket.Send(data); return(true); }