コード例 #1
0
 /// <summary>Creates a new packet with a given ID. Used for sending.</summary>
 /// <param name="_id">The packet ID.</param>
 public Packet(int _id)
 {
     buffer     = new List <byte>(); // Initialize buffer
     readPos    = 0;                 // Set readPos to 0
     packetType = (Server.Packets)_id;
     Write(_id);                     // Write packet id to the buffer
 }
コード例 #2
0
 public ChatServer(TextWriter logger)
     : base(logger)
 {
     Packets.Register(DataIdentifier.LogIn, ProcessLoginPacket);
     Packets.Register(DataIdentifier.Message, ProcessMessagePacket);
     Packets.Register(DataIdentifier.LogOut, ProcessLogoutPacket);
 }
コード例 #3
0
        public void OnConnectionRequest(ConnectionRequest rq)
        {
            try
            {
                string name = rq.Data.GetString(rq.Data.GetInt());
                Guid   uid  = new Guid(rq.Data.GetRemainingBytes());

                //TODO Check validaty

                NetPeer peer = rq.Accept();
                players.Add(peer.Id, new NetPlayer(peer, name, uid));
            } catch (Exception e)
            {
                rq.Reject(Packets.CreateProtocolError("Invalid connection request."));
            }
        }
コード例 #4
0
        private void Start(IPAddress host, int port)
        {
            if (listener == null)
            {
                Console.WriteLine(">> Initialize Protocol...");
                Packets.InitPacketList();

                connections = new Dictionary <uint, Connection>();

                udpClient = new UdpClient(port);

                listener = new TcpListener(host, port);
                listener.Start();
                listener.BeginAcceptTcpClient(OnAccept, listener);
                udpClient.BeginReceive(OnRead, udpClient);
            }
        }
コード例 #5
0
        public void RUN()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                serverMessage("Server-start\nStorage Path:" + pathText.Text + "\n");
                this.m_Listener = new TcpListener(Int32.Parse(portText.Text));
            }));
            this.m_Listener.Start();

            serverMessage("waiting client access...\n");


            Socket client = this.m_Listener.AcceptSocket();

            if (client.Connected)
            {
                serverMessage("client access!\n");
                this.m_NetStream = new NetworkStream(client);
            }

            serverMessage("리스트 전송...\n");

            this.Invoke(new MethodInvoker(delegate()
            {
                for (int i = 0; i < musicList.Items.Count; i++)
                {
                    m_musicList        = new MusicList();
                    m_musicList.Type   = (int)PacketType.음악리스트;
                    m_musicList.Length = musicList.Items.Count;
                    string strText     = null;
                    for (int j = 0; j < 4; j++)
                    {
                        strText += musicList.Items[i].SubItems[j].Text;
                        strText += "_";
                    }
                    m_musicList.list = strText;
                    Packets.Serialize(m_musicList).CopyTo(this.sendBuffer, 0);

                    this.Send();
                }
            }));

            serverMessage("리스트 전송 완료\n");


            /* 요청 파일 전송 대기 */
            int nRead = 0;

            while (client.Connected)
            {
                serverMessage("요청 대기중...\n");
                try
                {
                    nRead = 0;
                    nRead = this.m_NetStream.Read(this.readBuffer, 0, 1024 * 4);
                }catch
                {
                    this.m_NetStream = null;
                    break;
                }
                this.m_requestMusic = (RequestMusic)Packets.Deserialize(this.readBuffer);
                this.requestNo      = this.m_requestMusic.requestNo;
                int    index    = Int32.Parse(this.requestNo);
                string filename = fi[index].Name;

                /* mp3 길이 전송 */
                string songname = pathText.Text + "\\\\" + filename;

                fs = new FileStream(songname, FileMode.Open, FileAccess.Read);
                int    fileLength = (int)fs.Length;
                byte[] lengthB    = BitConverter.GetBytes(fileLength);

                /* mp3 파일명 전송 */
                m_fileName          = new FileName();
                m_fileName.fileName = filename;
                m_fileName.Length   = fileLength;
                Packets.Serialize(m_fileName).CopyTo(this.sendBuffer, 0);
                this.Send();
                /* mp3 전송 */
                int          count = fileLength / (1024 * 4) + 1;
                BinaryReader br    = new BinaryReader(fs);
                for (int i = 0; i < count; i++)
                {
                    lengthB = br.ReadBytes(1024 * 4);
                    lengthB.CopyTo(this.sendBuffer, 0);
                    this.Send();
                }
                br.Close();
                fs.Close();
            }
        }