Пример #1
0
        void ToPacket(DefinedProtocol.eReceiveMessage receiveID, object str, int idx)
        {
            DefinedStructure.PacketInfo packetRecieve1;
            packetRecieve1._id   = (int)receiveID;
            packetRecieve1._data = new byte[1024];
            byte[] temp = ConvertPacket.StructureToByteArray(str);
            for (int n = 0; n < temp.Length; n++)
            {
                packetRecieve1._data[n] = temp[n];
            }
            packetRecieve1._totalSize = temp.Length;

            _sendQueue.Enqueue(new sPacketData(ConvertPacket.StructureToByteArray(packetRecieve1), idx));
        }
Пример #2
0
        void DoOrder()
        {
            try
            {
                while (true)
                {
                    if (_receiveQueue.Count != 0)
                    {
                        rPacketData rPacket = _receiveQueue.Dequeue();

                        try
                        {
                            DefinedStructure.PacketInfo packetSend = new DefinedStructure.PacketInfo();
                            packetSend = (DefinedStructure.PacketInfo)ConvertPacket.ByteArrayToStructure(rPacket._buffer, packetSend.GetType(), rPacket._bufferLength);

                            string logMessage = string.Empty;
                            switch ((DefinedProtocol.eSendMessage)packetSend._id)
                            {
                            case DefinedProtocol.eSendMessage.Connect_Message:

                                DefinedStructure.Packet_LoginAfter clientInfo = new DefinedStructure.Packet_LoginAfter();
                                clientInfo = (DefinedStructure.Packet_LoginAfter)ConvertPacket.ByteArrayToStructure(packetSend._data, clientInfo.GetType(), packetSend._totalSize);

                                DefinedStructure.Packet_Login packet_login;
                                packet_login._UUID = GetUUIDFromDB(clientInfo._name);

                                ToPacket(DefinedProtocol.eReceiveMessage.Connect_User, packet_login, rPacket._index);

                                if (_currentUsers.Count != 0)
                                {
                                    foreach (long key in _currentUsers.Keys)
                                    {
                                        DefinedStructure.Packet_CurrentUser packetCurrent;
                                        packetCurrent._UUID        = key;
                                        packetCurrent._avatarIndex = _currentUsers[key];

                                        ToPacket(DefinedProtocol.eReceiveMessage.Current_User, packetCurrent, rPacket._index);
                                    }
                                }

                                break;

                            case DefinedProtocol.eSendMessage.Connect_After:

                                DefinedStructure.Packet_LoginAfter packet_loginAfter = new DefinedStructure.Packet_LoginAfter();
                                packet_loginAfter = (DefinedStructure.Packet_LoginAfter)ConvertPacket.ByteArrayToStructure(packetSend._data, packet_loginAfter.GetType(), packetSend._totalSize);

                                _currentUsers.Add(packet_loginAfter._UUID, packet_loginAfter._avatarIndex);
                                _currentUserName.Add(packet_loginAfter._UUID, packet_loginAfter._name);

                                logMessage = string.Format("{0} 유저 접속\t\t{1}", packet_loginAfter._name, DateTime.Now);

                                ToPacket(DefinedProtocol.eReceiveMessage.Connect_After, packet_loginAfter, -999);

                                break;

                            case DefinedProtocol.eSendMessage.Chatting_Message:

                                DefinedStructure.Packet_Chat packet_chat = new DefinedStructure.Packet_Chat();
                                packet_chat       = (DefinedStructure.Packet_Chat)ConvertPacket.ByteArrayToStructure(packetSend._data, packet_chat.GetType(), packetSend._totalSize);
                                packet_chat._chat = string.Format("{0} : {1}", _currentUserName[packet_chat._UUID], packet_chat._chat);

                                ToPacket(DefinedProtocol.eReceiveMessage.Chatting_Message, packet_chat, -999);

                                logMessage = string.Format("{0}\t\t{1}", packet_chat._chat, DateTime.Now);

                                break;

                            case DefinedProtocol.eSendMessage.Disconnect_Message:

                                DefinedStructure.Packet_Login packet_logOut = new DefinedStructure.Packet_Login();
                                packet_logOut = (DefinedStructure.Packet_Login)ConvertPacket.ByteArrayToStructure(packetSend._data, packet_logOut.GetType(), packetSend._totalSize);

                                DefinedStructure.Packet_LoginAfter packet_out;
                                packet_out._UUID        = packet_logOut._UUID;
                                packet_out._name        = _currentUserName[packet_logOut._UUID];
                                packet_out._avatarIndex = _currentUsers[packet_logOut._UUID];

                                logMessage = string.Format("{0} 유저 퇴장\t\t{1}", packet_out._name, DateTime.Now);

                                ToPacket(DefinedProtocol.eReceiveMessage.Disconnect_User, packet_out, -999);

                                _currentUserName.Remove(packet_logOut._UUID);
                                _currentUsers.Remove(packet_logOut._UUID);

                                _clients[rPacket._index].Shutdown(SocketShutdown.Both);
                                _clients[rPacket._index].Close();
                                _clients[rPacket._index] = null;
                                _clients.RemoveAt(rPacket._index);

                                break;
                            }

                            if (!string.IsNullOrEmpty(logMessage))
                            {
                                _totalLog.Add(logMessage);
                                Console.WriteLine(logMessage);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }

                    Thread.Sleep(1);
                }
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine(e.Message);
            }
        }