Пример #1
0
        public void SingleServerService(Socket server)
        {
            string ip = ((IPEndPoint)server.RemoteEndPoint).Address.ToString();
            byte[] bytes = new byte[1024 * 16];
            int len = 0;
            try
            {
                while (true)
                {
                    len = server.Receive(bytes);
                    if (len < 1)
                    {
                        PostLog("Connection to DTU (" + ip + ") is broken.", Brushes.Red);
                        break;
                    }
                    else
                    {
                        SentReceivedItem sri = null;
                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            sri = new SentReceivedItem();
                        }, null);
                        byte[] ba = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            ba[i] = bytes[i];
                        }

                        string rbrs = "";
                        string rbs = "";
                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            sri.Index = (SentReceivedOc.Count + 1).ToString();
                            sri.ReceivedBytes = ba;
                            rbrs = sri.ReceivedBytesRawString;
                            rbs = sri.ReceivedBytesString;
                        }, null);

                        PostLog("Received Raw : " + rbrs);
                        PostLog("Received : " + rbs);

                        if (DoSendOrNot == true)
                        {
                            bytes = Encoding.UTF8.GetBytes("Response : " + rbs);
                            PostLog("Trying sending response : Response " + rbs);
                            server.Send(bytes);
                            PostLog("Sent.");
                            Dispatcher.Invoke((ThreadStart)delegate()
                            {
                                sri.Sent = "Response : " + rbs;
                            }, null);
                        }

                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            SentReceivedOc.Add(sri);
                        }, null);
                    }
                }
            }
            catch (Exception ex)
            {
                PostLog(ex.Message);
            }
            PostLog("Finalize connection to (" + ip + ")...");
            Helper.SafeCloseSocket(server);
            PostLog("Done.");
        }
Пример #2
0
        public void ClientService(CancellationTokenSource cts, Socket soc)
        {
            string ip = ((IPEndPoint)soc.RemoteEndPoint).Address.ToString();
            byte[] bytes = new byte[Consts.SOCKET_RECEIVING_BUFFER_LENGTH];
            int len = 0;
            try
            {
                soc.Send(Encoding.UTF8.GetBytes(DTUID.Trim()));
                while (true)
                {
                    len = soc.Receive(bytes);
                    if (len < 1)
                    {
                        PostLog("Connection to DTU (" + ip + ") is broken.", Brushes.Red);
                        break;
                    }
                    else
                    {
                        SentReceivedItem sri = null;
                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            sri = new SentReceivedItem();
                        }, null);

                        byte[] ba = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            ba[i] = bytes[i];
                        }

                        string rbrs = "";
                        string rbs = "";
                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            sri.Index = (SentReceivedOc.Count + 1).ToString();
                            sri.ReceivedBytes = ba;
                            rbrs = sri.ReceivedBytesRawString;
                            rbs = sri.ReceivedBytesString;
                        }, null);

                        PostLog("Received Raw : " + rbrs);
                        PostLog("Received : " + rbs);

                        bytes = Encoding.UTF8.GetBytes("Response : " + rbs);
                        PostLog("Trying sending response : Response : " + rbs);
                        soc.Send(bytes);
                        PostLog("Sent.");

                        Dispatcher.Invoke((ThreadStart)delegate()
                        {
                            sri.Sent = "Response : " + rbs;
                            SentReceivedOc.Add(sri);
                        }, null);
                    }
                }
            }
            catch (Exception ex)
            {
                PostLog(ex.Message);
            }
            PostLog("Start shutdowning...");
            try
            {
                soc.Shutdown(SocketShutdown.Both);
                PostLog("Shutdown.");
            }
            catch (Exception ex)
            {
                PostLog("Exception when shutdowning : " + ex.Message, Brushes.Red);
            }
            PostLog("Start disconnecting...");
            try
            {
                soc.Disconnect(false);
                PostLog("Disconnected.");
            }
            catch (Exception ex)
            {
                PostLog("Exception when disconnecting : " + ex.Message, Brushes.Red);
            }
            PostLog("Start closing...");
            try
            {
                soc.Close();
                PostLog("Closed.");
            }
            catch (Exception ex)
            {
                PostLog("Exception when closing : " + ex.Message, Brushes.Red);
            }
            PostLog("Start disposing...");
            try
            {
                soc.Dispose();
            }
            catch (Exception ex)
            {
                PostLog("Exception when disposing : " + ex.Message, Brushes.Red);
            }
            PostLog("Disposed.");
            int? taskId = Task.CurrentId;
            if (taskId != null)
            {
                Task tt = null;
                foreach (Task t in TaskOc)
                {
                    if (taskId == t.Id)
                    {
                        tt = t;
                        break;
                    }
                }
                if (tt != null)
                    TaskOc.Remove(tt);
            }
            InRun = false;
        }