コード例 #1
0
        public async Task SendAsync(LoginDataBuffer buffer)
        {
            {
                Byte[] sendData = buffer.Serialize();
                writer.WriteInt32(Encoding.UTF8.GetByteCount(buffer.bufferData));
                writer.WriteBytes(sendData);
                try
                {
                    await writer.StoreAsync();

                    await writer.FlushAsync();

                    // Debug.WriteLine("Send Complete");
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
            }
        }
コード例 #2
0
        public async Task StartListener()
        {
            // Debug.WriteLine("Listner : Start Receive");

            listner.ConnectionReceived += async(s, e) =>
            {
                writer = new DataWriter(e.Socket.OutputStream);
                client = e.Socket;
                Debug.WriteLine("Listner : ConnectReceived");
                using (DataReader reader = new DataReader(e.Socket.InputStream))
                {
                    try
                    {
                        ClientConnected = true;
                        while (true)
                        {
                            uint sizeFiledCount = await reader.LoadAsync(4);

                            if (sizeFiledCount != 4)
                            {
                                break;
                            }
                            sizeFiledCount = reader.ReadUInt32();
                            Debug.WriteLine(sizeFiledCount);
                            uint message_len = await reader.LoadAsync(sizeFiledCount);

                            string message    = reader.ReadString(message_len);
                            var    bufferInfo = message.Split(new char[] { '@' });
                            if (bufferInfo[0] == "LoginData")
                            {
                                LoginDataBuffer buffer;
                                if (bufferInfo[1] == "Success")
                                {
                                    buffer = new LoginDataBuffer(message, true);
                                }
                                else
                                {
                                    buffer = new LoginDataBuffer(message, false);
                                }

                                if (LoginDataReceived != null)
                                {
                                    LoginDataReceived(buffer);
                                }
                            }
                            else if (bufferInfo[0] == "Download")
                            {
                                var buffer = new FileBuffer();
                                buffer.FileName = bufferInfo[1];
                                Debug.WriteLine(buffer.FileName);
                                await reader.LoadAsync(4);

                                buffer.FileLen = reader.ReadInt32();
                                Debug.WriteLine("Read FileLen:" + buffer.FileLen.ToString());

                                StorageFolder folder = ApplicationData.Current.LocalFolder;
                                // Debug.WriteLine(folder.Path);
                                var file = await folder.CreateFileAsync(buffer.FileName, CreationCollisionOption.ReplaceExisting);

                                using (var stream = await file.OpenStreamForWriteAsync())
                                {
                                    int  index = 0;
                                    int  total = buffer.FileLen;
                                    uint numReadBytes;
                                    do
                                    {
                                        numReadBytes = await reader.LoadAsync(1024);

                                        byte[] b = new byte[numReadBytes];
                                        reader.ReadBytes(b);
                                        stream.Write(b, 0, (int)numReadBytes);
                                    }while (numReadBytes > 0);
                                }
                                using (var stream = await file.OpenStreamForReadAsync())
                                {
                                    buffer.FileData = new byte[stream.Length];
                                    stream.Read(buffer.FileData, 0, buffer.FileData.Length);
                                }

                                if (FileReceived != null)
                                {
                                    FileReceived(buffer);
                                }
                                // Debug.WriteLine("Complete File Received");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        e.Socket.Dispose();
                    }
                }
            };



            await listner.BindEndpointAsync(new HostName(ip), "" + port);
        }