private void AcceptWS(IAsyncResult res) { if (!shouldRun) { return; } Socket sock = server.EndAccept(res); byte[] buffer = new byte[1024]; sock.Receive(buffer); Dictionary <string, string> headers = getHeaders(UTF8Encoding.UTF8.GetString(buffer)); bool worked = TryHandshake(sock, headers); if (!worked) { sock.Close(); } else { sockets.Add(sock); RecieveStruct state = new RecieveStruct(); state.buffer = new byte[WSREADSIZE];//4 bytes state.sock = sock; sock.BeginReceive(state.buffer, 0, WSREADSIZE, SocketFlags.None, SocketDataRecieved, state); } server.BeginAccept(AcceptWS, null); }
private void SocketDataSent(IAsyncResult e) { RecieveStruct state = (RecieveStruct)e.AsyncState; if (state.sock.Connected) { state.sock.EndSend(e); } }
protected void SendData(Socket socket, string data) { WSPacket packet = new WSPacket(); packet.opcode = 0x1; packet.textData = data; packet.lastPacket = true; byte[] newdata = EncodePacket(packet); RecieveStruct rs = new RecieveStruct(); rs.sock = socket; socket.BeginSend(newdata, 0, newdata.Length, SocketFlags.None, SocketDataSent, rs); }
private void AcceptWS(IAsyncResult res){ if (!shouldRun) { return; } Socket sock = server.EndAccept(res); byte[] buffer = new byte[1024]; sock.Receive(buffer); Dictionary<string,string> headers = getHeaders(UTF8Encoding.UTF8.GetString(buffer)); bool worked = TryHandshake(sock,headers); if (!worked) { sock.Close(); } else { sockets.Add(sock); RecieveStruct state = new RecieveStruct(); state.buffer = new byte[WSREADSIZE];//4 bytes state.sock = sock; sock.BeginReceive(state.buffer,0,WSREADSIZE,SocketFlags.None,SocketDataRecieved,state); } server.BeginAccept(AcceptWS, null); }
protected void SendData(Socket socket,string data){ WSPacket packet = new WSPacket(); packet.opcode = 0x1; packet.textData = data; packet.lastPacket = true; byte[] newdata = EncodePacket(packet); RecieveStruct rs = new RecieveStruct(); rs.sock = socket; socket.BeginSend(newdata, 0, newdata.Length, SocketFlags.None, SocketDataSent, rs); }
private void SocketDataRecieved(IAsyncResult e) { RecieveStruct state = (RecieveStruct)e.AsyncState; int ecode = 0; try { ecode = state.sock.EndReceive(e); } catch (SocketException) { sockets.Remove(state.sock); return; } catch (ObjectDisposedException) { sockets.Remove(state.sock); return; } if (ecode == 0) { //We should NEVER get zero data. sockets.Remove(state.sock); return; } WSPacket packet; try { packet = DecodePacket(state.buffer, state.sock); } catch (Exception) { CloseSocket(state.sock); return; } if (packet.opcode == OPCODE_INTERNAL_FAIL) { //Decoder could not get any more data from the packet, so the connection must be terminated. sockets.Remove(state.sock); return; } if (packet.lastPacket) { if (fragments.ContainsKey(state.sock)) { byte[] lastPacket = fragments[state.sock]; byte[] buffer = new byte[lastPacket.Length + packet.binaryData.Length]; Array.Copy(lastPacket, buffer, lastPacket.Length); Array.Copy(packet.binaryData, 0, buffer, lastPacket.Length, packet.binaryData.Length); fragments[state.sock] = buffer; packet.binaryData = buffer; if (packet.opcode == OPCODE_TEXT) { packet.textData = Encoding.UTF8.GetString(packet.binaryData); } fragments.Remove(state.sock); } packet.sender = state.sock; //Do something with the packet. if (packet.opcode == OPCODE_CLOSE) { state.sock.Close(); } else if (packet.opcode == OPCODE_PING) { WSPacket newpack = new WSPacket(); packet.opcode = 0xA; packet.length = 0; packet.lastPacket = true; byte[] newdata = EncodePacket(newpack); state.sock.BeginSend(newdata, 0, newdata.Length, SocketFlags.None, SocketDataSent, state); } else { processingQueue.Enqueue(packet); if (CanInterrupt) { UpdateThread.Interrupt(); } } } else { if (!fragments.ContainsKey(state.sock)) { fragments.Add(state.sock, packet.binaryData); } else { byte[] lastPacket = fragments[state.sock]; byte[] buffer = new byte[lastPacket.Length + packet.binaryData.Length]; lastPacket.CopyTo(buffer, 0); packet.binaryData.CopyTo(buffer, lastPacket.Length); fragments[state.sock] = buffer; } } if (state.sock.Connected) { state.sock.BeginReceive(state.buffer, 0, WSREADSIZE, SocketFlags.None, SocketDataRecieved, state); } }