コード例 #1
0
        static void Main(string[] args)
        {
            PolishCalc calc = new PolishCalc();

            TCPServer.TCPServer server = new TCPServer.TCPServer(calc);
            server.RunServer();
        }
コード例 #2
0
        private static void Main()
        {
            TCPServer.TCPServer tcpServer         = new TCPServer.TCPServer();
            CancellationToken   cancellationToken = new CancellationToken();

            tcpServer.Input(cancellationToken);
        }
コード例 #3
0
        public void Input(CancellationToken parentCancellationToken)
        {
            TCPServer.TCPServer tcpServer = new TCPServer.TCPServer();

            cancelationTokenSource = new CancellationTokenSource();
            CancellationToken childCancellationToken = cancelationTokenSource.Token;

            Task tcpServerTask = new Task(() => tcpServer.Input(childCancellationToken), cancelationTokenSource.Token, TaskCreationOptions.LongRunning);

            tcpServerTask.Start();

            Output(parentCancellationToken);

            cancelationTokenSource.Cancel();
        }
コード例 #4
0
        MyBitConverter <long> _NotifyIDConverter = new MyBitConverter <long>(); // converter dùng để convert byte thành long, số long nhận được chính là notificationID được yêu cầu bởi client
        #endregion

        public ClientCommunicator(Params.RawNotificationClientCommunicatorParams param)
        {
            _Param = param;
            server = new TCPServer.TCPServer(_Param.ListenPort);
            // đăng ký sự kiện nhận được một gói tin từ client
            server.OnPacketReceived += (s, e) =>
            {
                FromClientPacket packet = null;
                try
                {
                    packet = PacketConverter.BytesToObject(e.Data);
                }
                catch
                {
                    // Bên ngoài gửi gói tin không đúng định dạng, có thể do lỗi đường truyền cũng có thể do ý đồ phá hoại
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(ServerResult.DataCorrupt, null)));
                    e.CloseConnection();
                    return;
                }
                switch (packet.Type)
                {
                case FromClientPacketType.Register:
                {
                    RegisterPacketData data = packet.Data as RegisterPacketData;
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(Register(data), null)));
                    e.CloseConnection();
                }
                break;

                case FromClientPacketType.GetNotificationContent:
                {
                    GetNotificationContentPacketData data = packet.Data as GetNotificationContentPacketData;
                    ServerResult result;
                    // lấy ra content
                    byte[] content = NotificationContent(data.NotificationID, out result);
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(result, content)));
                    e.CloseConnection();
                }
                break;
                }
            };
        }
コード例 #5
0
        internal ServerCommunicator(int listenPort, int notifyPort, Func <ServerInfo> OnServerInfoRequestReceived, Action OnSendAllNotificationRequestReceived)
        {
            _ListenPort = listenPort;
            _NotifyPort = notifyPort;

            server                   = new TCPServer.TCPServer(_ListenPort);
            _NotifyServer            = new TCPServer.TCPServer(_NotifyPort, true);
            server.OnPacketReceived += (s, e) =>
            {
                System.Diagnostics.Debug.WriteLine("Đã nhận được yêu cầu");
                FromClientPacket ReceivedPacket = null;
                try
                {
                    ReceivedPacket = _FromClientConverter.BytesToObject(e.Data);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Gói tin bị lỗi khi vận chuyển");
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.DataCorrupted, ex)));
                    return;
                }
                // Giữa 2 server với nhau phải luôn được kết nối, vì vậy khi giao tiếp xong thì vẫn để kết nối đó cho lần tiếp theo
                switch (ReceivedPacket.PacketType)
                {
                case FromClientPacketType.AddNotification:
                {
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.AddNotificationResponse, AddNotification(ReceivedPacket.Data as AddNotificationPacketData)))); break;
                }

                case FromClientPacketType.RequestServerInfo:
                {
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.ServerInfo, OnServerInfoRequestReceived()))); break;
                }

                case FromClientPacketType.SendAllNotification:
                {         // nhận được yêu cầu gửi thì khỏi cần hồi âm
                    OnSendAllNotificationRequestReceived(); break;
                }
                }
            };
        }
コード例 #6
0
        public void Input()
        {
            bool whileContinue     = true;
            bool notValidResponse1 = false;
            int  threads;

            Client.Client       client    = new Client.Client();
            TCPServer.TCPServer tcpServer = new TCPServer.TCPServer();
            CancellationToken   ct        = new CancellationToken();

            Task serverTask = new Task(() => tcpServer.Input(ct), TaskCreationOptions.LongRunning);

            serverTask.Start();

            do
            {
                Console.WriteLine();
                Console.Write("Please enter an initial number of threads: ");

                string ifString = Console.ReadLine();

                if (int.TryParse(ifString.Trim(), out threads))
                {
                    while (whileContinue)
                    {
                        Console.WriteLine();
                        Console.Write("Number of threads: " + threads);
                        Console.WriteLine();

                        Parallel.For(0, threads, index => client.Input(new string[] { "-t", "username", "location" }));

                        bool notValidResponse2 = false;

                        do
                        {
                            Console.WriteLine();
                            Console.Write("Would you like to continue Y/N or enter a number of threads: ");

                            ifString = Console.ReadLine().Trim();

                            int tempThreads;

                            if (int.TryParse(ifString, out tempThreads))
                            {
                                threads = tempThreads;
                            }
                            else
                            {
                                ifString = ifString.ToUpper();

                                if (ifString == "Y")
                                {
                                    threads++;
                                }
                                else
                                {
                                    if (ifString == "N")
                                    {
                                        whileContinue = false;
                                    }
                                    else
                                    {
                                        Console.WriteLine();
                                        Console.Write("Please enter a valid response");

                                        notValidResponse2 = true;
                                    }
                                }
                            }
                        } while (notValidResponse2);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.Write("Please enter a valid response");

                    notValidResponse1 = true;
                }
            } while (notValidResponse1);
        }
コード例 #7
0
 public Form1()
 {
     InitializeComponent();
     thisServer = new TCPServer.TCPServer((int)num_ClientPort.Value);
     thisServer.OnPacketReceived += ThisServer_OnPacketReceived;
 }