Пример #1
0
        private void asyncBeginAccept(IAsyncResult iAsyncResult)
        {
            try {
                this.Id++;
                Task newTask = Task.Factory.StartNew(() => {
                    try {
                        TcpClient tcpClient = tcpListener.EndAcceptTcpClient(iAsyncResult);
                        tcpClient.ReceiveTimeout = 60000; //1 min
                        tcpClient.SendTimeout = 60000; //1 min

                        TcpTracker tcpTracker = new TcpTracker() {
                            Id = this.Id,
                            Imei = "",
                            TcpClient = tcpClient,
                            DataIn = "",
                            DataOut = "",
                            DateTime = DateTime.Now
                        };

                        while (!this.TcpClients.ContainsKey(tcpTracker.Id)) {
                            this.TcpClients.TryAdd(tcpTracker.Id, tcpTracker);
                        }

                        if (tcpClient.Connected && tcpClient != null) {
                            this.Communicate(tcpTracker);
                        }

                        while (this.TcpClients.ContainsKey(tcpTracker.Id)) {
                            this.TcpClients.TryRemove(tcpTracker.Id, out tcpTracker);
                        }

                    } catch (Exception exception) {
                        if (this.IsActivated) {
                            triggerEvent(new Log(exception.Message, LogType.SERVER_BEGINACCEPT));
                        }
                    }
                }, TaskCreationOptions.LongRunning);

                tcpListener.BeginAcceptTcpClient(new AsyncCallback(asyncBeginAccept), tcpListener);
            } catch (Exception exception) {
                triggerEvent(new Log(exception.Message, LogType.SERVER_BEGINACCEPT));
            }
        }
Пример #2
0
        protected override void Communicate(TcpTracker tcpTracker)
        {
            Gm gm = null;
            Byte[] bufferIn = new Byte[256];
            TcpClient tcpClient = tcpTracker.TcpClient;
            String dataOut = String.Empty;

            try {
                //do {
                NetworkStream networkStream = tcpClient.GetStream();

                //------------------------------------------------Receive message
                //Communication 1
                Array.Clear(bufferIn, 0, bufferIn.Length);
                Int32 count = networkStream.Read(bufferIn, 0, bufferIn.Length);
                base.PacketReceived++;
                base.ByteReceived += count;
                Byte[] incomingBytes = new Byte[count];
                Array.Copy(bufferIn, incomingBytes, count);

                if (incomingBytes[0] != 0x00 || incomingBytes[1] != 0x0f) {
                    return;
                    //throw new GmException(GmException.UNKNOWN_PROTOCOL, "");
                }

                gm = new Gm();
                gm.Unit = ASCIIEncoding.UTF8.GetString(incomingBytes, 2, incomingBytes.Length - 2);
                send(networkStream, new byte[] { 0x01 });
                dataOut = "0x01";

                //Communication 2
                Array.Clear(bufferIn, 0, bufferIn.Length);
                count = networkStream.Read(bufferIn, 0, bufferIn.Length);
                base.PacketReceived++;
                base.ByteReceived += count;
                incomingBytes = new Byte[count];
                Array.Copy(bufferIn, incomingBytes, count);

                if (!Teltonika.ParseGm(incomingBytes, ref gm)) {
                    return;
                }

                //Communication 3
                byte[] response = new byte[] { (byte)gm.RecordCount, 0x00, 0x00, 0x00 };
                send(networkStream, response);
                dataOut = "0x000000" + gm.RecordCount.ToString();

                //------------------------------------------------Send message if theres any
                //if (this.BufferOut != null) {
                //    if (this.BufferOut.ContainsKey(gm.Unit)) {
                //        if (this.BufferOut.ContainsKey(gm.Unit)) {
                //            String[] command;
                //            if (this.BufferOut.TryRemove(gm.Unit, out command)) {
                //                dataOut = Teltonika.GenerateCommand(command, gm.Identifier);
                //                send(networkStream, ASCIIEncoding.UTF8.GetBytes(dataOut));
                //            }
                //        }
                //    }
                //}
                //base.triggerEvent(new Log(BitConverter.ToString(incomingBytes).Replace("-", ""), LogType.FM1100));

                base.triggerDataReceived(gm);
                tcpTracker.Imei = gm.Unit;
                tcpTracker.DataIn = gm.Raw;
                tcpTracker.DataOut = dataOut;
                tcpTracker.DateTime = DateTime.Now;

                this.TcpClients.TrackersCount = countTrackers(this.TcpClients);
                //} while (tcpClient.Connected);

            } catch (GmException gmException) {
                triggerEvent(new Log(gmException.Imei + " : " + gmException.Description, LogType.FM1100));
            } catch (Exception exception) {
                triggerEvent(new Log(exception.Message, LogType.SERVER));
            } finally {
                if (!String.IsNullOrEmpty(gm.Unit)) {
                    tcpTracker.Imei = "";
                    tcpTracker.DataIn = "";
                    tcpTracker.DataOut = "";
                    tcpTracker.DateTime = DateTime.Now;
                    this.TcpClients.TrackersCount = countTrackers(this.TcpClients);
                }
                tcpClient.Close();
            }
        }
Пример #3
0
 protected virtual void Communicate(TcpTracker tcpTracker)
 {
     throw new NotImplementedException();
 }