Ejemplo n.º 1
0
 public static WorkSocketFactory GetWorkSocketServer()
 {
     if (singleton == null)
     {
         singleton = new WorkSocketFactory();
     }
     return(singleton);
 }
Ejemplo n.º 2
0
 public void Initialize(String2 key)
 {
     try
     {
         SendHandShake(key);
         Receive();
     }
     catch (Exception e)
     {
         LastException = e;
         WorkSocketFactory.GetWorkSocketServer().RemoveSocketClient(this);
     }
 }
Ejemplo n.º 3
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);
         }
     });
 }