private void MessageBroker(object objeto) { ClientSocketPacket packet = (ClientSocketPacket)objeto; MessageEventArgs args = new MessageEventArgs(); args.Message = packet.Message; args.ClientSocket = packet.CurrentSocket; args.ClientNumber = packet.ClientNumber; args.TipoMsg = packet.Message.Substring(0, 2); if (OnRequestReceived != null) { OnRequestReceived(this, args); } }
private void WaitForData(Socket socket, int clientNumber) { try{ if (WorkerCallBack == null) { WorkerCallBack = new AsyncCallback(OnDataReceived); } ClientSocketPacket _SocketPacket = new ClientSocketPacket(socket, clientNumber); socket.BeginReceive(_SocketPacket.sizeBuffer, 0, _SocketPacket.sizeBuffer.Length, SocketFlags.None, WorkerCallBack, _SocketPacket ); } catch (SocketException se) { logger.Error("WaitForData(): " + se.Message, se); throw se; } }
private void OnDataReceived(IAsyncResult asyn) { ClientSocketPacket socketData = (ClientSocketPacket)asyn.AsyncState; try { lock (this) { int ResultBytesLength = socketData.CurrentSocket.EndReceive(asyn); if (ResultBytesLength <= 0) { logger.Warn("Encerrou conexao (" + ResultBytesLength + ") recebendo tamanho"); this.CloseSocket(socketData.CurrentSocket); return; } // Se recebeu menos que 4 bytes, recebe os bytes restantes para compor o tamanho while (ResultBytesLength < 4) { int BytesReceived = socketData.CurrentSocket.Receive(socketData.sizeBuffer, ResultBytesLength, 4 - ResultBytesLength, SocketFlags.None); if (BytesReceived <= 0) { logger.Warn("Encerrou conexao (" + ResultBytesLength + ") recebendo complemento do tamanho"); this.CloseSocket(socketData.CurrentSocket); return; } ResultBytesLength += BytesReceived; } char[] charsLength = new char[ResultBytesLength]; System.Text.Decoder _Decoder = System.Text.Encoding.UTF8.GetDecoder(); _Decoder.GetChars( socketData.sizeBuffer, 0, ResultBytesLength, charsLength, 0); int BufferSize = 0; int pos = 0; BufferSize += (((int)socketData.sizeBuffer[pos++]) & 0xFF) << 24; BufferSize += (((int)socketData.sizeBuffer[pos++]) & 0xFF) << 16; BufferSize += (((int)socketData.sizeBuffer[pos++]) & 0xFF) << 8; BufferSize += (((int)socketData.sizeBuffer[pos++]) & 0xFF); int TotalBytesReceived = 0; string Message = string.Empty; while (TotalBytesReceived < BufferSize) { int ToReceive = BufferSize - TotalBytesReceived; byte[] dataBuffer = new byte[ToReceive]; int BytesReceived = socketData.CurrentSocket.Receive(dataBuffer, ToReceive, SocketFlags.None ); if (BytesReceived <= 0) { logger.Warn("Encerrou conexao (" + ResultBytesLength + ") recebendo dados"); this.CloseSocket(socketData.CurrentSocket); return; } char[] charLenght = new char[BytesReceived]; System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder(); decoder.GetChars(dataBuffer, 0, BytesReceived, charLenght, 0); Message += new string(charLenght); TotalBytesReceived += BytesReceived; } LastPacket = DateTime.Now; ClientSocketPacket pkt = new ClientSocketPacket(); pkt.ClientNumber = socketData.ClientNumber; pkt.CurrentSocket = socketData.CurrentSocket; pkt.Message = Message; pkt.MsgLen = BufferSize; Array.Copy(socketData.sizeBuffer, pkt.sizeBuffer, 4); ThreadPool.QueueUserWorkItem(new WaitCallback(MessageBroker), pkt); this.WaitForData(socketData.CurrentSocket, socketData.ClientNumber); } } catch (SocketException se) { //if (se.ErrorCode == (int)(SocketError.ConnectionReset)){ // this.CloseSocket(socketData.CurrentSocket); //} logger.Error("OnDataReceived(SocketEx): " + se.Message + " ErrorCode: " + se.ErrorCode + " Native: " + se.NativeErrorCode + " SocketError: " + se.SocketErrorCode, se); logger.Warn("OnDataReceived(): Encerrando conexao com MDS"); // Falha de conexão this.CloseSocket(socketData.CurrentSocket); } catch (Exception ex) { logger.Error("OnDataReceived(): " + ex.Message, ex); } }