Example #1
0
 //���X�|���X�p�P�b�g�̑��M
 void Send(SockUdp sockUdp,PacketDhcp sp)
 {
     //���M
     sockUdp.Send(sp.GetBuffer());
     //this.Logger.Set(LogKind.Detail,sockUdp,4,string.Format("{0} {1} {2}",sp.Mac,(sp.RequestIp == null) ? "0.0.0.0" : sp.RequestIp.ToString(),sp.Type.ToString()));
     Log(sockUdp, 4, sp.Mac,sp.RequestIp,sp.Type);
 }
Example #2
0
        //アップロード(ファイル受信)
        bool UpLoad(SockUdp childObj,string path)
        {
            var ret = false;

            ushort no = 0;
            var totalSize = 0;
            FileStream fs = null;
            BinaryWriter bw = null;

            if (!(bool)Conf.Get("write")) {//「書込み」が許可されていない
                Logger.Set(LogKind.Secure,childObj,11,path);
                //エラーコード(2) アクセス違反
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(2),"Transmission of a message prohibition"));
                goto end;
            }
            if (!(bool)Conf.Get("override")) {//「上書き」が許可されていない
                if (File.Exists(path)) {
                    Logger.Set(LogKind.Secure,childObj,12,path);
                    //エラーコード(6) アクセス違反
                    childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(6),"There is already a file"));
                    goto end;
                }
            }

            //ACK(0)送信
            childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Ack),Util.htons(no)));

            try {
                fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
            } catch (Exception e) {
                //エラーコード(2) アクセス違反
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(2),e.Message));
                goto end;
            }

            bw = new BinaryWriter(fs);

            while (true) {
                //受信
            //                if (!childObj.Recv(Timeout)) {
                var buf = childObj.Recv(Timeout);
                if (buf.Length==0) {
                    Logger.Set(LogKind.Error,childObj,7,string.Format("{0}sec",Timeout));
                    break;
                }
                if ((Opcode)(buf[1]) != Opcode.Data) {
                    break;
                }

                //次のデータかどうかの確認
                if (Util.htons(BitConverter.ToUInt16(buf,2)) != no + 1)
                    continue;
                no++;

                int size = buf.Length - 4;
                bw.Write(buf,4,size); //Write
                totalSize += size;

                //ACK送信
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Ack),Util.htons(no)));

                if (size != 512) {
                    Logger.Set(LogKind.Normal,childObj,8,string.Format("{0} {1}byte",path,totalSize));
                    ret = true;
                    goto end;
                }
                Thread.Sleep(1);
            }
            end:
            if(bw!=null)
                bw.Close();
            if(fs!=null)
                fs.Close();
            return ret;
        }
Example #3
0
 private void Udp(SockUdp sockUdp)
 {
     var buf = sockUdp.RecvBuf;
     sockUdp.Send(buf);
     //echoしたらセッションを閉じる
 }
Example #4
0
        //ダウンロード(ファイル送信)
        bool DownLoad(SockUdp childObj,string path)
        {
            var ret = false;
            var no = (ushort)1;
            var total = 0;
            FileStream fs = null;
            BinaryReader br = null;

            if (!(bool)Conf.Get("read")) {//「読込み」が許可されていない
                Logger.Set(LogKind.Secure,childObj,10,path);
                //エラーコード(2) アクセス違反
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(2),"Receive of a message prohibition"));
                goto end;
            }

            if (!File.Exists(path)) {//ファイルが見つからない
                Logger.Set(LogKind.Secure,childObj,13,path);
                //エラーコード(1) ファイルが見つからない
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(1),"A file is not found"));
                goto end;
            }

            try {
                fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read);
            } catch (Exception e) {
                //エラーコード(2) アクセス違反
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(2),e.Message));
                goto end;
            }

            br = new BinaryReader(fs);

            while (true) {
                var data = br.ReadBytes(512);

                //DATA 送信
                childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Data),Util.htons(no),data));
                total += data.Length;

                if (data.Length < 512) {
                    if (data.Length == 0) {
                        //最後の 0bytes データを送る
                        childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Data),Util.htons(no)));
                    }
                    Logger.Set(LogKind.Normal,childObj,9,string.Format("{0} {1}byte",path,total));
                    ret = true;
                    goto end;
                }

                Thread.Sleep(10);

                // ACK待ち
                //if (!childObj.Recv(Timeout)) {
                var buf = childObj.Recv(Timeout);
                if (buf.Length==0) {
                    Logger.Set(LogKind.Error,childObj,7,string.Format("{0}sec",Timeout));
                    break;
                }
                if ((Opcode)(buf[1]) != Opcode.Ack)
                    break;
                //ACK番号が整合しているかどうかの確認
                var ackNo = Util.htons(BitConverter.ToUInt16(buf,2));
                if (no != ackNo) {
                    Logger.Set(LogKind.Error,childObj,14,string.Format("no={0} ack={1}",no,ackNo));
                    //エラーとして処理する
                    childObj.Send(Bytes.Create(Util.htons((ushort)Opcode.Error),Util.htons(2),"unmatch ACK"));
                    goto end;
                }
                no++;//次のデータ
            }
            end:
            if (br != null)
                br.Close();
            if (fs != null)
                fs.Close();
            return ret;
        }