Esempio n. 1
0
        public bool Write(Frame frame) {
            if (frame != null) {
                byte[] frameByte;
                MemoryStream frameStream = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(frameStream, frame);
                frameByte = frameStream.ToArray();
                frameByte = EncodeHelper.Encode(frameStream.ToArray());

                List<byte> frameByteList = new List<byte>(frameByte);
                frameByteList.Add(255);
                frameByte = frameByteList.ToArray();
                try {
                    this.port.Write(frameByte, 0, frameByte.Length);
                } catch {
                    return false;
                }
                return true;
            }
            return false;
        }
Esempio n. 2
0
        public void SendEmail(string to, string theme, string content, bool confirmation = false) {
            Email message = new Email(to, settings.Email, theme, content, confirmation);
            string emailAddress = message.To;

            if (ARPDictionary.ContainsKey(emailAddress)) {
                int receiverId = ARPDictionary[emailAddress];
                Frame frame = new Frame(settings.Id, receiverId, FrameType.MESSAGE, message);
                sendQueue.Enqueue(frame);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Тред проверки активности сети
        /// </summary>
        private void Heartbeat() {
            bool lastFail = true;
            int failTime = 0;
            while (true) {
                Frame heartBeatFrame = new Frame(settings.Id, settings.Id, FrameType.HEART_BEAT);
                bool writeStatus = outgoingPort.Write(heartBeatFrame);

                if (this.heartBeatWait + 1 > settings.HeartBeatLossLimit || !writeStatus) {
                    this.messageReceiveHandler.onHeartBeatFail();
                    if (!lastFail) {
                        this.messageReceiveHandler.addToLog("Heartbeat fail");
                    }
                    lastFail = true;
                    failTime++;
                    if (failTime > 5) {
                        if (settings.HeartBeatInterval != 2000) {
                            this.messageReceiveHandler.addToLog("Heartbeat rate decrease");
                        }
                        settings.HeartBeatInterval = 2000;
                    }
                } else {
                    if (lastFail) {
                        this.messageReceiveHandler.onHeartBeatSuccess();
                        this.heartBeatWait = 0;
                        failTime = 0;
                        lastFail = false;
                        settings.RestoreHeartBeatInterval();
                        this.messageReceiveHandler.addToLog("Heartbeat rate restore");
                    }
                    this.heartBeatWait++;
                }
                Thread.Sleep(settings.HeartBeatInterval);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Тред чтения
        /// </summary>
        private void Reading() {
            while (true) {
                Frame frame = incomingPort.Read();
                if (frame != null) {
                    if ((frame.Type == FrameType.HEART_BEAT && frame.ReciverID == settings.Id) || frame.Type != FrameType.HEART_BEAT) {
                        messageReceiveHandler.addToLog("From: " + frame.ReciverID + ", To: " + frame.SenderID + ",  Type: " + frame.Type);
                    }
                } else {
                    messageReceiveHandler.addToLog("Bad package. Try RET");
                    continue;
                }

                if (Settings.instanceOf().Id != frame.ReciverID) {
                    if (frame.Type == FrameType.ACK || frame.Type == FrameType.RET || frame.Type == FrameType.MESSAGE || frame.Type == FrameType.HEART_BEAT) {
                        if (frame.Type == FrameType.MESSAGE && frame.SenderID == settings.Id) {
                            messageReceiveHandler.addToLog("Адресат не доступен, сообщение удалено из канала");
                        } else {
                            transitQueue.Enqueue(frame);
                        }
                    } else {
                        switch (frame.Type) {
                            case FrameType.BREAK_RING:
                                break;
                            case FrameType.CLIENT_LIST:
                                ClientList clientList = (ClientList)frame.Data;
                                clientList.AddInfo(settings.Email, settings.Id);
                                frame.Data = clientList;
                                transitQueue.Enqueue(frame);
                                break;
                        }

                    }
                } else {
                    switch (frame.Type) {
                        case FrameType.MESSAGE:
                            Frame outFrame = new Frame(settings.Id, frame.SenderID, FrameType.ACK);
                            transitQueue.Enqueue(outFrame);
                            Email message = (Email)frame.Data;
                            this.messageReceiveHandler.onEmailReceive(message);
                            break;
                        case FrameType.CLIENT_LIST:
                            ClientList clientList = (ClientList)frame.Data;
                            this.ARPDictionary = new ConcurrentDictionary<string, int>(clientList.List);
                            this.messageReceiveHandler.onClientListReceive(clientList);
                            break;
                        case FrameType.ACK:
                            waitACKAfterSendTimer.Stop();
                            waitACKAfterSendTimer.Reset();
                            break;
                        case FrameType.RET:
                            RET = true;
                            break;
                        case FrameType.HEART_BEAT:
                            if (this.heartBeatWait > 0) {
                                this.heartBeatWait--;
                            }
                            break;
                        case FrameType.BREAK_RING:
                            this.messageReceiveHandler.onDisconnect();
                            this.Disconnect();
                            break;

                    }
                }
            }
        }
Esempio n. 5
0
 public void InitGettingListClients() {
     ClientList clientList = new ClientList();
     clientList.AddInfo(settings.Email, settings.Id);
     Frame frame = new Frame(settings.Id, settings.Id, FrameType.CLIENT_LIST, clientList);
     transitQueue.Enqueue(frame);
 }