コード例 #1
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (!(obj is String2))
            {
                return(false);
            }
            String2 temp = obj as String2;

            if (Length != temp.Length)
            {
                return(false);
            }
            for (int i = 0; i < Length; i++)
            {
                if (this[i] != temp[i])
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
        public String2[] Split(String2 source)
        {
            if (source.Length < 1)
            {
                throw new ArgumentNullException("The length of split for source is short.");
            }
            if (Length < source.Length)
            {
                return(new String2[] { Copy() });
            }
            int             count = 0;
            int             pre   = 0;
            int             peek  = 0;
            String2LinkNode fin   = new String2LinkNode();
            String2LinkNode node  = fin;

            while ((peek = IndexOf(source, pre)) > -1)
            {
                node = node.Add(SubString(pre, peek - pre));
                pre  = peek + source.Length;
                count++;
            }
            node = node.Add(SubString(pre, Length - pre));
            String2[] ret = new String2[count + 1];
            for (int i = 0; i < ret.Length; i++)
            {
                ret[i] = fin.Data;
                fin    = fin.Next;
            }
            return(ret);
        }
コード例 #3
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
        public String2 Replace(String2 oldVal, String2 newVal)
        {
            if (oldVal.Length < 1)
            {
                throw new ArgumentNullException("Replace");
            }
            if (Length < oldVal.Length)
            {
                return(Copy());
            }
            int             pre  = 0;
            int             peek = 0;
            String2LinkNode fin  = new String2LinkNode();
            String2LinkNode node = fin;

            while ((peek = IndexOf(oldVal, pre)) > -1)
            {
                node = node.Add(SubString(pre, peek - pre));
                node = node.Add(newVal);
                pre  = peek + oldVal.Length + 1;
            }
            node = node.Add(SubString(pre, Length - pre));
            String2 ret = fin.Data;

            while (fin.Next != null)
            {
                fin = fin.Next;
                if (fin.IsNull())
                {
                    break;
                }
                ret += fin.Data;
            }
            return(ret);
        }
コード例 #4
0
        private void SendHandShake(String2 key)
        {
            String2 temp = new String2(Encoding.UTF8);

            temp += "HTTP/1.1 101 Switching Protocols" + String2.CRLF;
            temp += "Upgrade: websocket" + String2.CRLF;
            temp += "Connection: Upgrade" + String2.CRLF;
            temp += "Sec-WebSocket-Accept:" + ComputeHash(key) + String2.CRLF + String2.CRLF;
            client.Send(temp.ToBytes());
        }
コード例 #5
0
        private String2 ComputeHash(String2 key)
        {
            if (SHA == null)
            {
                SHA = SHA1CryptoServiceProvider.Create();
            }
            String buffer = key.Trim().ToString() + Define.GUID;

            byte[] hash = SHA.ComputeHash(Encoding.ASCII.GetBytes(buffer));
            return(new String2(Convert.ToBase64String(hash), Encoding.UTF8));
        }
コード例 #6
0
 public void Initialize(String2 key)
 {
     try
     {
         SendHandShake(key);
         Receive();
     }
     catch (Exception e)
     {
         LastException = e;
         WorkSocketFactory.GetWorkSocketServer().RemoveSocketClient(this);
     }
 }
コード例 #7
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
 public int IndexLastOf(String2 source)
 {
     if (source.Length < 1)
     {
         return(-1);
     }
     if (Length < source.Length)
     {
         return(-1);
     }
     for (int i = Length - source.Length - 1; i >= 0; i--)
     {
         if (String2.CheckByte(data, i, source.ToBytes(), 0, source.Length))
         {
             return(i);
         }
     }
     return(-1);
 }
コード例 #8
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
 public int IndexOf(String2 source, int index)
 {
     if (source.Length < 1)
     {
         return(-1);
     }
     if (Length < source.Length + index)
     {
         return(-1);
     }
     for (int i = index; i <= Length - source.Length; i++)
     {
         if (String2.CheckByte(data, i, source.ToBytes(), 0, source.Length))
         {
             return(i);
         }
     }
     return(-1);
 }
コード例 #9
0
ファイル: ClientImpl.cs プロジェクト: nowonbun/nowonbunstore
        public String2 Receive(int length)
        {
            String2 buffer      = new String2(length);
            String2 retBuffer   = new String2(0);
            int     revlength   = 0;
            int     totallength = 0;

            while ((revlength = stream.Read((byte[])buffer, 0, length)) > 0)
            {
                buffer       = buffer.SubString(0, revlength);
                retBuffer   += buffer;
                totallength += revlength;
                if (totallength >= length)
                {
                    break;
                }
            }
            return(retBuffer);
        }
コード例 #10
0
ファイル: ClientImpl.cs プロジェクト: nowonbun/nowonbunstore
        public String2 Receive()
        {
            String2 buffer    = new String2(Define.BUFFER_SIZE);
            String2 retBuffer = new String2(0);
            int     revlength = 0;

            while ((revlength = stream.Read(buffer.ToBytes(), 0, buffer.Length)) > 0)
            {
                buffer     = buffer.SubString(0, revlength);
                retBuffer += buffer;

                if (retBuffer.CheckEnd(Define.CRLF))
                {
                    break;
                }
                buffer = new byte[Define.BUFFER_SIZE];
            }
            return(retBuffer);
        }
コード例 #11
0
        private byte[] BuildHeader(ResponeCode code, HeaderOption option = null)
        {
            String2 ret = new String2(Encoding.UTF8);

            if (object.Equals(code, ResponeCode.CODE200))
            {
                ret += "HTTP/1.1 200 OK" + String2.CRLF;
            }
            else
            {
                ret += "HTTP/1.1 " + ((int)code) + " NG" + String2.CRLF;
            }
            if (option != null)
            {
                foreach (String key in option.ToResult().Keys)
                {
                    ret += key + ": " + option.ToResult()[key] + String2.CRLF;
                }
            }
            ret += "Keep-Alive: timeout=15, max=93" + String2.CRLF;
            ret += "Connection: Keep-Alive" + String2.CRLF + String2.CRLF;
            return(ret.ToBytes());
        }
コード例 #12
0
 public void Send(int opcode, String2 data)
 {
     try
     {
         if ((opcode == (int)Opcode.MESSAGE) || (opcode == (int)Opcode.BINARY) && data != null)
         {
             if (data.Length <= 0x80)
             {
                 client.Send(new byte[] { (byte)(0x80 | 1), (byte)data.Length });
             }
             else if (data.Length <= 65535)
             {
                 client.Send(new byte[] { (byte)(0x80 | 1), (byte)0x7E });
                 client.Send(Util.Reverse(BitConverter.GetBytes((short)data.Length)));
             }
             else
             {
                 client.Send(new byte[] { (byte)(0x80 | 1), (byte)0x7F });
                 client.Send(Util.Reverse(BitConverter.GetBytes((long)data.Length)));
             }
             client.Send(data.ToBytes());
             return;
         }
         else if ((opcode == (int)Opcode.PING) || (opcode == (int)Opcode.PONG))
         {
             client.Send(new byte[] { (byte)(0x80 | opcode), (byte)0x00 });
             return;
         }
         throw new Exception("This setting is wrong OPCODE = " + opcode);
     }
     catch (Exception e)
     {
         LastException = e;
         client.Close();
     }
 }
コード例 #13
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
        public String2 ToBinary()
        {
            String2 ret = new String2(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, Encode);

            for (int i = 0; i < 0x10; i++)
            {
                ret += String.Format(" 0x{0:X2}", i);
            }
            ret += CRLF;
            for (int i = 1, j = 0x00; i < Length; i++, j += 0x10)
            {
                --i;
                bool header = true;
                ret += CRLF;
                ret += String.Format("0x{0:X2}    ", j);
                for (; i < Length && (i % 0x10 == 0 && !header); i++)
                {
                    ret   += String.Format("0x{0:X2}", this[i]);
                    header = false;
                }
            }
            ret += CRLF;
            return(ret);
        }
コード例 #14
0
 public void Initialize(String2 header)
 {
     try
     {
         String2[] buffer = header.Split(" ");
         if (buffer.Length != 3 || buffer[1] == null)
         {
             throw new FormatException();
         }
         String temp = buffer[1].ToString().Trim();
         int    pos  = temp.IndexOf("?");
         if (pos < 0)
         {
             this.Path = temp;
             return;
         }
         this.Path      = temp.Substring(0, pos);
         this.Parameter = temp.Substring(pos + 1, temp.Length - (pos + 1));
     }
     catch (Exception e)
     {
         throw new FormatException(null, e);
     }
 }
コード例 #15
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
 public String2LinkNode Add(String2 data)
 {
     this.data = data;
     this.next = new String2LinkNode();
     return(this.next);
 }
コード例 #16
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
 public int IndexOf(String2 source)
 {
     return(IndexOf(source, 0));
 }
コード例 #17
0
ファイル: String2.cs プロジェクト: nowonbun/nowonbunstore
 public bool CheckEnd(String2 separator)
 {
     return(CheckEnd(separator.ToBytes()));
 }
コード例 #18
0
 private void Receive()
 {
     ThreadPool.QueueUserWorkItem(_ =>
     {
         try
         {
             while (true)
             {
                 String2 data = null;
                 byte opcode  = (byte)0;
                 String2 head = Receive(2);
                 if (head.Length < 2)
                 {
                     throw new Exception("header size");
                 }
                 bool fin = (head[0] & 0x80) == 0x80;
                 if (!fin)
                 {
                     throw new Exception("Fin error");
                 }
                 opcode     = (byte)(head[0] & 0x0f);
                 bool mask  = (head[1] & 0x80) == 0x80;
                 int length = head[1] & 0x7F;
                 if (length == 0x7E)
                 {
                     length = BitConverter.ToInt16(Receive(2).Reverse().ToBytes(), 0);
                 }
                 if (length == 0x7F)
                 {
                     length = (int)BitConverter.ToInt64(Receive(8).Reverse().ToBytes(), 0);
                 }
                 String2 key = mask ? Receive(4) : null;
                 if (opcode == (int)Opcode.MESSAGE)
                 {
                     byte[] buffer = Receive(length).ToBytes();
                     if (key != null)
                     {
                         for (int i = 0; i < buffer.Length; i++)
                         {
                             buffer[i] = (byte)(buffer[i] ^ key[i % 4]);
                         }
                     }
                     data = new String2(buffer, Encoding.UTF8);
                     receive(this, opcode, data);
                     continue;
                 }
                 if (opcode == (int)Opcode.BINARY)
                 {
                     byte[] buffer = Receive(length).ToBytes();
                     if (key != null)
                     {
                         for (int i = 0; i < buffer.Length; i++)
                         {
                             buffer[i] = (byte)(buffer[i] ^ key[i % 4]);
                         }
                     }
                     data = buffer;
                     receive(this, opcode, data);
                     continue;
                 }
                 if (opcode == (int)Opcode.EXIT)
                 {
                     return;
                 }
                 if ((opcode == (int)Opcode.PING) || (opcode == (int)Opcode.PONG))
                 {
                     Send(opcode);
                     continue;
                 }
                 throw new Exception("This opcode is wrong. Receive OPCODE - " + opcode);
             }
         }
         catch (Exception e)
         {
             LastException = e;
             client.Close();
         }
         finally
         {
             WorkSocketFactory.GetWorkSocketServer().RemoveSocketClient(this);
         }
     });
 }