public DeliveryWorker(Configuration configuration, NSettings settings, NStorage storage, BackgroundWorker backgroundWorker) { try { this.NewMessages = 0; int i = 0; mConfiguration = configuration; mSettings = settings; mStorage = storage; Boolean updateIndex = false; NTransport mTransport = new NTransport(this.mConfiguration); backgroundWorker.ReportProgress(0); #region RECEIVING //Receiving Headers List <NMessageHeader> messageHeaders = mTransport.ReciveHeaders(); if (messageHeaders != null && !mCancel) { i = 0; foreach (NMessageHeader messageHeader in messageHeaders) { if (!mStorage.InboxIndex.ExistsID(messageHeader.MsgID) && !mStorage.ArchiveIndex.ExistsID(messageHeader.MsgID)) { //skip already downloaded NMessage message = mTransport.ReciveMessage(messageHeader); if (message != null) { mStorage.SaveInbox(message); } updateIndex = true; NewMessages++; } backgroundWorker.ReportProgress(100 * i / (messageHeaders.Count)); i++; } } if (updateIndex) { mStorage.UpdateIndex(NStorageFolder.Inbox); } #endregion #region SENDING //Sending updateIndex = false; string[] mFileList = Directory.GetFiles(mStorage.OutboxPath, "*.xml"); i = 1; if (mFileList.Length > 0) { backgroundWorker.ReportProgress(100 * i / (mFileList.Length + 1)); foreach (String file in mFileList) { if (mCancel) { break; } NMessage message = new NMessage(); using (StreamReader sr = new StreamReader(file)) { if (File.Exists(file)) { XmlSerializer des = new XmlSerializer(typeof(NMessage)); message = (NMessage)des.Deserialize(new System.Xml.XmlTextReader(sr)); sr.Close(); } } if (message != null) { NResultSend resultSend = mTransport.SendMessage(message); if (resultSend.Success) { if (File.Exists(storage.SentPath + file)) { File.Delete(storage.SentPath + file); } File.Move(file, storage.SentPath + Path.GetFileName(file)); updateIndex = true; } ; } i++; backgroundWorker.ReportProgress(100 * i / (mFileList.Length + 1)); } if (updateIndex) { mStorage.UpdateIndex(NStorageFolder.Outbox); mStorage.UpdateIndex(NStorageFolder.Sent); } } #endregion } catch (Exception) { #if (DEBUG) throw; #endif } backgroundWorker.ReportProgress(100); }
public NResultSend SendMessage(NMessage message) { NResultSend resultSend = new NResultSend(); resultSend.Success = false; //$username, $password, $recipients, $compression, $bytes, $type, $format try { String recipients = ""; foreach (String recipient in message.Header.To) { recipients += recipient + ";"; } //XMLSendMessage($username, $password, $recipients, $compression, $bytes, $type, $format) Hashtable parameters = new Hashtable(); parameters.Add("username", mUserConfiguration.Username); parameters.Add("password", mUserConfiguration.Password); parameters.Add("recipients", recipients); parameters.Add("compression", "none"); parameters.Add("bytes", 0); parameters.Add("type", "XML"); parameters.Add("format", "amr"); try { String boundary = Guid.NewGuid().ToString(); WebRequest requestSendMessage = WebRequest.Create(mUserConfiguration.Url + BulildWebRequestQuery("XMLSendMessage", parameters)); requestSendMessage.ContentType = "multipart/form-data; boundary=" + boundary; requestSendMessage.Method = "POST"; //encode header String postHeader = ""; postHeader += "\r\n" + @"--" + boundary + "\r\n"; postHeader += "Content-Disposition: form-data; name=\"uploadFile1\"; filename=\"uploadFile1\"\r\n"; postHeader += "Content-Type: application/octet-stream\r\n\r\n"; byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n" + @"--" + boundary + @"--" + "\r\n"); //read in the file as a stream long contentLength = postHeaderBytes.Length + message.Body.AttachementBodys[0].Data.Length + boundaryBytes.Length; requestSendMessage.ContentLength = contentLength; Stream requestStream = requestSendMessage.GetRequestStream(); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); requestStream.Write(message.Body.AttachementBodys[0].Data, 0, message.Body.AttachementBodys[0].Data.Length); requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); //requestStream.Close(); HttpWebResponse responseSendMessage = (HttpWebResponse)requestSendMessage.GetResponse(); Stream dataStream = responseSendMessage.GetResponseStream(); XmlSerializer des = new XmlSerializer(typeof(NResultSend)); resultSend = (NResultSend)des.Deserialize(new System.Xml.XmlTextReader(dataStream)); dataStream.Close(); } catch (Exception ex) { #if (DEBUG) throw; #endif } } catch (Exception) { #if (DEBUG) throw; #endif } return(resultSend); }