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()); }
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); }
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); }
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); }
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()); }
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(); } }
public bool CheckEnd(String2 separator) { return(CheckEnd(separator.ToBytes())); }