public WebSocketClient(WebSocketServer server, Socket s) { this.server = server; clientSocket = s; s.BeginReceive(this.buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(this.ReadCallback), this); this.OnPacketReceived += new PacketRecievedHandler(delegate(WebSocketClient client, WebSocketFrame frame) { if (frame.opcode == (byte)WebSocketOpcode.CLOSE) { client.clientSocket.Close(); } }); this.OnPacketReceived += new PacketRecievedHandler(delegate(WebSocketClient client, WebSocketFrame frame) { if (frame.opcode == (byte)WebSocketOpcode.PING) { WebSocketFrame f = new WebSocketFrame(); f.opcode = (byte)WebSocketOpcode.PONG; f.payloadData = frame.payloadData; client.Send(f); } }); }
private void LaunchWAMPEvents(WebSocketClient c, WebSocketFrame frame) { if (frame.opcode == (byte)WebSocketOpcode.CLOSE) { return; } if (!frame.fin) { if (fragFrame == null) { fragFrame = frame; } fragStream.Write(frame.payloadData, 0, (int)frame.payloadLength); } else if (fragFrame != null) { fragStream.Seek(0, SeekOrigin.Begin); byte[] buf = new byte[fragStream.Length]; fragStream.Read(buf, 0, buf.Length); fragFrame.payloadData = buf; fragFrame.Parse(fragFrame.Encode()); } JArray wampPacket = JArray.Parse(Encoding.UTF8.GetString(frame.payloadData)); switch ((WAMPMessageType)((int)wampPacket [0])) { case WAMPMessageType.HELLO: this.OnWAMPHello(wampPacket); break; case WAMPMessageType.SUBSCRIBE: this.OnWAMPSubscribe(wampPacket); break; case WAMPMessageType.UNSUBSCRIBE: this.OnWAMPUnsubscribe(wampPacket); break; case WAMPMessageType.PUBLISH: this.OnWAMPPublish(wampPacket); break; case WAMPMessageType.CALL: this.OnWAMPCall(wampPacket); break; case WAMPMessageType.WELCOME: Console.WriteLine("WAMP Server: Aborting Connection (Unexpected Message Type)"); JArray array = new JArray(); array.Add(WAMPMessageType.ABORT); JObject reasons = new JObject(); reasons.Add("message", "WAMP Server: Aborting Connection (Unexpected Message Type)"); array.Add(reasons); array.Add("wamp.error.UNEXPECTED_MESSAGE"); this.Send(array.ToString()); break; } }
private void ReadCallback(IAsyncResult ar) { //TODO DoS? What happens if packet is too long? Console.WriteLine("Receiving Data"); if (packetStream == null || !packetStream.CanWrite) { packetStream = new MemoryStream(); } int bytesRead = this.clientSocket.EndReceive(ar); if (bytesRead > 0) { packetStream.Write(buffer, 0, bytesRead); Console.WriteLine("Got {0} bytes of data", bytesRead); byte[] buf = packetStream.ToArray(); //Handshake first... if (handshake == null) { string strHandshake = Encoding.UTF8.GetString(buf); //End of an HTTP header if (strHandshake.EndsWith("\r\n\r\n")) { Console.WriteLine("Got Handshake"); this.DoHandshake(strHandshake); } packetStream.Close(); packetStream = new MemoryStream(); } else { while (WebSocketFrame.HaveFullPacket(buf)) { Console.WriteLine("Got a Packet"); WebSocketFrame frame = new WebSocketFrame(); //Read bytes off the stream ulong readBytes = frame.Parse(buf); if (OnPacketReceived != null) { OnPacketReceived(this, frame); } //Pull off unread bytes byte[] newbuf = new byte[(ulong)buf.Length - readBytes]; Array.Copy(buf, (int)readBytes, newbuf, 0, newbuf.Length); buf = newbuf; //Refresh the stream packetStream.Close(); packetStream = new MemoryStream(); packetStream.Write(newbuf, 0, newbuf.Length); } } if (clientSocket.Connected) { clientSocket.BeginReceive(this.buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(this.ReadCallback), this); } } }