예제 #1
0
        private void MultiThreadingForSocket(object data)
        {
            var threadHandler         = (Socket)data;
            var threadCommandParser   = new CommandParser();
            var threadCommandExecuter = new CommandExecuter();

            UpdateSocketsData(threadHandler);
            SocketCommandsListnerRun(threadHandler, threadCommandParser, threadCommandExecuter);
        }
예제 #2
0
        private void SocketCommandsListnerRun(Socket threadHandler, CommandParser threadCommandParser, CommandExecuter threadCommandExecuter)
        {
            var socketsModel = sockets.First(x => x.handler == threadHandler);
            var threadTripleEndPointModel = new TripleEndPointModel()
            {
                EndPoint         = threadHandler.LocalEndPoint,
                EndPointUDPRead  = endPointModel.EndPointUDPRead,
                EndPointUDPWrite = socketsModel.EndPointUDPWrite
            };

            while (true)
            {
                while (threadHandler.Connected)
                {
                    StringBuilder builder    = new StringBuilder();
                    int           bytes      = 0;
                    byte[]        socketData = new byte[256];

                    do
                    {
                        try
                        {
                            bytes = threadHandler.Receive(socketData);
                        }
                        catch (Exception exc)
                        {
                        }

                        builder.Append(Encoding.ASCII.GetString(socketData, 0, bytes));
                        bytes = 0;
                        if (builder.ToString().Contains("\r\n"))
                        {
                            break;
                        }
                    }while (threadHandler.Connected && !builder.ToString().Contains("\r\n"));
                    var commandString = builder.ToString();
                    threadHandler.ReceiveTimeout = 0;
                    threadCommandExecuter.ContinueExecuteCommandThreading(threadHandler, threadCommandParser.ParseCommand(commandString), socketsModel.socketUDP, socketsModel.socketUDPRead, threadTripleEndPointModel);
                    if (!threadHandler.Connected)
                    {
                        Console.WriteLine("this is SERVER");
                        return;
                    }
                }
            }
        }